0% found this document useful (0 votes)
44 views

Stack and Queue

The document discusses linear data structures, specifically stacks and queues. It defines a stack as a structure that follows LIFO principles where elements are added and removed from one end called the top. Queues follow FIFO principles where elements are added to the rear and removed from the front. The document describes various stack and queue operations and applications such as expression evaluation, parenthesis matching, memory management, and task scheduling.

Uploaded by

Lavanya J
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Stack and Queue

The document discusses linear data structures, specifically stacks and queues. It defines a stack as a structure that follows LIFO principles where elements are added and removed from one end called the top. Queues follow FIFO principles where elements are added to the rear and removed from the front. The document describes various stack and queue operations and applications such as expression evaluation, parenthesis matching, memory management, and task scheduling.

Uploaded by

Lavanya J
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

UNIT- II

Linear data structure


Stack ADT
• Stack is a linear data structure in which the insertion and
deletion operations are performed at only one end. In a stack,
adding and removing of elements are performed at a single
position which is known as "top“

• In stack, the insertion and deletion operations are performed


based on LIFO (Last In First Out) principle or FILO(First In Last
Out).
Operations
• isFull()
• isEmpty()
• Push(x)
• Pop()
• Peek()
• Size()
• https://
towardsdatascience.com/stack-and-array-i
mplementation-with-python-and-nodejs-b
8b260229e3a
Applications of stack
• used for expression evaluation.
• used to check parenthesis matching in an
expression.
• used for Conversion from one form of
expression to another.
• used for Memory Management.
• used in backtracking problems.

• https://
www.youtube.com/watch?v=d_XvFOkQz5k
Evaluating arithmetic
expressions
• Polish notation(Prefix) +AB
• Reverse polish notation(Postfix ) AB+
• Infix notation A+B

• A+B/*C-
• (A+B)/(C-D)
• +AB-C
• http://www.btechsmartclass.com
/data_structures/infix-to-postfix.
Conversion of infix to postfix notation
Conversion of infix to postfix
• Place parentheses around every group of operators in the
correct order of evaluation. There should be one set of
parentheses for every operator in the infix expression
• ((A * B) + (C / D))
• For each set of parentheses, move the operator from the
middle to the end preceding the corresponding closing
parenthesis.
• ((A B *) (C D /) +)
• Remove all of the parentheses, resulting in the equivalent
postfix expression.
• AB*CD/+
Evaluating arithmetic
expressions
1) Postfix expression: 2 3 4 * +

Input Stack
234*+ empty Push 2
34*+ 2 Push 3
4*+ 32 Push 4
*+ 432 Pop 4 and 3, and
perform 4*3 = 12.
Push 12 into the stack.

+ 12 2 Pop 12 and 2 from the


stack, and perform
12+2 = 14. Push 14
into the stack.
2 )Postfix expression: 3 4 * 2 5 * +

Input Stack

34*25*+ empty Push 3

4*25*+ 3 Push 4
*2 5 * + 43 Pop 3 and 4 from the stack and
perform 3*4 = 12. Push 12 into
the stack.

25*+ 12 Push 2
5*+ 2 12 Push 5
*+ 5 2 12 Pop 5 and 2 from the stack and
perform 5*2 = 10. Push 10 into
the stack.

+ 10 12 Pop 10 and 12 from the stack


and perform 10+12 = 22. Push
22 into the stack.
Queue ADT
• Queue data structure is a collection of similar data
items in which insertion and deletion operations are
performed based on FIFO principle
Operations
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)
4.Queue() – creates a new empty queue
5.isEmpty()-returns a boolean value
6.Length()-returns the number of items currently in the
queue
https://www.geeksforgeeks.org/array-implementation-of-queue-
simple
/
Circular queue
• A circular queue is a linear data structure in which the
operations are performed based on FIFO (First In First Out)
principle and the last position is connected back to the first
position to make a circle.
Priority queue
• Every item has a priority associated with it.
• An element with high priority is dequeued before an element
with low priority.
• If two elements have the same priority, they are served
according to their order in the queue.

Types:
1)Bounded priority queue - small limited range of p priorities
over the interval of integers [0 . . . P]
2)Unbounded priority queue - no limit on the range of integer
values
Doubly Ended queue(Deque)
• It allows insertion and removal of elements from
both the ends, i.e , front and back.
Applications of Queue
• In operating systems like Semaphores,FCFS, Buffer for
devices
• CPU task scheduling
• In networks like mail queues, queues in routers/switches

Real time application


• call center phone system to hold the calls

You might also like