StacksAndQueues in Data Structures
StacksAndQueues in Data Structures
UNIT - II
Stacks: Definitions, Operations and Applications, Array and Linked Representation of Stacks.
Stacks
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 single
position which is known as "top". That means, new element is added at top of the stack and an
element is removed from the top of the stack. In stack, the insertion and deletion operations are
performed based on LIFO (Last In First Out) principle.
Stack data structure can be implement in two ways. They are as follows...
1. Using Array
2. Using Linked List
When stack is implemented using array, that stack can organize only limited number of
elements. When stack is implemented using linked list, that stack can organize unlimited number
of elements.
Array Implementation:
This implementation is very simple, just define a one dimensional array of specific size
and insert or delete the values into that array by using LIFO principle with the help of a variable
'top'. Initially top is set to -1. Whenever we want to insert a value into the stack, increment the
top value by one and then insert. Whenever we want to delete a value from the stack, then delete
the top value and decrement the top value by one
Push Operation
PUSH (STACK,TOP,MAXSTK,ITEM)
This procedure pushes an ITEM onto a stack
1. If TOP=MAXSTK, then print OVERFLOW and return.
2. Set TOP:=TOP+1.
3. Set STACK[TOP]:=ITEM.
Return
Pop Operation
POP(STACK,TOP,ITEM)
1. If TOP=0, then print underflow and return.
2. Set ITEM:=STACK[TOP].
3. Set TOP:=TOP-1
4. Return.
The major problem with the stack implemented using array is, it works only for
fixed number of data values. That means the amount of data must be specified at the
beginning of the implementation itself. Stack implemented using array is not suitable,
when we don't know the size of data which we are going to use. A stack data structure
can be implemented by using linked list data structure. The stack implemented using
linked list can work for unlimited number of values. That means stack implemented using
linked list works for variable size of data. So, there is no need to fix the size at the
beginning of the implementation. The Stack implemented using linked list can organize
as many data values as we want.
Refer to Insert at the beginning to push the elements into the stack and delete at
the beginning to pop inorder implement stacks using Linked list.
Applications of Stacks.
Expressions
What is an Expression?
In any programming language, if we want to perform any calculation or to frame a
condition etc., we use a set of symbols to perform the task. These set of symbols makes
an expression.
Postfix Expression
In postfix expression, operator is used after operands. We can say that "Operator follows
the Operands". The general structure of Postfix expression is as follows...
Operand1 Operand2 Operator
Example
Prefix Expression
In prefix expression, operator is used before operands. We can say that "Operands
follows the Operator". The general structure of Prefix expression is as follows...
Operator Operand1 Operand2
Example
1. Expression Conversion
Any expression can be represented using three types of expressions (Infix, Postfix
and Prefix). We can also convert one type of expression to another type of expression like
Infix to Postfix, Infix to Prefix, Postfix to Prefix and vice
versa.
To convert any Infix expression into Postfix or Prefix expression we can use the
following procedure...
1. Find all the operators in the given Infix Expression.
2. Find the order of operators evaluated according to their Operator precedence.
3. Convert each operator into required type of expression (Postfix or Prefix) in the same
order.
Algorithm
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
Let's see an example to better understand the algorithm:
Expression: 456*+
3. Tower of Hanoi
Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The
objective of the puzzle is to move the entire stack to another rod, obeying the following simple
rules:
1) Only one disk can be moved at a time.
2) Each move consists of taking the upper disk from one of the stacks and placing it on top of
another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
3) No disk may be placed on top of a smaller disk.
Tower of Hanoi can be implemented with the help of excessive recursion using Stacks.
Queues
Queue is an abstract data type or a linear data structure, just like stack data structure,
in which the first element is inserted from one end called the REAR(also called tail), and the
removal of existing element takes place from the other end called as FRONT(also called head).
This makes queue as FIFO(First in First Out) data structure, which means that element inserted
first will be removed first.
Operations on Queues
Representation of Queue
Array Representation
Linked Representation
QINSERT(QUEUE,N,REAR,FRONT,ITEM)
The major problem with the queue implemented using array is, it will work for only fixed
number of data. That means, the amount of data must be specified in the beginning itself. Queue
using array is not suitable when we don't know the size of data which we are going to use. A
queue data structure can be implemented using linked list data structure. The queue which is
implemented using linked list can work for unlimited number of values. That means, queue
using linked list can work for variable size of data (No need to fix the size at beginning of the
implementation). The Queue implemented using linked list can organize as many data values as
we want.
In linked list implementation of a queue, the last inserted node is always pointed by 'rear'
and the first node is always pointed by 'front'.
Example
In above example, the last inserted node is 50 and it is pointed by 'rear' and the first inserted
node is 10 and it is
pointed by 'front'. The order of elements inserted is 10, 15, 22 and 50.
Note : Use the Single linked list, Insertatend and deleteAtBeg to implement Queue
Variations of Queues(types)
Priority Queue
Double Ended Queue (DeQueue)
Circular Queue
Priority Queue
A priority queue is a collection of elements such that each element has been assigned a
priority. An element with a higher priority will be processed before the other elements.
If the elements have the same priority then the elements are processed in the order in which they
were added into the queue.
Insertion : Elements will inserted in the sorted format.
Circular Queue
In a normal Queue Data Structure, we can insert elements until queue becomes full. But once if
queue becomes full, we cannot insert the next element until all the elements are deleted from the
queue. For example consider the queue below...
After inserting all the elements into the queue.
Now consider the following situation after deleting three elements from the queue.
This situation also says that Queue is full and we cannot insert the new element because, 'rear'
is still at last position. In above situation, even though we have empty positions in the queue we
cannot make use of them to insert new element. This is the major problem in normal queue data
structure. To overcome this problem we use circular queue data structure.
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.
Graphical representation of a circular queue is as follows...
Implementation
Insertion
1. If the „rear‟ points to the end of the queue then goto step 2, else go to Step 3.
2. Let rear value=‟0‟
3. Increase the rear value by 1
4. If the front and the „rear pointers point at the same place in the queue and that place has
a not NULL value, then “queue overflow” , go to step 6, else go to step 5
5. The new value is inserted in the position the rear pointer points to.
6. Exit.
In a normal Queue, we can insert elements until queue becomes full. But once queue
becomes full, we can not insert the next element even if there is a space in front of queue.
The space in the front even if it is not in use is not being reused.
Circular Queue overcomes this advantage by providing the reuse of the space once rear
reaches the end point and if there is a space in the front.