0% found this document useful (0 votes)
27 views4 pages

R23_DS_Unit III-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

R23_DS_Unit III-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Unit III [R23]

Stacks: Introduction to stacks: properties and operations, implementing stacks using arrays
and linked lists, Applications of stacks in expression evaluation, backtracking, reversing
list etc.

Introduction to Stacks:
 Stack is a linear data structure in which element insertion and deletion can be done at one end
called top of the stack.
 The top element of the stack is the last element added to the stack.
 Also the top element of the stack is the first element to be removed from the stack.
 So it works on the principle called Last in First out (LIFO) mechanism. Sometimes it is also
called as First in Last out (FILO) mechanism.
 Hence, this stack is also called as LIFO or FILO list.
 There are many real life examples of stack. For example plates stacked over one another in
canteen. CDs are placed in its stand. Shuttle cocks placed in barrel.

Operations: There are two basic operations can be performed over stacks, which are given as
1. PUSH
2. POP
3. PEEK
1. PUSH: This operation is used to insert an element into the stack. At the time of insertion of
element first check stack is full or not. If the stack is full then it generates an error message
that “stacks overflows”.
2. POP: This operation is used to delete an element from the stack. At the time of deletion of
element first check stack is empty or not. If the stack is empty then it generates an error
message “stack underflows”.
3. PEEK: This operation is used to get the top element of the stack without removing it. This
can be useful in situations where we need to check the top element without altering the state
of the stack.
Representation or Implementation:
A stack is implemented using two representations like
1. Array Implementation
2. Linked list Implementation

Array Implementation:
 In this implementation, stack elements are stored in an array.
 Every stack is associated with a variable called Top which indicates index of array to insert an
element or delete an element of stack.
 There is another variable called Max which is used to store maximum number of elements
that the stack can hold.
 Generally we initialize Top with -1. So, the condition Top==-1 indicates that stack is empty
and Top will be incremented by 1 for every PUSH operation and then the condition
Top==Max-1 indicates that stack is full.

Algorithm of PUSH:
1. Check if stack is full or not
2. If it is full then print an error message that stack overflows and exit
3. If it is not full then increment Top and insert element at Top index.

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 1


Step 1: if Top==Max-1 then print Overflow and go to step 4.
Step 2: set Top = Top+1
Step 3: set Stack[Top]=element
Step 4: exit.
This PUSH operation is implemented by the following function in C language
void push(int element)
{
if (top == MAX_SIZE - 1)
printf("Stack overflow! Cannot push element.\n");
else {
top++;
stack[top] = element;
printf("%d pushed to stack.\n", element);
}
}
Algorithm of POP:
1. Check if the stack is empty or not.
2. If it is empty then print an error message that stack underflows and exit
3. If it is not empty then delete element at Top index and decrement Top.

Step 1: if Top==-1 then print Underflow and goto step 4


Step 2: set element = Stack[Top]
Step 3: set Top = Top -1
Step 4: exit.
This POP operation is implemented by the following function in C language
int pop()
{
if (top == -1) {
printf("Stack underflow! Cannot pop element.\n");
return -1;
} else {
int element = stack[top];
top--;
return element;
}
}

2. Linked list Implementation:


 In this implementation, the stack is represented as linked list. i.e a linked list is used to
represent a stack.
 In this implementation, each element of the stack is stored as a node in the linked list.
 The main draw back in the array implementation is that array must have some fixed size.
Moreover we cannot insert the elements once stack is full.
 The key advantage of using a linked list for implementing a stack is that it provides dynamic
memory allocation which allows the stack to grow or shrink as needed without requiring a
fixed size like an array-based implementation.
 Each node contains two parts: the data (the value to be stored in the stack) and a pointer to the
next node. This node is implemented by the following structure in C,

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 2


struct Node {
int data;
struct Node* next;
};
typedef struct Node* NODE;
 The starting pointer of the linked list is used as Top.
 All the insertions and deletions are done at the Top.
 If the Top == NULL then it indicates that stack is empty.

Example:

The PUSH operations is implemented by using the following function in C language


void push(int element)
{
NODE n = (NODE)malloc(sizeof(NODE));
n->data = element;
if (Top == NULL)
n->next=NULL;
else
n->next=Top;
top = n;
}
The POP operations is implemented by using the following function in C language
int pop()
{
NODE t;
int ele;
if (top == NULL) {
printf("Stack underflow! Cannot pop element.\n");
return -1;
}
ele = top->data;
t = top;
top = top->next;
free(t);
return ele;
}
Applications:
1. It is used to convert infix expression into postfix expression.
2. It is used to evaluate postfix expression.
3. It is used to check for balancing of braces used in the program.

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 3


4. It is used in the backtracking techniques.
5. In the recursion all the intermediate arguments and return values are stored in the stacks,

Evaluation of Postfix expression:


Step 1: Read postfix expression from left to right.
Step 2: Initialize stack
Step 3: Repeat unit end of postfix expression.
a) If the character is operand then push into to the stack.
b) If the character is operator then pop top two elements of the stack and perform the
operation and then push the result back into stack.
Step 4: Finally, we have one element in the stack which is the result of expression and pop it.

Example: Evaluate the postfix expression 456*+.

Backtracking: Backtracking is another application of stack. It is a recursive algorithm that is


used to solve the optimization problem. Backtracking involves making choices at various points.
The stack stores information about these choices. When a chosen path does not leads to the
solution, and then algorithm needs to backtrack. By popping the top element from the stack, it
retrieves the information about the previous decision point. This allows the algorithm to undo the
last choice and explore a different option.
Consider the classic N-Queens problem, where the goal is to place N queens on an N×N
chessboard in such a way that no two queens threaten each other.

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy