Title: Stack, Queue and List
1Stack, Queue and List
2Container
- Stack, Queue, and List are containers
- e.g. Stack of char, Queue of Person, List of
integer - we can store entry into a container
- we can retrieve an entry from a container
3Stack
A stack is open at one end (the top) only. You
can push entry onto the top, or pop the top entry
out of the stack. Note that you cannot
add/extract entry in the middle of the stack.
top
C
B
A
bottom
4Last-in First-out (LIFO)
The last one pushed in is the first one popped
out! (LIFO)
When we push entries onto the stack and then pop
them out one by one, we will get the entries in
reverse order.
5Usage of Stack
- Rev.dpr - reverse a string
- Check nesting structure - paren.dpr
- Undo list
- Stack frame of procedure call
- Hardware stack in CPU
6Example 1
program example1 uses AStack var S Stack c
char begin CreateStack(S) Push(a, S)
Push(b, S) Pop(c, S) writeln(c) end.
The type Stack and the procedures CreateStack,
Push and Pop are defined in the unit AStack.
Before using a stack, we must create it first!
7Array implementation
const MAXSTACK6 type StackEntry char
Stack record top 0..MAXSTACK entry
array 1..MAXSTACK of StackEntry end
top 3
entry
8Create Stack
procedure CreateStack (var S stack) begin
S.top 0 end
entry
top 0
9Push
procedure Push (x StackEntry var S
Stack) begin if S.top MAXSTACK then
Error('Cannot push onto a full stack') else
begin S.top S.top 1 S.entryS.top
x end end
entry
top 0 ?1
10Pop
procedure Pop (var x StackEntry var S
Stack) begin if S.top0 then Error('Cannot
pop from empty stack.') else begin x
S.entryS.top S.top S.top - 1
end end
entry
top 1 ?0
11Other operations
- StackEmpty
- StackFull
- ClearStack
- StackSize
- StackTop
12Linked list implementation
type StackEntry char StackPointer
StackNode StackNode record entry
StackEntry next StackPointer end
Stack record top StackPointer end
13Create Stack
procedure CreateStack (var S stack) begin
S.top nil end
top
14Push
Please refer to the program listing for detail.
15Pop
Please refer to the program listing for detail.
16Queue
enqueue
dequeue
front
rear
A queue is open at two ends. You can only add
entry (enqueue) at the rear , and delete entry
(dequeue) at the front. Note that you cannot
add/extract entry in the middle of the queue.
17First-in First-out (FIFO)
The first one enqueued is the first one dequeued.
(FIFO)
When we enqueue entries in the queue and then
dequeue them one by one, we will get the entries
in the same order.
18Usage of Queue
- Queues in the real world
- e.g. queue of person
- Queue of jobs to be done
- Message queue in Windows
- I/O buffer
19Example 2
program example2 uses AQueue var Q Queue c
char begin CreateQueue(Q) Enqueue(a, Q)
Enqueue(b, Q) Dequeue(c, Q)
writeln(c) end.
The type Queue and the procedures CreateQueue,
Enqueue and Dequeue are defined in the unit
AQueue.
Before using a queue, we must create it first!
20Array implementation, 1st trial
deQ A,B,C, then enQ F and G..
D
E
G
A
B
C
D
E
F
rear 6
rear 5
front 4
We cannot enqueue G because the rear has
reached the end of the array.
front 1
entry
entry
21Circular Array
If we view the array as a circle, the cell after
the 6th cell is the 1st cell.
G
entry
6
front 4
5
rear 6 ? 1
4
3
2
1
entry
22Array implementation
const MAXQUEUE6 type QueueEntry char
Queue record count, front, rear
0..MAXQUEUE entry array1..MAXQUEUE
of QueueEntry end
front 4
rear 6
6
5
4
3
2
1
Number of entries in the Q
count 3
entry
23Array implementation
G
G
front 1
front 4
front 2
front 1
rear 3
rear 1
rear 1
rear 1
6
6
6
6
5
5
5
5
4
4
4
4
3
3
3
3
2
2
2
2
1
1
1
1
entry
entry
entry
entry
24Create Queue
procedure CreateQueue (var Q Queue) begin
with Q do begin count 0 front
1 rear 0 end end
We can also use these values front 2 rear
1 front 5 rear 4front 3 rear
2 front 6 rear 5 front 4 rear
3 front 1 rear 6 We have to make
sure that when we enqueue the first entry, front
and rear points to that entry.
25Enqueue
procedure Enqueue (x QueueEntry var Q
Queue) begin with Q do begin if
QueueFull(Q) then Error() else
begin count count 1 rear
(rear mod MAXQUEUE) 1 entryrear
x end end
This formula is the same as If rearMAXQUEUE
then rear 1 else rear rear 1
Remember to update count whenever the number of
entries in the Q changes!
26Dequeue
procedure Dequeue (var x QueueEntry var Q
Queue) begin with Q do begin if
QueueEmpty(Q) then Error() else
begin count count - 1 x
entryfront front (front mod MAXQUEUE)
1 end end end
Front is updated in a similar way as rear in
Enqueue.
Remember to check underflow and overflow properly.
27Traversing a Queue
procedure PrintQueue (var Q Queue) var i, c
integer begin i Q.front write('ltlt ')
for c 1 to QueueSize(Q) do begin
write(Q.entryi, ' ') i (i mod
MAXQUEUE) 1 end writeln(gtgt) end
28Other operations
- QueueEmpty
- QueueFull
- ClearQueue
- QueueSize
- QueueFront
- PrintQueue
29Linked list implementation
Which one is better?
30Linked list implementation
type QueueEntry char QueuePointer
QueueNode QueueNode record entry
QueueEntry next QueuePointer end Queue
record front, rear QueuePointer
count integer end
31Create queue
type QueueEntry char QueuePointer
QueueNode QueueNode ... Queue record
front, rear QueuePointer count integer
end
front
rear
count 0
32Enqueue
D
2. rear.next newNode
B
front
C
A
3. rear newNode
rear
D
newNode
1. Initialize newNode
33Dequeue
2. frontfront.next
delNode
3. Dispose this node
1. delNodefront
34Handling empty queue
enqueue
dequeue
Empty queue has to be handled specially in
Enqueue and Dequeue! Why? Read the code
carefully.
35Deque
addReardelRear
addFrontdelFront
A
B
C
front
rear
A double-ended queue (Deque) is open at two ends.
You can add/delete entry (addRear/ delRear) at
the rear , or add/delete entry (addFront/
delFront) at the front. But you cannot add/delete
entry in the middle of the deque.
36List
A list is a sequence of nodes. You can insert a
node and delete a node anywhere. And you can
retrieve or replace the entry in any
position. Stack and Queue are special cases of
list.
1
2
3
position
37Example 3
program example3 uses SList var L List a
integer begin CreateList(L) InsertList(1,
10, L) InsertList(2, 20, L) InsertList(2,
15, L) DeleteList(3, a, L)
PrintList(L) end.
38Operations on list
- CreateList(L)
- InsertList(pos, x, L)
- DeleteList(pos, x, L)
- RetrieveList(pos, x, L)
- ReplaceList(pos, x, L)
- ClearList(L)
39Usage of List
- Many...
- With many variations...
40Array implementation
- Inefficient to implement list using array. Need
to copy entries around a lot.
41Linked list implementation
- Singly linked list next
- Doubly linked list next, prev (previous)
42Singly linked list implementation
1
2
3
type ListEntry char ListPointer
ListNode ListNode record entry
ListEntry next ListPointer end List
record head ListPointer count integer
end
43Create List
head
procedure CreateList (var L List) begin
L.head nil L.count 0 end
44Insert List
B
head
C
A
C
B
A
X
1. Move p to pos-1
p
InsertList(3,x,L)
B
C
A
head
X
newNode
3. p.next newNode
2. Initialize newNodenewNode.next p.next
45Delete List
B
head
C
A
C
A
B
1. Move p to pos-1
p
DeleteList(2,x,L)
C
A
head
B
delNode
3. p.next delNode.next
2. delNode p.next
46Special case
- InsertList(1,x,L) and DeleteList(1,x,L) have to
be handled specially!
47Doubly-linked list
type ListEntry char ListPointer
ListNode ListNode record entry
ListEntry next, prev ListPointer end List
record head ListPointer count integer
end
48Create List
head
count 0
49Insert List
- Four cases
- case 1 insert into empty list
- case 2 nonempty list, pos1
- case 3 nonempty list, posL.count
- case 4 nonempty list, 1ltposltL.count
50Insert List, case 4
head
C
A
newnode
head
C
B
A
51Insert List, case 2
head
C
A
newnode
B
C
A
52Insert List, case 3
head
C
A
newnode
head
B
C
A
53Insert List, case 1
head
newnode
B
54Delete List
- Four cases
- case 1 delete the only node of a list
(L.count1) - case 2 delete the first node, L.countgt1
- case 3 delete the last node, L.countgt1
- case 4 delete a middle node, 1ltposltL.count,
L.countgt1