KEMBAR78
Implementation of stacks and queues in C | PPTX
Stacks and Queues
Stack
 Stores a set of elements in a particular order
 Stack principle: LAST IN FIRST OUT = LIFO
 It means: the last element inserted is the first one
to be removed
 Example
Last In First Out
B
A
D
C
B
A
C
B
A
D
C
B
A
E
D
C
B
A
top
top
top
top
top
A
Stack Operations
 The last item added is pushed (added) to
the stack.
 The last item added can be popped
(removed) from the stack.
 The last item added can be topped
(accessed) from the stack.
 These operations all take constant time:
O(1).
A typical stack interface:
void push(Thing newThing);
void pop();
Thing top();
Basic Alg for Stack Push & Pop
 PUSH(STACK,TOP,MAXSTK,ITEM)
1. If TOP=MAXSTK, then
print OVERFLOW, and
Return
2. Set TOP = TOP +1
3. Set STACK[TOP] = ITEM
4. Return
 POP(STACK,TOP,MAXSTK,ITEM)
1. If TOP=0, then print
UNDERFLOW, and
Return
2. ITEM = Set STACK[TOP]
3. Set TOP = TOP - 1
4. Return
Stack Applications
 String Conversion
 Converting Infix to Postfix
 Evaluation of Expression
 Towers of Hanoi
 Balancing symbols
Infix to postfix
 Algorithm
1) Examine the next element in the input.
2) If it is an operand, output it.
3) If it is opening parenthesis, push it on stack.
4) If it is an operator, then
i) If stack is empty, push operator on stack.
ii) If the top of the stack is opening parenthesis,
push operator on stack.
 iii) If it has higher priority than the top of stack,
push operator on stack.
iv) Else pop the operator from the stack and
output it, repeat step 4.
5) If it is a closing parenthesis, pop operators
from the stack and output them until an opening
parenthesis is encountered. pop and discard the
opening parenthesis.
6) If there is more input go to step 1
7) If there is no more input, unstack the
remaining operators to output.
infixVect
postfixVect
( a + b - c ) * d – ( e + f )
Infix to postfix conversion
infixVect
postfixVect
a + b - c ) * d – ( e + f )
(
stackVect
Infix to postfix conversion
infixVect
postfixVect
+ b - c ) * d – ( e + f )
(
a
Infix to postfix conversion
stackVect
infixVect
postfixVect
b - c ) * d – ( e + f )
(
a
+
Infix to postfix conversion
stackVect
infixVect
postfixVect
- c ) * d – ( e + f )
(
a b
+
stackVect Infix to postfix conversion
infixVect
postfixVect
c ) * d – ( e + f )
(
a b +
-
stackVect Infix to postfix conversion
infixVect
postfixVect
) * d – ( e + f )
(
a b + c
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
* d – ( e + f )
a b + c -
Infix to postfix conversion
stackVect
infixVect
postfixVect
d – ( e + f )
a b + c -
*
Infix to postfix conversion
stackVect
infixVect
postfixVect
– ( e + f )
a b + c - d
*
Infix to postfix conversion
stackVect
infixVect
postfixVect
( e + f )
a b + c – d *
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
e + f )
a b + c – d *
-
(
Infix to postfix conversion
stackVect
infixVect
postfixVect
+ f )
a b + c – d * e
-
(
Infix to postfix conversion
stackVect
infixVect
postfixVect
f )
a b + c – d * e
-
(
+
Infix to postfix conversion
stackVect
infixVect
postfixVect
)
a b + c – d * e f
-
(
+
Infix to postfix conversion
stackVect
infixVect
postfixVect
a b + c – d * e f +
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
a b + c – d * e f + -
Infix to postfix conversion
stackVect
Stacks
Evaluating Expression
Expression: 5 3 - 6 + 8 2 / 1 2 + - *
5 2 8
3
5
Execute 5 - 3 Execute 2 + 6
8
8
2
8
8
Execute 8 / 2
4
8
1
4
8
2
1
4
8 Execute
1 + 2
3
4
8
Execute
4 - 3
1
8
Execute
8 * 1
8
3
5
3
5
6
2
Algorithm for evaluating
expression
By using a stack algorithm
1. Initialize an empty stack
2. Repeat the following until the end of the
expression is encountered
a) Get the next token (const, var, operator) in the
expression
b) Operand – push onto stack
Operator – do the following
i. Pop 2 values from stack
ii. Apply operator to the two values
iii. Push resulting value back onto stack
3. When end of expression encountered,
value of expression is the (only) number
left in stack
Towers of Hanoi
Algorithm for Balanced Symbol
Checking
 Make an empty stack
 read symbols until end of file
 if the symbol is an opening symbol push it onto the
stack
 if it is a closing symbol do the following
 if the stack is empty report an error
 otherwise pop the stack. If the symbol popped does not
