Chapter 5 ALGO
Chapter 5 ALGO
Chapter Five
Stack and Queues
A stack is one of the most important and useful non-primitive linear data structure in computer
science. It is an ordered collection of items into which new data items may be added /inserted
and from which items may be deleted at only one end, called the top of the stack. As all the
addition and deletion in a stack is done from the top of the stack, the last added element will be
first removed from the stack. That is why the stack is also called Last-in-First-out (LIFO). Note
that the most frequently accessible element in the stack is the top most elements, whereas the
least accessible element is the bottom of the stack.
The operation of the stack can be illustrated as in Fig.
The insertion (or addition) operation is referred to as push, and the deletion (or remove)
operation as pop. A stack is said to be empty or underflow, if the stack contains no elements. At
this point the top of the stack is present at the bottom of the stack. And it is overflow when the
stack becomes full, i.e., no other elements can be pushed onto the stack. At this point the top
pointer is at the highest location of the stack.
1
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science
Pop: The process of deleting (or removing) an element from the top of stack is called Pop
operation. After every pop operation the stack is decremented by one. If there is no element in
the stack and the pop operation is performed then the stack underflow condition occurs.
Stack Implementation
Implementation of stack using arrays is a very simple technique. Algorithm for pushing (or add
or insert) a new element at the top of the stack and popping (or delete) an element from the stack
is given below.
2
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science
Expression
Another application of stack is calculation of postfix expression. There are basically three types
of notation for an expression (mathematical expression; an expression is defined as the number
of operands or data items combined with several operators.)
1. Infix notation
2. Prefix notation
3. Postfix notation
The infix notation is what we come across in our general mathematics, where the operator is
written in-between the operands. For example: The expression to add two numbers A and B is
written in infix notation as:
A+B
Note that the operator ‘+’ is written in between the operands A and B.
The prefix notation is a notation in which the operator is written before the operands, it is also
called polish notation. The same expression when written in prefix notation looks like:
+AB
As the operator ‘+’ is written before the operands A and B, this notation is called prefix (pre
means before).
In the postfix notation the operator(s) 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. The
above expression if written in postfix expression looks like:
AB+
Operator Precedence
Algorithm
Suppose P is an arithmetic expression written in infix notation. This algorithm finds the
equivalent postfix expression Q. Besides operands and operators, P (infix notation) may also
contain left and right parentheses. We assume that the operators in P consists of only exponential
( ^ ), multiplication ( * ), division ( / ), addition ( + ) and subtraction ( - ).
The algorithm uses a stack to temporarily hold the operators and left parentheses. The postfix
expression Q will be constructed from left to right using the operands from P and operators,
which are removed from stack. We begin by pushing a left parenthesis onto stack and adding a
right parenthesis at the end of P. the algorithm is completed when the stack is empty.
1. Push “(” onto stack, and add“)” to the end of P.
2. Scan P from left to right and repeat Steps 3 to 6 for each element of P until the stack is
empty.
3. If an operand is encountered, add it to Q.
4. If a left parenthesis is encountered, push it onto stack.
5. If an operator ⊗is encountered, then:
(a) Repeatedly pop from stack and add P each operator (on the top of stack), which has
the same precedence as, or higher precedence than ⊗.
(b) Add ⊗to stack.
6. If a right parenthesis is encountered, then:
(a) Repeatedly pop from stack and add to P (on the top of stack until a left parenthesis is
encountered.
(b) Remove the left parenthesis. [Do not add the left parenthesis to P.]
4
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science
7. Exit.
Note. Special character ⊗is used to symbolize any operator in P.
Consider the following arithmetic infix expression P
P = A + (B / C - (D * E ^ F) + G) * H
Fig. shows the character (operator, operand or parenthesis) scanned, status of the stack and
postfix expression Q of the infix expression P.
Queues
A queue is logically a first in first out (FIFO or first come first serve) linear data structure. The
concept of queue can be understood by our real life problems. For example a customer come and join
in a queue to take the train ticket at the end (rear) and the ticket is issued from the front end of queue.
That is, the customer who arrived first will receive the ticket first. It means the customers are
serviced in the order in which they arrive at the service centre.
It is a homogeneous collection of elements in which new elements are added at one end called rear,
and the existing elements are deleted from other end called front.
The basic operations that can be performed on queue are
1. Insert (or add) an element to the queue (enqueue).
2. Delete (or remove) an element from a queue (dequeue)
Push operation will insert (or add) an element to queue, at the rear end, by incrementing the array
index. Pop operation will delete (or remove) from the front end by decrementing the array index and
will assign the deleted value to a variable. Following figure will illustrate the basic operations on
queue.
Queue Implementation
Queue can be implemented in two ways:
6
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science
CIRCULAR QUEUE
In circular queues the elements Q[0],Q[1],Q[2] .... Q[n – 1] is represented in a circular fashion
with Q[1] following Q[n]. A circular queue is one in which the insertion of a new element is
done at the very first location of the queue if the last location at the queue is full.
Suppose Q is a queue array of 6 elements. Push and pop operation can be performed on circular.
The following figures will illustrate the same.
7
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science
After inserting an element at last location Q[5], the next element will be inserted at the very first
location (i.e., Q[0]) that is circular queue is one in which the first element comes just after the
last element.
At any time the position of the element to be inserted will be calculated by the relation Rear =
(Rear + 1) % SIZE
After deleting an element from circular queue the position of the front end is calculated by the
relation Front= (Front + 1) % SIZE
After locating the position of the new element to be inserted, rear, compare it with front. If (rear
= front), the queue is full and cannot be inserted anymore.
Algorithms
Let Q be the array of some specified size say SIZE. FRONT and REAR are two pointers where
the elements are deleted and inserted at two ends of the circular queue. DATA is the element to
be inserted.
Inserting an element to circular Queue
1. Initialize FRONT = – 1; REAR = 1
2. REAR = (REAR + 1) % SIZE
3. If (FRONT is equal to REAR)
(a) Display “Queue is full”
(b) Exit
4. Else
(a) Input the value to be inserted and assign to variable “DATA”
5. If (FRONT is equal to – 1)
(a) FRONT = 0
(b) REAR = 0
6. Q[REAR] = DATA
7. Repeat steps 2 to 5 if we want to insert more elements
8. Exit
8
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science
There are two types of deque depending upon the restriction to perform insertion or deletion
operations at the two ends. They are
1. Input restricted deque
2. Output restricted deque
An input restricted deque is a deque, which allows insertion at only 1 end, rear end, but allows
deletion at both end, rear and front end of the lists.
An output-restricted deque is a deque, which allows deletion at only one end, front end, but
allows insertion at both ends, rear and front ends, of the lists.
The possible operation performed on deque is
1. Add an element at the rear end
2. Add an element at the front end
3. Delete an element from the front end
4. Delete an element from the rear end
Only 1st, 3rd and 4th operations are performed by input-restricted deque and 1st, 2nd and 3rd
operations are performed by output-restricted deque.
9
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science
(a) left = – 1
(b) right = – 1
4. Else
(a) if(right == 0)
(i) right = MAX-1
(b) else
(i) right = right-1
5. Exit
Delete an Element from the Left Side of the De-Queue
1. If (left == – 1)
(a) Display “Queue Underflow”
(b) Exit
2. DATA = Q [left]
3. If(left == right)
(a) left = – 1
(b) right = – 1
4. Else
(a) if (left == MAX-1)
(i) left = 0
(b) Else
(i) left = left +1
5. Exit
11
Complied By Mr. Abraham W