Unit - II - Stack and Queue Stack
Unit - II - Stack and Queue Stack
Queues:
➢ Introduction
➢ Types of queues: Simple queues, Circular queues, Double ended queues,
Priority Queue
➢ Applications of Queue
➢ Operations of Simple Queue: Insert and Delete
----------------------------------------------------------------------------------------------------
Stack
Definition: A stack is an ordered collection of homogeneous data elements where the insertion
and deletion operation take place at one end only.
Characteristics of a Stack:
A stack data structure has following characteristics:
• It is non-primitive linear data structure.
• Its nature is LIFO (Last In First Out).
• The insertion and deletion operation occur only at one end.
• It is linear data structure of variable size.
• The elements are removed from the opposite order from that in which they are added to the
stack.
Operations on Stack:
Operation Description
1 Push • An insertion operation is known as Push.
Top of a stack:
• The most accessible element of a stack is known as Top of a stack.
• The pointer at the top most position is called TOP.
• It is the most commonly accessible element of the stack. Insertion and deletion occur at
top.
Bottom of a stack
• The least accessible element of a stack is known as Bottom of a stack.
• The pointer at the last position is called BOTTOM.
Algorithms
[1]. An algorithm to insert an element into a stack.
PUSH (S, TOP, X, N): This procedure inserts an element X to the top of a stack which is
represented by a vector S containing N elements with a pointer Top denoting the position of the
top element in the stack.
Step 1: [ Check for Stack Overflow ]
If ( TOP >= N )
Then
Write (‘STACK OVERFLOW’)
Return
Step 2: [ Increment Top ]
TOP TOP + 1
Step 3: [ Insert an Element ]
S[ TOP ] X
Step 4: [ Finished ]
Return
[2]. An algorithm to delete an element from a stack.
POP (S, TOP): This function removes the top element from a stack which is represented by a
vector S and returns this element. Top is a pointer to the top element of the stack.
Step 1: [ Check for Underflow on Stack]
If ( TOP = 0 )
Then
Write (‘STACK UNDERFLOW ON POP’)
Take action in response to underflow
Exit
Step 2: [ Decrement pointer ]
TOP TOP - 1
Step 3: [ Return top Element of stack ]
Return ( S[ TOP + 1] )
Prefix
• A notation in which the operator is written before the operands is called Prefix notation.
• It is also called polish notation in the honor of the mathematician Jan Lukasiewicz who
developed this notation.
• Example: + AB
• As the operator '+' is written before the operands A and B, this notation is called prefix (pre
means before).
Postfix
• In the postfix notation the operators are written after the operands, so it is called the postfix
notation (post means after),
• It is also known as suffix notation or reverse polish notation.
• Example: AB +
• The prefix and postfix notations are not really as efficient to use as they might look at first.
• For example, a C function to return the sum of two variables A and B (passed as argument)
is called or invoked by the instruction: add (A, B)
• Note the operator add (name of the function) precedes the operands A and B.
• Because the postfix notation is a type of notation which is most suitable for a computer to
calculate any expression (due to its reversing characteristic), and is the universally accepted
notation for designing arithmetic and logical (ALU) unit of the CPU (processor). Therefore
it is necessary for us to study the postfix notation.
• Moreover the postfix notation is the way computer looks towards any arithmetic
expression, any expression entered into the computer is first converted into postfix
notation, stored in stack and then calculated.
Conversion: Infix to Postfix using manually and stack for parenthesis and
Non-parenthesis
Operator precedence
Exponential operator ^ Highest precedence
A+ (B * C) Parenthesized expression
A + (BC+ DE + F* + G /)
T–C
AB + C - Postfix Form
(A * B) + C / D
(AB *) + C / D
(T) + C / D T = AB *
(T) + (C / D)
(T) + (CD /)
T+S S = CD /
TS +
AB * CD / Postfix expression
(AB *) + C
(T) + C
TC +
AB * C+ Postfix expression
A + (BC / ) – D
A+T–D T = BC /
(A + T) – D
(AT +) – D
S–D S = AT +
SD -
AT + D -
(AB+) / (CD-)
TS /
T*C/D T = (AB+)
(T * C) / D
(TC *) / D
S/D S = (TC*)
SD /
TC * D /
AB + C * D / Postfix expression
T * C / D + ( EF ^) / G
T*C/D+S/G S = (EF ^)
(T * C) / D + S / G
(TC *) / D + S / G
Q/D+S/G Q = (TC *)
(Q / D) + S / G
(QD /) + S / G
P+S/G P = (QD /)
P + (S / G)
P + (SG / )
P+O O = (SG /)
QD / O +
TC * D / O +
AB + C * D / S G / +
AB + C * D / EF ^ G / + Postfix expression
A + [ T + (S * F) ] / G
A + [ T + (SF*) ] / G
A + (TQ+) / G
A + (PG /)
A PG/ +
A TQ+ G/ +
A BC+ Q+ G/ +
A BC+ SF*+ G/ +
A+(B*C-(D/T)*G)*H T = EF ^
A + ( B * C - ( DT/ ) * G ) * H
A+(B*C-S*G)*H S = DT /
A + ( (B * C) - S * G ) *H
A + ( (BC *) – S * G ) * H
A+(Q–S*G)*H Q = (BC *)
A + ( Q – (S * G) ) * H
A + ( Q – (SG *) ) * H
A+(Q–P)*H P = (SG *)
A + ( QP - ) * H
A+O*H O = (QP - )
A+(O*H)
A + (OH *)
A+N N = (OH *)
A OH * +
A QP – H * +
A BC * P – H * +
A BC* SG * - H * +
A BC* DT/ G * - H * +
A – B / (C * (DE ^ ))
A – B / (C * T) T = (DE ^)
A – B / (CT *)
A–B/S S = (CT *)
A–(B/S)
A – ( BS / )
A–Q Q = (BS /)
A BS/ -
A B CT* / -
Queue:
In ordinary English, a queue is defined as a waiting line, like a line of people waiting to purchase
tickets, where the first person in line is the first person served.
Real Life Examples:
1) A queue is a waiting line – seen in daily life
2) A line of people waiting for a bank teller
3) A line of cars at a toll booth
4) Patients waiting outside the doctor's clinic
• A queue is logically a First-In First-Out (FIFO) type of list.
• A queue is a non-primitive linear data structure.
• Definition: Queue is an ordered collection of homogeneous elements in which new elements
are added at one end called the REAR end, and the existing elements are deleted from
another end called the FRONT end.
• This linear data structure represents a linear list and permits deletion to be performed at one
end of the list and the insertion at the other.
• The information in such a list is processed in the same order as it was received.
Types of Queue
1) Simple Queue
2) Circular Queue
3) Priority Queue
4) Dequeue (Double Ended Queue)
Simple Queue
• Simple queue defines the simple operation of queue in which insertion occurs at the rear
(Tail) of the list and deletion occurs at the front (Head) of the list.
Step 1: [Underflow?]
If F = 0
then Write (‘UNDERFLOW’)
Return(0) (0 denotes an empty queue)
Step 2: [Delete element]
Y Q[F]
Step 3:[Queue empty?]
If F = R
then FR0
else F F +1
Step 4: [Return element]
Return(Y)
• When we remove an element from the queue, we are actually moving the front of the queue
forward, thereby reducing the overall size of the queue.
• And we cannot insert new elements, because the rear pointer is still at the end of the queue.
• The only way is to reset the linear queue, for a fresh start.
• This is the major limitation of a classical / simple queue, i.e. even if there is space available
at the front of the queue we cannot use it.
Circular Queue
• The basic limitation of simple queue can be solved by using 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.
We can represent this circular queue as:
Fig. 1
1) Front will always be pointing to the first element (as in the linear queue)
2) An output-restricted deque is one where insertion can be made at both ends, but
deletion can be made from one end only.
• Since both insertion and deletion are performed from either end, it is necessary to design an
algorithm to perform the following four operations:
1) Insertion of an element at the Rear end of the queue
2) Deletion of an element From the Front end of the queue
3) Insertion of an element at the Front end of the queue
4) Deletion of an element from the Rear end of the queue
• For an input-restricted deque only the operations specified in 1, 2, 3 and 4 are valid.
• For an output-restricted deque only the operations specified in 1, 2 and 3 are valid.
Priority Queue
• Priority Queue is more specialized data structure than Queue.
• Like ordinary queue, priority queue has same method but with a major difference.
• In Priority queue items are ordered by key value so that item with the lowest value of key is
at front and item with the highest value of key is at rear or vice versa.
• So we're assigned priority to item based on its key value.
• Lower the value, higher the priority.
• Priority Queue is an extension of queue with following properties.
A. Every item has a priority associated with it.
B. An element with high priority is dequeued before an element with low priority.
C. If two elements have the same priority, they are served according to their order in the
queue.
• Priority Queue is an extension of queue with following properties.
A. Every item has a priority associated with it.
B. An element with high priority is dequeued before an element with low priority.
C. If two elements have the same priority, they are served according to their order in the
queue.
• Deletion: Deletion requires a search for the element of highest priority and deletes the
element with highest priority.
• The following methods can be used for deletion/removal from a given Priority Queue:
A. An empty indicator replaces deleted elements
B. After each deletion, elements can be moved up in the array decrementing the Rear
C. The array in the queue can be maintained as an ordered circular array
Applications of Queues
1) Round robin technique for processor scheduling is implemented using queues.
2) All types of customer services (like railway ticket reservation) center software are
designed using queues to store and service customers information.
3) Printer server routines are designed using queues. A number of users share a printer using
printer server