Unit 3 - Stack and Queues
Unit 3 - Stack and Queues
2
3.1 LINEAR AND NON-LINEAR DATA
STRUCTURES
Linear Data Structure:
In the linear data structure processing of data items is possible
in linear fashion.
Data are processed one by one sequentially.
4
ARRAY REPRESENTATION OF STACK
1.Verticaly Representation of stack
5
The operation in a stack is representation by using vector
consisting of number of elements.
A pointer top keeps of the top elements ,when stack is empty
to phas a value 0.
Every time a new element is added at top,top is incremented
by one.
When an element is deleted from a top,top is decremented by
one.
2.Horizontal Representation of stack
6
IMPLEMENTATION OF STACK
Implementation of Stack:
A stack is a list and list can be implemented by
two ways:
1. Pointer(linked List/ Dynamic Implementation)
2. Array(Static Implementation)
7
OPERATIONS ON STACK
1. Push Operation:
The operations that add an element to the top
of the stack is called PUSH operation. It is
used to insert an element into the stack.
2. Pop Operation:
The operation that delete top element from
the top of stack is called POP. it is used to
delete an element from stack
8
PUSH POP OPERATIONS ON STACK
1. PUSH Operation
In push operation, we can add elements to the top of the stack,
so before push operation, user must check the stack, it should
not be a full.
If stack is already full and when we try to add the elements then
error occurs. It is called “Stack Over Flow” error.
10
2. POP Operation
In POP operation, we can delete or remove an elements from
top of the stack, so before pop operation, user must check the
stack, stack should not be a empty.
12
INFIX, PREFIX AND POSTFIX FORMS OF
EXPRESSIONS
There are basically three types of polish notation:
Infix (B) Prefix (C) Postfix
1. (A + B) * C 2. (A + B ) * (C + D) 3. (A-B)+C*A+B
= (+ A B) * C = (+ A B) * (+ C D) =-AB+C*A+B
= * (+ A B) C =*(+ A B) (+ C D) =-AB+*CA+B
= * +AB C =* + AB + CD =+-AB*CA+B
=++-AB*CAB
4. (B*C/D)/(D/C+E) 5. A*B/(C+D-E)
=(*BC/D)/(D/C+E) =A*B/(+CD-E)
=(/*BCD)/(D/C+E) =A*B/(-+CDE)
=(/*BCD)/(/DC+E) =*AB/-+CDE
=(/*BCD)/(+/DCE) =/*AB-+CDE
=//*BCD+/DCE 15
Convert infix into Prefix expression.
1. A * B / C +D 2. C*B*A/D 3. (A-B)+C*A+B
= [AB*]/C+D = [CB*]*A/D = [AB-] +C*A+B
= [AB*C/] +D = [CB*A*]/D = [AB-] + [CA*] +B
=AB*C/D+ =CB*A*D/ = [AB-CA*+] +B
=AB-CA*+B+
16
APPLICATION OF STACK
There are three main application of stack:
(1) Recursion:
Recursion means function call itself.
18
RECURSIVE FUNCTIONS (FACTORIAL, GREATEST
COMMON DIVISOR, FIBONACCI SERIES)
20
Greatest Common Divisor (GCD):
To implement Greatest Common Divisor(GCD) function in
a recursive form using following steps:
If m <= n and n % m = 0 then gcd(n, m) = m(Termination
step).
If n < m then gcd(n, m) = gcd(m, n) ( Recursive definition of
gcd)
If n >= m then gcd(n, m) = gcd(m, n % m) (Recursive
definition of gcd)
21
3.3 QUEUE
A queue is a linear list in which insertion is performed at one
end called rear end and deletion is performed at another end of
the list called front end.
The information in such a list is proceeds in the same order as it
was received.
Since insertion is performed at one end and deletion is
performed at another end the element which is inserted first is
first to delete. So it is also known as First in First out (FIFO)
or First Come First Serve (FCFS) data structure.
22
ARRAY REPRESENTATION OF QUEUE
A queue has two pointer, from pointer and rear pointer
which are pointing to front and rear element of the
queue.
23
IMPLEMENTATION OF QUEUE
24
OPERATIONS ON QUEUE
1. QINSERT (Q, Front, Rear, N, Val)
This function insert an element into the queue
ALGORITHM:QINSERT
26
2. Algorithm to delete an element from the queue
QDELETE (Q, Front, Rear)
The Queue is represented by vector Q which contains N
elements.
Front is a pointer which points to the front end
Step-1: [Underflow?]
If Front = 0 then
Write (‘Queue Underflow’)
Exit
Step-2:[Delete element]
XQ [F]
27
Step-3:[Queue empty?]
If Front =Rear
Then FrontRear0
Else FrontFront+1
Step-4:[Return element]
Return (X)
28
LIMITATION OF SINGLE QUEUE
Disadvantage of simple queue is that even if we have a free
memory space in a queue we cannot use that free memory
space to insert element.
For example consider following operations:
30
3.4 CONCEPTS OF CIRCULAR QUEUE
A circular queue is a queue in which elements are
arranged such that the first element in the queue follows
the last element in the queue.
The arrangement of circular queue is shown in figure
below:
31
Here in the circular queue the first element q[0]
follows the last element q[n-1].
Disadvantage of simple queue is that even if
we have a free memory space in a queue we
cannot use that free memory space to insert
element.
32
3.5 APPLICATION OF QUEUE
A queue is the natural data structure for a system to serve its
incoming requests. Most of the process scheduling or disk
scheduling algorithms in operating systems use queues.
Computer hardware like a processor or a network card also
maintain buffers in the form of queues for incoming resource
requests. A stack-like data structure causes starvation of the first
requests, and is not applicable in such cases.
A mailbox or port to save messages to communicate between
two users or processes in a system is essentially a queue-like
structure.
Queue is widely used in Simulation.
33
3.6 DIFFERENCE OF CIRCULAR QUEUE AND
SIMPLE QUEUE
Simple Queue Circular Queue
The last element of the queue The last element in the queue
does not follow the first element. follow the first element.
Problem of Overflow in a simple Problem of Overflow in a circular
queue is frequently occurs. queue infrequently occurs.
34
COMPARISON OF LIFO AND FIFO
LIFO FIFO
(1) In LIFO the insertion and (1) In FIFO the insertion and
deletion operation are deletion operation are performed
performed at only one end. at two different ends.
(2) In LIFO the element which (2) In FIFO the element which is
is inserted last is first to delete. inserted first is first to delete.
(3) LIFO require only one (3) FIFO requires two pointers
pointer called TOP called front and rear.
(4) Example: piles of trays in (4) Example: students at
cafeteria registration counter
(5) In LIFO there is no wastage (5) In FIFO even if we have free
of memory space. memory space sometimes we
35
cannot use that space to store
elements.