# 7-Stack-n-queue

```7 – Stack and Queue
Objectives of these slides:


Explain the design, use, and operation of a stack and
a queue
Implement a stack and a queue using a linked list
structure
1
Overview:
1. A Stack Collection
2. Stack Implementation
3. Stack applications
4. A queue Collection
5. Queue Implementation
2
1. The Stack Collection
A stack is a sequence of items that are
accessible only from the top of the stack.
pop
push
Other
operations:
A Stack (of plates)
isEmpty
peek
size
3
1. The Stack Collection: Stack Operations
A stack is a Last In, First Out (LIFO) data structure
in which all insertions and deletion are restricted to
one end called a top
Three basic stack operations
- Push
- Pop
- Stack Top
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
4
1.2 Stack Operations : Push
1. Push :
 adds an item at the top of the stack
 after the push, the new item becomes the
top
 the stack is in an overflow state if there is
no room for the new item
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
5
1.2 Stack Operations : Pop
2. Pop
 when a stack is popped, the item at the top
of the stack is removed and return it to the
user
 as the top item has been removed, the next
older item in the stack becomes the top
 when the last item in the stack is deleted, it
must be set to its empty state
 if pop is called when the stack is empty,
then it is in an underflow state
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
6
1.2 Stack Operations : Pop
7
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
1.2 Stack Operations : Stack Top
3. Stack Top
 copies the item at the top of the stack
 it returns the data in the top element to the
user but does not delete it
 stack top can also result in underflow if the
stack is empty
8
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Stack operations Example (1/2)
0. with an empty
stack
1. push green into
stack
2. push blue into
stack
3. pop
4. push red into
stack
9
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Stack operations Example (2/2)
5.
6.
7.
stack top =?
pop
pop
10
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
2. Stack implementation
There are several data structures that could
be used to implement a stack, e.g. array or
In this section, we implement it as a linked list
To implement the linked-list stack, we need
two different structures: - a head node
a data node
11
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
2.1 Stack Head and Stack Data Node
stack head requires at least 2 attributes
 a count of the number of elements in the
stack
 a top pointer
other stack attributes can be placed in a stack
 such as the time the stack was created
stack data node also requires at least 2
attributes
 data
 pointer to the next node
12
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Stack head node structure and data node
structure
13
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
2.2 Stack Algorithms
8 operations are defined to solve any basic
stack problem
 create stack
 destroy stack
 push stack
 pop stack
 Checking stack status
full stack
 empty stack
 a number of member (stack count)
 stack top

there may be additional stack operations
14
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Stack operations
15
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Create Stack
16
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Push
Stack
17
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Pop Stack
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
18
Stack Top
19
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Empty Stack Checking
20
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Full Stack Checking
21
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Stack Count
22
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Stack Destruction
23
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
3. Stack Applications
3.1 Reversing Data
3.2 Converting Decimal to Binary
3.3 Converting Infix to Postfix (optional)
24
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
3.1 Reversing Data
given a set of data, the first and last elements
are exchanged with all of the positions between
the first and last being relatively exchanged
also
for example:
 {1, 2, 3, 4} becomes {4, 3, 2, 1}
we examine 2 different reversing applications:
 reveres a list
 convert decimal to binary
25
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Algorithm Reverse a number series (1/2)
Algorithm reverseNumber
This program reverses a list of integers read
from the keyboard by pushing them into a
stack and retrieving them one by one
Begin reverseNumber
1 stack = createStack
2 print(Enter a number)
Continued…
26
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Algorithm Reverse a number series (2/2)
/*---- Fill stack ---* /
4 loop ( not end of data AND stack not full)
1 pushStack (number)
2 prompt(Enter next number: <EOF> to stop)
/*---- Print numbers in reverse ---*/
5 loop (not emptyStack (stack)
1 popStack (stack, dataOut)
2 print (dataOut)
6 stack = destroyStack (stack)
End reverseNumber
27
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
3.2 Convert Decimal to Binary
1
2
loop (number > 0)
1 digit = number modulo 2
2 print (digit)
3 number = number / 2
28
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Algorithm Convert decimal to binary (1/2)
algorithm decimalToBinary
This algorithm reads an integer from the keyboard and print its binary equivalent. It
uses a stack to reverse the order of 0’s and 1’s produces.
Begin decimalToBinary
1
2
3
4
stack = createStack
prompt(Enter a decimal to convert to binary)
loop (number > 0)
1 digit = number modulo 2
2 pushOK = push (stack, digit)
3 if (pushOK ≠ false)
1 print (Stack overflow creating digit)
2 quit algorithm
4 number = number/2
Continued…
29
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Algorithm Convert decimal to binary (2/2)
/* Binary number create in stack. Now print it.
*/
5
loop (not emptyStack(stack))
1 popStack (stack, digit)
2 print(digit)
/* Binary number create. Destroy stack and
return */
6
destroy(stack)
End decimalToBinary
30
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
3.3 Convert Infix to Postfix (arithmetic
expression)
Prefix Notation (Polish notation): operator is always placed
before its operands
e.g. + 1 2 , * 3 – 4 5 , / 9 + 1 – 8 6
In the Postfix evaluation format for an expression, an operator
comes after its operands.
 also called Reverse Polish Notation (RPN)
Examples:
Infix notation
Postfix notation
 a+b
RPN: a b +
 a*b +c
RPN: a b * c +
 a + (b * c)
RPN: a b c * +
 (a + b) * c
RPN: a b + c *
 (a*b + c) / d
RPN: a b * c + d /
31
Postfix Evaluation (1/4)
To evaluate a postfix expression, execute the
following steps until the end of the expression.
1. If current input is an operand, push it on the
stack.
2. If current input is an operator, pop its two
operands, apply the operator, and push the result
onto the stack.
3. At the end of input, the value of the postfix
expression is on the top of the stack.
 Postfix and prefix (Polish) notations do not need
the parenthesis and priority of operators. The order
of evaluation is not ambiguous. One expression
can only be evaluated in one way.
32
Postfix Evaluation (2/4)
Example: evaluate "4 3 * 5 +"
3
4
4
5
12
12
17
Evaluate these postfix expressions:
 8 2–3/4 2*+
 28 7 13 5 6 + - * /
33
Postfix evalutation: Error: too many
operators (3/4)
e.g. 3 8 + * 9
The stack will contain only one element when
we reach "*".
34
Postfix evalutation: Error: too many
operands (4/4)
e.g. 9 8 + 7
The stack will contain too many operands at
end of input.
35
Algorithm for coverting Infix to Postfix
(1/2)
36
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Algorithm for coverting Infix to Postfix
(2/2)
3 end loop
4 pushStack (Stack, token)
4 else
Character is operand
1 Concatenate token to postFix
5 end if
3 end loop
Input formula empty. Pop stack to postfix
4 loop ( not emptyStack(Stack) )
1 popStack(Stack, character)
2 concatenate token to postFixExpr
5 end loop
6 return postFix
end intoPostFix
37
Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
4. Queue Collection
Like a stack, a queue is also a list. However, with a
queue, insertion is done at one end, while deletion
is performed at the other end.
front
rear
Accessing the elements of queues follows a First
In, First Out (FIFO) order.

Like customers standing in a check-out line
in a store, the first customer in is the first
customer served (first come first serve).
Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt
38
4.1 Enqueue and Dequeue
Primary queue operations:
 Enqueue and Dequeue
Like check-out lines in a store, a queue has a
front and a rear.
 Enqueue
 insert an element at the rear of the queue
 Dequeue
 remove an element from the front of the
queue
Remove
(Dequeue)
Insert
(Enqueue)
front
Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt
rear
39
5. Implementation of Queue
Just as stacks can be implemented as arrays or
Dynamic queues have the same advantages over
static queues as dynamic stacks have over static
stacks
Example: Queue implemented with linked list
data structure
Count (number of elements)
Front (pointer to front node) Rear (pointer to
rear node)
 Queue Node Structure
Data (node data)
Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt
40
5.1 Application of Queues
In a multitasking operating system, the CPU
time is shared between multiple processes.
At a given time, only one process is running,
all the others are ‘sleeping’. The CPU time is
administered by the scheduler. The scheduler
keeps all current processes in a queue with
the active process at the front of the queue.
Original slides from http://isg.cs.tcd.ie/giangt/Stack-Queue.pdf
41
5.2 Stack and Queue comparison
List out one similarity and one difference between
Stack and Queue
 Similarity
 Both ADTs can be implemented using array or