4 Stacks
4 Stacks
Stacks
• Stack is an ordered list of items, ordered in the input sequence.
• Basically, items are inserted and deleted at one end, called the
top of the stack.
• Works on principle last in first out (LIFO).
• Other names used for stacks are piles and pushdown list.
• Adding an item
– Referred to as pushing it onto the stack.
• Removing an item
– Referred to as popping it from the stack.
Last In First Out
top
top E top
D D D
C top C C C
B top B B B B
A top
A A A A A
Empty push(A) push(B) push(C) push(D) push(E) pop(E)
stack
Implementation of Stacks
• Stack can be implemented based on
–Array (static implementation)
–Linked list (dynamic implementation)
4
Store data 10, as
3
st.stk[st.top]=10;
2
1
10 0 st.top
Array-based Stack Implementation
Empty
Stack
Push(56)
4
4 Increament top by 1, as
st.top++; 3
3 2
2 1 st.top
1 10 0
0
st.top= -1
4
Store data 56, as
3
st.stk[st.top]=56;
2
56 1 st.top
10 0
Array-based Stack Implementation
Empty
Stack
push(10) push(56) push(80) push(32)
4 4 4 4 4
3 3 3 3 32 3 st.top
2 2 2 80 2 st.top 80 2
1 1 56 1 st.top 56 1 56 1
0 10 0 st.top 10 0 10 0 10 0
st.top= -1
45 4 st.top 4 4 4 4
32 3 32 3 st.top 3 3 3
80 2 80 2 80 2 st.top 2 2
56 1 56 1 56 1 56 1 st.top 1
10 0 10 0 10 0 10 0 10 0 st.top
Empty
Stack
4
3
2
1
0 st.top= -1
Array-based Stack Implementation continue…..
Algorithm :
1.Define structure containing array for stack and top as
variable for top of stack.
2.Initialize stack top to -1.
3.Perform push operation,
-Before pushing data onto stack ,
check stack is full or not.
-If full, print as stack is full.
-else
Enter the data to be pushed from user.
Increment the counter i.e. top
Insert that data onto stack
Push Operation
printf("\n\tPUSH");
if(st.top==MAX-1)
{
printf("\n\tSTACK IS FULL!");
}
else
{
printf("\n\tENTER THE ELEMENT TO BE PUSHED: ");
scanf("%d",&num);
st.top++;
st.stk[st.top]=num;
}
Push Operation & stack full condition
using function
void push(int num)
{ if(full()==1)
int full()
printf("\n\tSTACK IS FULL!");
{
else
if(st.top==max-1)
{
return 1;
st.top++;
else
st.stk[st.top]=num;
return 0;
}
}
}
Array-based Stack Implementation continue…..
if(st.top==-1)
{
printf("\n\tSTACK IS ENPTY!");
}
else
{
printf("\n\tTHE STACK IS:");
for(i=st.top; i>=0; i--)
{
printf("\n\t%d",st.stk[i]);
}
}
Display Operation using function
void display()
{
int i;
if(empty()==1)
printf(“\n\t STACK IS EMPTY”);
else
{
printf("\n\tTHE STACK IS:");
for(i=st.top; i>=0; i--)
{
printf("\n\t%d",st.stk[i]);
} //End of for loop
} // End of else
} // End of Display function
Linked list-based Stack Implementation
Algorithm :
1.Define structure containing data to be enter on the stack
and link to the next data.
2.Initialize stack top to NULL.
3.Perform push operation,
a. Create new node as p using malloc.
b. Enter data to be pushed onto the stack.
c. make p->next=NULL;
e. If top==NULL, make top=p;
struct stack
else, p->next=top;
{
top=p; int number;
struct stack *next;
}*top, *p;
Linked list-based Stack Implementation
Push(10)
Create the node using malloc function,
p
100
Enter the data as 10 in number field, as
p->number=10 and make next field as NULL and this is first node
in stack so top must point to p as top=p;
10 NULL
p
100
top
Linked list-based Stack Implementation
Push(30)
Create the node using malloc function,
p
150
Enter the data as 30 in number field, as p->number=30 and make
next field point to top.
p 30
150
top 10 NULL
100
Now top must be point node which is pushed last as top= p;
p 30
top 150
10 NULL
100
Linked list-based Stack Implementation
push(80)
p 80
top 300
30
150
10 NULL
100
p 80
top 300 Pop():
It will pop top element as p=top;
and top is pointing to node containing value 30 using
30 statement top=top->next;
150 And node get deleted using free(p);
10 NULL
100
top 30
150
10 NULL
100
Push Operation
void push() /*FUNCTION FOR PUSHING ELEMENTS*/
{
printf("\n\tENTER THE NUMBER TO BE PUSHED: ");
p=(struct stack*)malloc(sizeof (struct stack));
scanf("%d",&p->number);
p->next=NULL;
if(top==NULL)
{
top=p;
}
else
{
p->next=top;
top=p;
}
}
Linked list-based Stack Implementation continue…..
Stack empty
top == -1 top == NULL
condition
Programs:
1.1. Convert a decimal number to binary using stack.
2.2. Reverse the string using stack.
3.3. Check whether the a given string is palindrome or not.
Applications of Stacks
1.Nested parenthesis check or parenthesis
Matching
2.Expression evaluation
3.The underlying structure for recursion
4.Function calls (and its associated variables)
Real-time Uses of Stacks
• The runtime stack used by a
process (running program) to
keep track of methods in
progress
• Search problems
• Undo, redo, back, forward
Mathematical Calculations
• What does 3 + 2 * 4 equal?
or 10 / 2 + 5
What about 1 - 2 - 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 3
Application of stack-
Expression Evaluation and conversion
Consider an expression - A + B
Operand Operand
Operator
Evaluation of an Expression:
• To evaluate an infix Expression, we need the precedence and associativity
of the operators And which is very complex task.
• But what is do we mean by associativity?
Let us consider an example A+B+C. In this example the operators are
of same precedence, so whether to start with A+B or B+C. To resolve this
conflict we need to know the associativity of the operators i.e. whether to
start from left to right or from right to left.
Precedence And associativity
Operation Precedence Associativity Example
1. 653+9*+
2. ab*c+d-e+
where, a=5,b=4,c=10,d=15 & e=6
No empty - - - -
symbol
6 6 push(6) - - -
5 6,5 push(5)
3 6,5,3 push(3)
9 6,8,9 push(9) - - -
* 6,72 Pop top element and store in op2, 8 9 72
Pop top element and store in op1
Perform the operation op1*op2 and push result
on stack
- 78 Final result - - -
1.2. ab*c+d-e+ where, a=5,b=4,c=10,d=15 & e=6
Input Content of stack Operation op1 op2 result
Symbol
No empty - - - -
symbol
a=5 5 push(5) - - -
b=4 5,4 push(4)
Algorithm:
5. If the input symbol read is a closing parenthesis ‘)’ then pop all operators
from the stack, place them in output till the opening parenthesis is not
popped. So pop ‘(‘ & discard it.
6. If the end of input string is encountered, then iterate the loop until stack is
not empty. Pop the stack elements and append the remaining input string to
the output.
Examples:
1. a*b+c
2. a*b/(c-d)+e*(f-g)