match the closing symbol report an error
 At the end of the file if the stack is not empty
report an error
Queue
 Two types of queue:-
 Simple Queue
 Circular Queue
Basic Insert Alg for Queue
 QINSERT(QUEUE,N,FRONT,REAR,ITEM)
1. If FRONT = 1 and REAR = 1, or if FRONT = REAR
+ 1, then Write OVERFLOW and Return
2. If FRONT = NULL, then
SET FRONT = 1 and REAR = 1
Else If REAR = N, then:
SET REAR = 1
Else
SET REAR = REAR+1
3. Set QUEUE[REAR] = ITEM
4. Return
Basic Delete Alg for Queue
 QDELETE(QUEUE,N,FRONT,REAR,ITEM)
1. If FRONT = NULL, then Write UNDERFLOW and
Return
2. Set ITEM = QUEUE[FRONT]
3. If FRONT = REAR, then
SET FRONT = NULL and REAR = NULL
Else If FRONT = N, then:
SET FRONT = 1
Else
SET FRONT = FRONT+1
3. Return
Working of a simple queue
0 1 2 3 4 5 6 7
Proc
ess:
Elements
:
a b c d e f g h
F
R
-
1
Animated by S.Graceline Jasmine
Working of a circular queue
1 2 3 4 5 6 7 8
Proc
ess:
Elements
:
a b c d e f g h
F
R
0
Animated by M.Jayasudha
i j
F = R = 0
Priority Queue
 A priority queue is a collection of elements such
that each element has been assigned a priority
and such that the order in which elements are
deleted and processed comes from the following
rules:
 An element of higher priority are processed before
any element of lower priority.
 Two elements with the same priority are
processed according to the order in which they
were added to the queue.
Array representation of a priority queue
 Use separate queue foe each level of priority.
 Each such queue will appear in its own circular
array and must have its own pair of pointers,
FRONT and REAR.
 If each queue is allocated the same amount of
space, a two-dimensional array QUEUE can be
used instead of linear arrays.
Example for Priority Queue
 Front[K] and REAR[K] contain, respectively, the
front and rear elements of row K of QUEUE.
2
1
0
5
4
Front Rear
2
3
0
1
4
AAA
BBB CCC XXX
FFF DDD EEE
GGG
1 2 3 4 5 6
1
2
3
4
5
Implementation of stacks and queues in C

