UNIT 4 - Stack
UNIT 4 - Stack
4.1 Introduction
2)It is a linear list where all insertions and deletions are permitted only at one end of the list.
4)In a stack, when an element is added, it goes to the top of the stack.
Definition
“Stack is a collection of similar data items in which both insertion and deletion operations are
performed based on LIFO
principle”.
1. Create :-
2. Push :-
The push operation adds a new element to the stack. As stated above, any element added to the
stack goes at the top, so push adds an element at the top of a stack
In a stack, push() is a function used to insert an element into the stack. In a stack, the new element is
always inserted at top position.
Push function takes one integer value as parameter and inserts that value into the stack.
We can use the following steps to push an element on to the stack..
Step 2 - If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and terminate the
function.
Step 3 - If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value
(stack[top] = value).
3 Pop :-
The pop operation removes and also returns the top-most (or most recent element) from the stack.
In a stack, pop() is a function used to delete an element from the stack. In a stack, the element is
always deleted from top
We can use the following steps to pop an element from the stack...
Step 2 - If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not possible!!!" and terminate the
function.
Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--).
3. isEmpty() :-
1)This operation checks whether a stack is empty or not i.e., if there is any element present in the
stack or not.
2)When a stack is completely full, it is said to be Overflow state and if stack is completely empty, it is
said to be Underflow state.
4. Isfull() :-
This operation checks whether the stack isfull. It returns TRUE if stack is full and false otherwise
Step 2 - If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the function.
Step 3 - If it is NOT EMPTY, then define a variable 'i' and initialize with top. Display stack[i] value and
decrement i value by one (i--).
2)In a stack, inserting and deleting of elements is performed at a single position which is known as,
Top.
3)Insertion operation can be performed using Push() function and deletion operation can be
performed using Pop() function .
3. Reversing a string.
5. Stacks can be used for Conversion from one form of expression to another.
The stack can be used to convert some infix expression into its postfix equivalent, or prefix
equivalent.
These postfix or prefix notations are used in computers to express some expressions.
backtracking is, that is solving all sub-problems one by one in order to reach the best possible
solution.
Functions of Stack:
void push()
int val;
if(top==MAX-1) {
printf("\nStack is full!!"); }
else {
scanf("%d",&val);
top=top+1;
stack[top]=val;
void pop()
if(top==-1)
printf("\nStack is empty!!"); }
else {
top=top-1; }
void pop()
int i;
if(top == -1) {
else
printf("\n%d", stack[i]); }
To convert infix expression to postfix expression, use the stack data structure. Scan the infix
expression from left to right. Whenever we get an operand, add it to the postfix expression and if we
get an operator or parenthesis add it to the stack by maintaining their precedence.
● If the precedence of the current scanned operator is higher than the precedence of
the operator on top of the stack, or if the stack is empty, or if the stack contains a ‘(‘,
then push the current operator onto the stack.
● Else, pop all operators from the stack that have precedence higher than or equal to
that of the current operator. After that push the current operator onto the stack.
5. If the scanned character is a ‘)’, pop the stack and output it until a ‘(‘ is encountered, and
discard both the parenthesis.
1. Infix to Postfix Conversion Following table shows the evaluation of Infix to Postfix:
2)A/(B^C)+D
3)(A-CB/C)*(D*E-F)
4)A/B^C-D
i) (A + B) * C – D
ii) A + B * C – D/E * F
Practice above questions for SPPU Examination