CH 2 - Stack
CH 2 - Stack
● POP operation: The pop operation is used to remove or delete the top
element from the stack.
● When we remove an
item, we say that we
popped it from the
stack.
● When an item popped,
it is always the top
item which is removed.
Stack Operation [3]
● The PUSH and the POP operations are the basic or primitive operations on a
stack.
Some others operations are:
● CreateEmptyStack operation: This operation is used to create an empty
stack.
● IsFull operation: The isfull operation is used to check whether the stack is full
or not ( i.e. stack overflow)
● IsEmpty operation: The isempty operation is used to check whether the stack
is empty or not. (i. e. stack underflow)
● Top operations: This operation returns the current item at the top of the stack.
However this operation doesn’t remove the current item.
Stack as an ADT
A stack of elements of type T is a finite sequence of elements of T together with the
following operations:
● CreateEmptyStack(S): Create or make stack S be an empty stack
● Push(S, x): Insert x at one end of the stack, called its top
● Top(S): If stack S is not empty; then retrieve the element at its top but don’t
delete.
● Pop(S): If stack S is not empty; then delete the element at its top
● IsFull(S): Determine if S is full or not.
Return true if S is full stack; return false otherwise
● IsEmpty(S): Determine if S is empty or not.
Return true if S is an empty stack; return false otherwise.
Stack Implementation
Steps:
1. Initially, we get empty stack. The top of an empty stack is set to -1.
2. Next, we push an element into the stack. The top of the stack will now point to
the element just pushed.
3. Whenever, we push element into the stack, the top is incremented so as to
point to the recent element.
4. Whenever a pop operation is performed, the top element is removed from the
stack and top is decrement to point to the next element.
5. If the stack becomes empty, the top will be set to -1.
Stack Implementation [2]
Stack Implementation [3]
struct Stack{
struct Node{
Int top;
int data;
Int data[MAX];
struct Node *next;
Static Stack Implementation
Algorithm InitializeEmptyStack(S)
1. Start
2. Assign the value to top to -1. ie s->top = -1
3. Stop
Static Stack Implementation[2]
Algorithm isFull(S)
1. Start
2. Check if the top of S is equal to MAX-1, return true
ie
if(s->top == MAX-1)
return true;
3. Else return false
4. Stop
Static Stack Implementation[3]
Algorithm isEmpty(S)
1. Start
2. Check if the top of S is equal to -1, return true
ie
if(s->top == -1)
return true;
3. Else return false
4. Stop
Static Stack Implementation[4]
Algorithm push(S,x)
1. Start
2. Check if the S is full,
2.1. if full then display “Stack is full” and exit
2.2. Else, go to step 3
3. Increment top by 1 ie s->top += 1
4. Place the item x into “top” ie: s->items[s->top] = x
5. Stop
Static Stack Implementation[4]
Algorithm pop(S)
1. Start
2. Check if the S is empty,
2.1. if empty then display “Stack is empty” and exit
2.2. Else, go to step 3
3. Place the item at “top” to x ie:
x = s->items[s->top]
4. Decrement top by 1 ie s->top -= 1
5. Stop
Dynamic Stack Implementation
Algorithm InitializeEmptyStack(S)
1. Start
2. Assign S to null pointer
3. Stop
Dynamic Stack Implementation[2]
Algorithm isFull(S)
1. Start
2. Try allocating a new node as new
3. If new is a valid pointer, then
Algorithm isEmpty(S)
1. Start
2. Check if S is null,
3. If so, return true
4. Else if S is a valid pointer, return false.
5. Stop
Dynamic Stack Implementation[4]
Algorithm push(S,x)
1. Start
2. Check if S is full, if so then
2.1 display “Stack is full”
2.2 return
1. Allocate a new node as newNode with data as x.
2. Start with a node pointer p = S
3. Move p to next node until p->next != null
4. Set p->next = newNode.
5. Stop
Dynamic Stack Implementation[4]
Algorithm pop(S)
1. Start
2. Check if S is empty, if so then
2.1 display “Stack is empty”
2.2 return
1. Initialize a node pointer p = S
2. Set x = p->data and S = p->next
3. Delete p
4. Return the value of x
5. Stop
Infix, Prefix and Postfix Notation
● One of the applications of the stack is to evaluate the arithmetic expression.
● We can represent the arithmetic expressions following three types of notation:
○ Infix Prefix Postfix
● Infix expression: It is an ordinary mathematical notation of expression where
operator is written in between the operands.
Example: A+B. Here ‘+’ is an operator and A and B are called operands
● Prefix notation: In prefix notation the operator precedes the two operands. That
is the operator is written before the operands. It is also called polish notation.
Example: +AB
● Postfix notation: In postfix notation the operators are written after the operands
so it is called the postfix notation (post mean after). In this notation the operator
follows the two operands.
Example: AB+
● Both prefix and postfix are parenthesis free expressions.
Infix, Prefix and Postfix Notation [2]
4. Post-fix form
Infix to Postfix Conversion [4]
=>A + (B * C)
Algorithm
● Step 1: Reverse the infix expression. Note that while reversing the infix
expression, the left and right parentheses are interchanged.
● Step 2: Obtain the postfix expression of the input expression as obtained in
[1].
● Step 3: Reverse the postfix expression to obtain the prefix expression.
Infix to prefix conversion[3]
5E$DCB$A+*+
+*+A$BCD$E5
Infix to prefix conversion[4]
Algorithm
ABCD$+*EF$GH/*-
DO THIS:
123+*321-+* => ??
Evaluation of prefix expression
Algorithm: