Linked List
Linked List
Ashish Patel
1 What is Stack?
2 Applications of Stack
3 Operations on Stack
Reversing String
Evaluating expressions
Backtracking
Parenthesis matching
item pop() → Remove the top element from the stack and return it to the
caller.
item top() → Return the top element of the stack without removing it.
Only top item can be popped from the stack decreasing stack size by one. If
no top item is available in the stack, it is empty and called stack underflow.
pop ( )
{
if ( t o p < 0 )
{
p r i n t f ( ‘ ‘ S t a c k U n d e r f l o w ’’ ) ;
return ;
}
item = s t a c k [ top ] ;
top = top − 1 ;
}
The above figure demonstrates push and pop operations on the stack.
1 Initially stack is empty and top = −1, when user inserts first value, say 10,
the function first increments top, setting it’s value to 0.
2 Using the same method 20 and 30 are inserted and top = 2.
3 Next operations is pop. The only value user can pop from the stack is 30.
After popping 30, the code decrements top by 1, setting top = 1.
4 Then 40 is pushed onto the stack, setting top = 2.
5 The last operation is pop, which removes 40 from the stack and set the
value top = 1.
Ashish Patel (SVMIT) Data Structures (3130702) 9 / 14
C Program: Stack using array
while ( m y S t r i n g [ i n d e x ] != ’ \0 ’ )
{
pu sh (& s t k , m y S t r i n g [ i n d e x ] ) ;
i n d e x ++;
}
END