0% found this document useful (0 votes)
70 views35 pages

Unit 3 - Stack and Queues

Uploaded by

Ilesh Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views35 pages

Unit 3 - Stack and Queues

Uploaded by

Ilesh Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

UNIT:3

Stack & Queue


1
SYLLABUS

3.1 Linear and Non-Linear Data Structures


3.2 Stack : Array representation of Stack, PUSH POP Operations
on Stack, Implementation of Stack, Application of Stack, Infix,
Prefix and Postfix Forms of Expressions, Recursive Functions
(factorial, greatest common divisor, Fibonacci series)
3.3 Queue: Array representation of Queue, Operations on Queue,
Implementation of Queue, Limitation of Single Queue
3.4 Concepts of Circular Queue
3.5 Application of queue
3.6 Difference circular queue and simple queue

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.

 Examples of the linear data structure are:

 (A) Array (B) Stack (C) Queue (D) Linked List

 Non Linear data structure:


 In the Non linier data structure processing of data items is not
possible in linier fashion.
 Examples of the non linier data structure are:

 (A)Tree (B) Graph 3


3.2 STACK
 A stack is a linear OR non-primitive list in which insertion and
deletion operations are performed at only one end of the list.
 A stack is Last in First out (LIFO) Structure.

 A stack is a linear data structure in which elements are added


and remove from one end, called top of stack.
 For example of stack is, consider a stack of plates on the
counter in cafeteria, during the time of dinner, customer take
plates from top of the stack and waiter puts the washed plates
on the top of the stack. So new plates are put on the top and old
one yet bottom.
 Another example is Railway Shunting System.

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.

 Algorithm: PUSH(S, TOP, VAL)


 This algorithm insert an element X to the top of the stack.

 The Stack is represented by vector S which contains N elements.

 TOP is a pointer which points to the top element of the Stack.


9
Step-1: [Check for stack overflow]
If (TOP >= N) then
Write (‘Stack Overflow’)
Return
Step-2: [Increment TOP]
TOP = TOP + 1
Step-3: [Insert Element]
S [TOP] = VAL
Step-4: [Finished]
Return

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.

 If the stack is empty, and we try to pop an element, then error


occur. It is called “Stack under Flow” error.

 Algorithm: POP(S, TOP)


 This algorithm removes an element from the Top of the Stack.

 The Stack is represented by vector S which contains N


elements.
 TOP is a pointer which points to the top element of the Stack.
11
Step-1: [Check for the Underflow on the Stack]
If TOP = 0 then
Write (‘STACK UNDERFLOW’)
Exit
Step-2:[Decrement Pointer]
TOP = TOP - 1
STEP-3:[Return former top element of the
stack]
Return(S [TOP + 1])

12
INFIX, PREFIX AND POSTFIX FORMS OF
EXPRESSIONS
 There are basically three types of polish notation:
 Infix (B) Prefix (C) Postfix

 When the operator(+) exists between two operands(A & B) then


it is known as Infix notation.(e.g. A+B)
 When the operator(+) are written before their operands(A & B)
then it is known as Prefix notation(Polish notation). (e.g. +AB)
 When the operator(+) are written after their operands(A & B)
then it is known as Postfix notation(Reverse polish notation).
(e.g. AB+).
 Stack is widely used to convert infix notation into prefix or
postfix notation.
13
Rules for converting infix notation to prefix/ postfix
 The operations with heights precedence are converted first and
then after a portion of the expression have been converted to
postfix.
 It is to be treated as single operand.

 Take only two operands at a time to convert in a postfix from


like A+B AB+
 Always, convert the parenthesis first

 Convert postfix exponentiation second if there are more than


one exponentiation in sequence then take order from right to
left.
 Convert in to postfix as multiplication and division operator,
left to right.
 Convert in postfix as addition and subtraction left to right.
14
 Convert infix into Prefix expression.

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+

4. A*B/(C+D-E) 5. (B*C/D)/(D/C+E) 6. A*B+C*D+E*F


