0% found this document useful (0 votes)
13 views32 pages

unit-3

The document covers the concepts of stacks and queues, detailing their operations, implementations using arrays and linked lists, and applications such as expression evaluation. It explains stack operations (push, pop, peek, display) and queue operations (enqueue, dequeue, display), along with limitations and the concept of circular queues. Additionally, it includes algorithms for evaluating postfix expressions and converting infix expressions to postfix notation.

Uploaded by

evan.mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views32 pages

unit-3

The document covers the concepts of stacks and queues, detailing their operations, implementations using arrays and linked lists, and applications such as expression evaluation. It explains stack operations (push, pop, peek, display) and queue operations (enqueue, dequeue, display), along with limitations and the concept of circular queues. Additionally, it includes algorithms for evaluating postfix expressions and converting infix expressions to postfix notation.

Uploaded by

evan.mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

CHRIST

Deemed to be University

Unit-3
Stacks and Queues

Stacks- stacks using dynamic arrays- queues – circular queue using


dynamic arrays- Evaluation of Expressions, Evaluating Postfix
Expressions, Infix to Postfix.

Excellence and Service


Stack and Queue

MISSION VISION CORE VALUES


CHRIST is a nurturing ground for an individual’s holistic Excellence and Service Faith in God | Moral Uprightness
2
development to make effective contribution to the society in a Love of Fellow Beings
dynamic environment Social Responsibility | Pursuit of Excellence
CHRIST
Deemed to be University

What is a Stack
• Stack of Books

3
Excellence and Service
CHRIST
Deemed to be University

Stacks
• What can we do with a stack?
– push - place an item on the stack
– peek - Look at the item on top of the stack, but do not remove it
– pop - Look at the item on top of the stack and remove it

4
Excellence and Service
CHRIST
Deemed to be University

Stacks
• A stack is a LIFO (Last-In/First-Out) data structure
• A stack is sometimes also called a pushdown store.
• What are some applications of stacks?
– Program execution
– Parsing
– Evaluating postfix expressions
– Function calls

5
Excellence and Service
CHRIST
Deemed to be University

Operations of Stack

Push
Pop
Peek
Traversal (Display)

6
Excellence and Service
CHRIST
Deemed to be University

Stack

Stack overflow
Stack underflow

7
Excellence and Service
CHRIST
Deemed to be University

Push

• Step 1 - Check whether stack is FULL. (top == SIZE-1)


• 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).

Stack overflow

8
Excellence and Service
CHRIST
Deemed to be University

Pop

• Step 1 - Check whether stack is EMPTY. (top == -1)


• 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--).

Stack underflow

9
Excellence and Service
CHRIST
Deemed to be University

Display

• Step 1 - Check whether stack is EMPTY. (top == -1)


• 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--).
• Step 3 - Repeat above step until i value becomes '0'.

10
Excellence and Service
CHRIST
Deemed to be University

Limitations of stack using arrays

● Size of the array should be considered

11
Excellence and Service
CHRIST
Deemed to be University

Stack using linked list

12
Excellence and Service
CHRIST
Deemed to be University

Push

• Step 1 - Create a newNode with given value.


• Step 2 - Check whether stack is Empty (top == NULL)
• Step 3 - If it is Empty, then set newNode → next = NULL.
• Step 4 - If it is Not Empty, then set newNode → next = top.
• Step 5 - Finally, set top = newNode.

13
Excellence and Service
CHRIST
Deemed to be University

Pop

• Step 1 - Check whether stack is Empty (top == NULL).


• 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 define a Node pointer 'temp' and set it to
'top'.
• Step 4 - Then set 'top = top → next'.
• Step 5 - Finally, delete 'temp'. (free(temp)).

14
Excellence and Service
CHRIST
Deemed to be University

Display

• Step 1 - Check whether stack is Empty (top == NULL).


• 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 Node pointer 'temp' and initialize
with top.
• Step 4 - Display 'temp → data --->' and move it to the next node. Repeat
the same until temp reaches to the first node in the stack. (temp → next !
= NULL).
• Step 5 - Finally! Display 'temp → data ---> NULL'.

15
Excellence and Service
CHRIST
Deemed to be University

Evaluation of postfix expression

1) Add ) to postfix expression.

2) Read postfix expression Left to Right until ) encountered

3) If operand is encountered, push it onto Stack

[End If]

4) If operator is encountered, Pop two elements

i) A -> Top element

ii) B-> Next to Top element

iii) Evaluate B operator A

push B operator A onto Stack

5) Set result = pop

6) END
Excellence and Service
CHRIST
Deemed to be University

Expression: 456*+

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

example --2

Excellence and Service


CHRIST
Deemed to be University

try it by yourself

231*+9-

100 200 + 2 / 5 * 7 +

46+9*

Excellence and Service


CHRIST
Deemed to be University

ANSWERS

1. -4
2. 757
3. 90

Excellence and Service


CHRIST
Deemed to be University

Algorithm to convert Infix To Postfix


Let, X is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression Y.
1. Push “(“onto Stack, and add “)” to the end of X.
2. Scan X from left to right and repeat Step 3 to 6 for each element of X until the Stack is empty.
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto Stack.
5. If an operator is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) which has the same
precedence as or higher precedence than operator.
2. Add operator to Stack.
[End of If]
6. If a right parenthesis is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) until a left parenthesis is
encountered.
2. Remove the left Parenthesis.
[End of If]
[End of If]
7. END.
Excellence and Service
CHRIST
Deemed to be University

Infix Expression: A+ (B*C-(D/E^F)*G)*H, where ^ is an exponential operator.

Excellence and Service


CHRIST
Deemed to be University

Try it by yourself

a+b*c

(a+b)*c+(d-a)

((4+8)(6-5))/((3-2)(2+2))

Excellence and Service


CHRIST
Deemed to be University

ANSWERS

a b c * +

a b + c * d a - +

4 8 + 6 5 - 3 2 - 2 2 + /

Excellence and Service


CHRIST
Deemed to be University

QUEUE

● FIFO (First In First Out) principle.

26
Excellence and Service
CHRIST
Deemed to be University

Operations on a Queue

1. enQueue(value) - (To insert an element into the queue)


2. deQueue() - (To delete an element from the queue)
3. display() - (To display the elements of the queue)

27
Excellence and Service
CHRIST
Deemed to be University

enqueue
void enQueue(int value){
if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{
if(front == -1&& rear==-1)
{
front = 0;
rear=0;
}
else
rear++;

queue[rear] = value;
printf("\nInsertion success!!!");
}
}

28
Excellence and Service
CHRIST
Deemed to be University

dequeue
void deQueue(){
if(front == -1 && rear==-1)

printf("\nQueue is Empty!!! Deletion is not possible!!!");

else if ((front==rear) ||(front>rear))


{
printf("\nDeleted : %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nDeleted : %d", queue[front]);
front++;
}
}
29
Excellence and Service
CHRIST
Deemed to be University

Display

void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}
}

30
Excellence and Service
CHRIST
Deemed to be University

Drawbacks of queue

31
Excellence and Service
CHRIST
Deemed to be University

Circular queue

● Modulo arithmetic
● Queue full
○ Case 1 full
○ Case 2 empty
○ Few elements

● Queue empty
○ Case 1 empty
○ Case 2 one element
○ Few elements

32
Excellence and Service

You might also like