Unit - 2 The Stack
Unit - 2 The Stack
Definition of stack:-
A stack is a non-primitive linear data structure. It is an ordered collection of item in to
which new item are inserted and form which item may be deleted at one end called the top of
stack unlike the array. As all the deletion and insertion in a stack is done form top of the stack,
the last added element will be the first to be removed from the stack. The definition of stack
provided dynamic (constantly changing object)
• When an item is added to the stacks it is pushed on stack.
• When an item is removed it is pop from stack therefore stack exhibits LIFO( Last In First
Out) or FILO (First In Last Out) properties.
Stack as an ADT:-
Though a very simple technique but is not a flexible way of creation, as the size of stack
has to be declared during program design, after that the size cannot be varied. So it isn’t too
efficient with respect to memory utilization. As the declaration of array is done before the
start of the operation, now if there are too few elements be stored in the stack the statically
allocated memory will be wasted, on the other hand if there are more number of elements to
be stored in the stack then we can’t be able to change the size of array of increase its capacity,
so that it can accommodate new element.
For more details:
https://www.youtube.com/watch?v=ZniDyolzrBw&t=22s
Operation on stack :-
i) Push (s, x):-
The process of adding a new element to the top of the stack or inserts an element
to stack is called push operation. As new element will be inserted at the top after every
push operation the top is incremented by one. In case the array is full and no new
elements can be accommodated, it is called stack full condition. This condition is called
stack over flow.
Algorithm of push
Push (stack[maxSIZE],item): this algorithm inserts an item at the top of the stack[maxSIZE].
Step 1 : initialize Set
top =-1
Step 2 : repeat steps 3 to 5 until top<maxSIZE-1
Step 3 : read, item
Step 4 : set top=top+1
Example:-
The function for the stack push operation in c is as follows. Considering stack is declared as
int stack[5], top=-1;
void push()
{
Int item ;
If(top<=4)
{
Printf(”enter the number to be insert:”);
Scanf(“%d”,&item);
Top=top+1;
Stack[top]=item;
}
Else
Printf(”stack overflow”);
}
Remove element from stack or the process of deleting an element from the top of stack is
called pop operation. After every pop operation the stack is decremented by one. If there is no
element on the stack and the pop is performed then this will result into stack underflow
condition.
Example:-
The function for the stack pop operation in c is as follows.
void pop()
{
Int item ;
If(top>=0)
{
Item=stack[top];
top=top-1;
Printf(”deleted number is: %d”,item);
}
Else
{
printf(”stack is empty”);}
Stack Applications:
• String reversal: Stack is also used for reversing a string. For example, we want to reverse
a "DataStructure" string, so we can achieve this with the help of a stack.
• UNDO/REDO: It can also be used for performing UNDO/REDO operations. For example,
we have an editor in which we write 'a', then 'b', and then 'c'; therefore, the text written
in an editor is abc. So, there are three states, a, ab, and abc, which are stored in a stack.
There would be two stacks in which one stack shows UNDO state, and the other shows
REDO state.
• Recursion: The recursion means that the function is calling itself again. To maintain the
previous states, the compiler creates a system stack in which all the previous records of
the function are maintained.
• DFS(Depth First Search): This search is implemented on a Graph, and Graph uses the stack
data structure.
• Backtracking: Suppose we have to create a path to solve a maze problem. If we are moving
in a particular path, and we realize that we come on the wrong way. In order to come at
the beginning of the path to create a new path, we have to use the stack data structure.
• Expression conversion: Stack can also be used for expression conversion. This is one of
the most important applications of stack. The list of the expression conversion is given
below:
Infix to prefix
Infix to postfix
Prefix to infix
Prefix to postfix
Postfix to infix
• Memory management: The stack manages the memory. The memory is assigned in the
contiguous memory blocks. The memory is known as stack memory as all the variables
are assigned in a function call stack memory. The memory size assigned to the program is
known to the compiler. When the function is created, all its variables are assigned in the
Type of expression:-
1. Infix expression
2. Prefix expression
3. Postfix expression
Infix expression:-
The infix notation is what we come across in our general mathematics, where the
operator is written in between the operands. For example: the expression to add two numbers
A and B is written in infix notation as: A+B. Note that the operator ‘+’ is written in between the
operands A and B. The reason why this notation is called infix is the place of operator in the
expression.
Prefix expression:-
It is a notation in which the operator is written before the operands, it is also called the
police notation in the honor of the mathematician Jan Lukasiewicz who developed this
notation the same expression when written in prefix notation look likes : +AB , as the operator
‘+’ is written before the operands A and B, this notation is called prefix (pre means before).
Postfix expression:-
In the postfix notation the operators are written after the operands, so it is called the
postfix notation (post means after), it is also known as suffix notation or reverse polish
notation. The above expression if written in postfix expression looks like follows: AB+.
Example 2:
Evaluate the expression 5 6 2 + * 12 4 / - in tabular form showing stack after every step
Step Input Symbol/Element Stack Output (if
credited)
1 5 Push 5
2 6 Push 5, 6
3 2 Push 5, 6, 2
4 + Pop (2 Element) 5 6+2=8
5 Push Result (8) 5, 8
6 * Pop (2 Element) 5*8=40
7 Push Result (40) 40
8 12 Push 40, 12
9 4 Push 40, 12, 4
10 / Pop (2 Element) 40 12/4=3
11 Push Result(3) 40, 3
12 - Pop (2 Element) 40-3=37
13 Push Result(37) 37
14 No more Elements 37 Results
Note:
I) if associativity L to R then pop & print the top of the stack & then push the incoming
operator
II) If associativity R to L then push the incoming operator