=A*B/([CD+]-E)
=A*B/[CD+E-] = ([BC*]/D)/(DC/+E) =[AB*]+C*D+E*F
=[AB*]/[CD+E-] =[AB*]+[CD*]+E*F
= [BC*D/]/[DC/E+] =[AB*]+[CD*]+[EF*]
=AB*CD+E-/
=BC*D/DC/E+/ =[AB*CD*+]+[EF*]
=AB*CD*+EF*+

16
APPLICATION OF STACK
 There are three main application of stack:
(1) Recursion:
 Recursion means function call itself.

 It is the technique of defining a process in terms of itself.

 Stack is widely used in recursion because of its Last in First out


Property.
 In terms of C language the function calling itself is known
as recursion.
(2)Evaluate polish notation:
 The process of writing the operators of an expression
either before their operands or after operands are
called the polish notation. 17
 There are basically three types of polish notation:
 Infix (B) Prefix (C) Postfix

(3) Stack Machine:


 Stack Machines are also used to evaluate polish notations.

 Two example of the stack machine are PDP-11 and B5000.


These machines are well suited for stacking local variables
and parameter that arise in the function call.
 Stack machines provides faster execution of polish notation.

18
RECURSIVE FUNCTIONS (FACTORIAL, GREATEST
COMMON DIVISOR, FIBONACCI SERIES)

 Recursion means a function call itself.


 Example: The factorial function can be recursively defined as,

 Here, FACTORIAL(N) is defined in terms of FACTORIAL(N-1)


which is again defined in terms of FACTORIAL(N-2) and this
process continue until FACTORIAL(0) is reached.
19
 The value of FACTORIAL(0) is defined as 1.
 ToGenerate the Fibonacci number using
recursion:
 A fibonacci series is,
0 1 1 2 3 5 8 ………………. N
 If we consider F0 = 0 and F1 = 1 then F2 = F0 + F1 = 0 + 1
= 1, similar F3 = F2 + F1 = 1 + 1 = 2, and F4 = F3 + F2 = 2
+ 1 = 3 and so on.
 It is clear that each succeeding(next) term is the sum of two
preceding(previous) terms, doing this procedure until i
becomes less then the value of n.

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

 A Queueis a list and list can be implemented


by two ways:
1. Pointer(linked List/ Dynamic Implementation)
2. Array(Static Implementation)

24
OPERATIONS ON QUEUE
1. QINSERT (Q, Front, Rear, N, Val)
 This function insert an element into the queue

 The Queue is represented by vector Q which contains N


elements.
 Front is a pointer which points to the front end

 Rear is a pointer which points to the rear end

 Y is the element to be inserted.

 ALGORITHM:QINSERT

STEP-1: [Check Overflow error?]


 If Rear≥N then

 Write (‘Queue Overflow’)


25
 Exit
Step-2: [Increment rear pointer]
RearRear+1
Step-3: [Insert element]
Q[Rear] Val
Step-4:[Is front pointer properly set?]
If Front=0 then
Front1
Return
Step-5: [Finished]
Exit

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

 Rear is a pointer which points to the rear end

Step-1: [Underflow?]
If Front = 0 then
Write (‘Queue Underflow’)
Exit
Step-2:[Delete element]
XQ [F]
27
Step-3:[Queue empty?]
If Front =Rear
Then FrontRear0
Else FrontFront+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:

 As shown in figure we insert three elements 5, 10, 15 in


simple queue. After that we delete 5 and 10 as shown in figure.
Now even we have a free memory space we cannot use that
memory space. So simple queue results in wastage of memory
space. This problem can be solved by using circular queue.
29
 For example consider the following operations:
 As shown in 1st figure, we insert eight elements 10,
20,30,40,50,60,70,80 in simple queue. After that we delete
10, 20 and 30 as shown in 2nd figure. Now we have a free
memory space in circular queue and we can use that memory
space by incrementing rear pointer by 1(rear=0).

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.

There is wastage of Memory There is no wastage of Memory


space. space.
Example of Simple queue Example of Circular queue

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.

You might also like