You are on page 1of 22

Stack

LIFO/FILO data structure.


• LIFO stands for Last-in-first-out
• FILO stands for First in Last out

Implementation
• Array (fixed size )
• Linked List (dynamic resizing)

1
Operations
• peek() − get the top data element of the stack,
without removing it.
• isFull() − check if stack is full.
• isEmpty() − check if stack is empty.

• push() − Pushing (storing) an element on the


stack.
• pop() − Removing (accessing) an element from
the stack.

2
Array implementation
• Initially top=-1
• Push  increment top by 1
• Pop  decrement top by 1

3
Implementation of peek()
int peek()
{
return stack[top];
}

4
Implementation of isfull()
bool isfull()
{
if(top == MAXSIZE-1)
return true;
else
return false;
}

5
Implementation of isempty()
bool isempty()
{
if(top == -1)
return true;
else
return false;
}

6
Implementation of push()
void push(int data)
{
if(!isFull())
{
top = top + 1;
stack[top] = data;
}
else
{
printf("Could not insert data, Stack is full.\n");
}
}

7
Implementation of pop()
int pop()
{
if(!isempty())
{
data = stack[top];
top = top - 1;
return data;
}
else
{
printf("Could not retrieve data, Stack is empty.\n");
}
}

8
Linked List: Push operation
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;

top = newNode;
Print “Insertion is Success!!!";
}

9
Linked List: Pop operation
void pop()
{
if(top == NULL)
print “Stack is Empty";
else
{
struct Node *temp = top;
Print “deleted element", temp->data ;
top = temp->next;
free(temp);
}
}

10
Linked List: display
void display()
{
if(top == NULL)
print “Stack is Empty";
Else
{
struct Node *temp = top;
while(temp->next != NULL)
{
Print temp->data ;
temp = temp -> next;
}

}
}

11
Application of stack
• Infix to postfix conversion
• Evaluation of postfix expressions
• Balanced parentheses
• function calls

12
Infix Expression into Postfix Expression
Algorithm
1. Scan the infix expression from left to right until $ symbol.
2. If the scanned character is an operand, output it.
3. If the scanned character is an operator
3.1 check If the precedence of the scanned operator is greater
priority than the precedence of the operator in the stack(or the stack
is empty), push it.
3.2 Else, Pop the operator from the stack until the precedence
of the scanned operator is less or equal priority to the precedence of
the operator residing on the top of the stack. Push the scanned
operator to the stack.
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop and output from the stack
until an ‘(‘ is encountered.
6. Repeat steps 2-6 until infix expression is scanned.
7. If input symbol is $ then Pop all symbols and output it until stack is
not empty.

13
Example convert ( A + B ) * ( C - D ) into
postfix

14
15
Postfix Evaluation Algorithm
1. Read all the symbols one by one from left to right in the
given Postfix Expression until $ symbol
2. If the reading symbol is operand, then push it on to the
Stack.
3. If the reading symbol is operator (+ , - , * , / etc.,), then
perform TWO pop operations and store the two popped
operands in two different variables (operand2 and
operand1). Then perform operation using operand1 and
operand2 and push result back on to the Stack.
4. Repeat step 2,3 until all postfix expression operands are
consumed
5. If input symbol is $ then perform pop operation and
display the popped value as final result.

16
17
18
Balanced parentheses

Algorithm:
1) Read all the symbols one by one from left to
right in the given infix Expression until $ symbol.
2) a) If the current character is a starting bracket
‘(‘ then push it to stack.
b) If the current character is a closing bracket ‘)’
then perform pop operation if stack is empty
then it is “not balanced”
3) After complete traversal, if there is some
starting bracket left in stack then “not balanced”
else “it is balanced”.

19
Example
1. (a+b)$
Input Stack Operation
( ( Push (
) Pop (
$ Empty stack
Print balanced parenthesis

2. ((a+b)$
Input Stack Operation
( ( Push (
( (( Push (
) ( Pop (
$ ( Stack is not empty
Print it is not balanced parenthesis

20
function calls

• Functions are executed based on stack


operation
• Last invoked functions executed first

21
Infix to postfix conversion
1.A * B ^ C + D
2.A * (B + C * D) + E
3.(4+8)*(6-5)/((3-2)*(2+2))
4.a-b+c*d/e

Postfix Expression Evaluation


1. 6 2 / 3 – 4 2 * +
2. 4 2 ^ 3 * 3 -8 4 / 1 1 + / +
3. 6 2 3 + - 3 8 2 / + * 2 ^ 3 +
4. 7 5 3 2 ^ * 9 2 2 ^ - / + 6 4 * +

22

You might also like