Implementation of stacks and queues in C

  • 1.
  • 2.
    Stack  Stores aset of elements in a particular order  Stack principle: LAST IN FIRST OUT = LIFO  It means: the last element inserted is the first one to be removed  Example
  • 3.
    Last In FirstOut B A D C B A C B A D C B A E D C B A top top top top top A
  • 4.
    Stack Operations  Thelast item added is pushed (added) to the stack.  The last item added can be popped (removed) from the stack.  The last item added can be topped (accessed) from the stack.  These operations all take constant time: O(1). A typical stack interface: void push(Thing newThing); void pop(); Thing top();
  • 5.
    Basic Alg forStack Push & Pop  PUSH(STACK,TOP,MAXSTK,ITEM) 1. If TOP=MAXSTK, then print OVERFLOW, and Return 2. Set TOP = TOP +1 3. Set STACK[TOP] = ITEM 4. Return  POP(STACK,TOP,MAXSTK,ITEM) 1. If TOP=0, then print UNDERFLOW, and Return 2. ITEM = Set STACK[TOP] 3. Set TOP = TOP - 1 4. Return
  • 6.
    Stack Applications  StringConversion  Converting Infix to Postfix  Evaluation of Expression  Towers of Hanoi  Balancing symbols
  • 7.
    Infix to postfix Algorithm 1) Examine the next element in the input. 2) If it is an operand, output it. 3) If it is opening parenthesis, push it on stack. 4) If it is an operator, then i) If stack is empty, push operator on stack. ii) If the top of the stack is opening parenthesis, push operator on stack.
  • 8.
     iii) Ifit has higher priority than the top of stack, push operator on stack. iv) Else pop the operator from the stack and output it, repeat step 4. 5) If it is a closing parenthesis, pop operators from the stack and output them until an opening parenthesis is encountered. pop and discard the opening parenthesis. 6) If there is more input go to step 1 7) If there is no more input, unstack the remaining operators to output.
  • 9.
    infixVect postfixVect ( a +b - c ) * d – ( e + f ) Infix to postfix conversion
  • 10.
    infixVect postfixVect a + b- c ) * d – ( e + f ) ( stackVect Infix to postfix conversion
  • 11.
    infixVect postfixVect + b -c ) * d – ( e + f ) ( a Infix to postfix conversion stackVect
  • 12.
    infixVect postfixVect b - c) * d – ( e + f ) ( a + Infix to postfix conversion stackVect
  • 13.
    infixVect postfixVect - c )* d – ( e + f ) ( a b + stackVect Infix to postfix conversion
  • 14.
    infixVect postfixVect c ) *d – ( e + f ) ( a b + - stackVect Infix to postfix conversion
  • 15.
    infixVect postfixVect ) * d– ( e + f ) ( a b + c - Infix to postfix conversion stackVect
  • 16.
    infixVect postfixVect * d –( e + f ) a b + c - Infix to postfix conversion stackVect
  • 17.
    infixVect postfixVect d – (e + f ) a b + c - * Infix to postfix conversion stackVect
  • 18.
    infixVect postfixVect – ( e+ f ) a b + c - d * Infix to postfix conversion stackVect
  • 19.
    infixVect postfixVect ( e +f ) a b + c – d * - Infix to postfix conversion stackVect
  • 20.
    infixVect postfixVect e + f) a b + c – d * - ( Infix to postfix conversion stackVect
  • 21.
    infixVect postfixVect + f ) ab + c – d * e - ( Infix to postfix conversion stackVect
  • 22.
    infixVect postfixVect f ) a b+ c – d * e - ( + Infix to postfix conversion stackVect
  • 23.
    infixVect postfixVect ) a b +c – d * e f - ( + Infix to postfix conversion stackVect
  • 24.
    infixVect postfixVect a b +c – d * e f + - Infix to postfix conversion stackVect
  • 25.
    infixVect postfixVect a b +c – d * e f + - Infix to postfix conversion stackVect
  • 26.
    Stacks Evaluating Expression Expression: 53 - 6 + 8 2 / 1 2 + - * 5 2 8 3 5 Execute 5 - 3 Execute 2 + 6 8 8 2 8 8 Execute 8 / 2 4 8 1 4 8 2 1 4 8 Execute 1 + 2 3 4 8 Execute 4 - 3 1 8 Execute 8 * 1 8 3 5 3 5 6 2
  • 27.
    Algorithm for evaluating expression Byusing a stack algorithm 1. Initialize an empty stack 2. Repeat the following until the end of the expression is encountered a) Get the next token (const, var, operator) in the expression b) Operand – push onto stack Operator – do the following i. Pop 2 values from stack ii. Apply operator to the two values iii. Push resulting value back onto stack 3. When end of expression encountered, value of expression is the (only) number left in stack
  • 28.
  • 29.
    Algorithm for BalancedSymbol Checking  Make an empty stack  read symbols until end of file  if the symbol is an opening symbol push it onto the stack  if it is a closing symbol do the following  if the stack is empty report an error  otherwise pop the stack. If the symbol popped does not match the closing symbol report an error  At the end of the file if the stack is not empty report an error
  • 30.
    Queue  Two typesof queue:-  Simple Queue  Circular Queue
  • 31.
    Basic Insert Algfor Queue  QINSERT(QUEUE,N,FRONT,REAR,ITEM) 1. If FRONT = 1 and REAR = 1, or if FRONT = REAR + 1, then Write OVERFLOW and Return 2. If FRONT = NULL, then SET FRONT = 1 and REAR = 1 Else If REAR = N, then: SET REAR = 1 Else SET REAR = REAR+1 3. Set QUEUE[REAR] = ITEM 4. Return
  • 32.
    Basic Delete Algfor Queue  QDELETE(QUEUE,N,FRONT,REAR,ITEM) 1. If FRONT = NULL, then Write UNDERFLOW and Return 2. Set ITEM = QUEUE[FRONT] 3. If FRONT = REAR, then SET FRONT = NULL and REAR = NULL Else If FRONT = N, then: SET FRONT = 1 Else SET FRONT = FRONT+1 3. Return
  • 33.
    Working of asimple queue 0 1 2 3 4 5 6 7 Proc ess: Elements : a b c d e f g h F R - 1 Animated by S.Graceline Jasmine
  • 34.
    Working of acircular queue 1 2 3 4 5 6 7 8 Proc ess: Elements : a b c d e f g h F R 0 Animated by M.Jayasudha i j F = R = 0
  • 35.
    Priority Queue  Apriority queue is a collection of elements such that each element has been assigned a priority and such that the order in which elements are deleted and processed comes from the following rules:  An element of higher priority are processed before any element of lower priority.  Two elements with the same priority are processed according to the order in which they were added to the queue.
  • 36.
    Array representation ofa priority queue  Use separate queue foe each level of priority.  Each such queue will appear in its own circular array and must have its own pair of pointers, FRONT and REAR.  If each queue is allocated the same amount of space, a two-dimensional array QUEUE can be used instead of linear arrays.
  • 37.
    Example for PriorityQueue  Front[K] and REAR[K] contain, respectively, the front and rear elements of row K of QUEUE. 2 1 0 5 4 Front Rear 2 3 0 1 4 AAA BBB CCC XXX FFF DDD EEE GGG 1 2 3 4 5 6 1 2 3 4 5