Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks and Queues Dr. Andrew Wallace PhD BEng(hons) EurIng

Similar presentations


Presentation on theme: "Stacks and Queues Dr. Andrew Wallace PhD BEng(hons) EurIng"— Presentation transcript:

1 Stacks and Queues Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

2 Overview In and out Stack operations Stack implementation Queue operations Queue implementation Complexity

3 In and Out Data Memory Address Data Memory Address Data Memory Address Data Memory Address Data Memory Address Data Memory Address Data Memory Address Data Memory Address Stack Queue

4 In and out LIFO Last in first out FIFO First in first out

5 Stack Operations S[1 … S.top] Is empty Push Pop

6 Stack Operations IsEmpty(S) if S.top == null return TRUE else return FALSE

7 Stack Operations Push(s.x) S.top = S.top + 1 S[S.top] = s.x data S.top s.x

8 Stack Operations Pop(S) ASSERT(!IsEmpty(S)) S.top = S.top -1 return S[S.top + 1] data S.top s.x

9 Stack Implementation Array Linked list

10 Stack Implementation 1023 13 s.tops.top 012345 83 s.tops.top

11 Queue Operations Q[Q.head … Q.tail] Enqueue(Q.tail) Dequeue(Q.head)

12 Queue Operations Enqueue(Q.x) Q.tail = Q.x if Q.tail == Q.length Q.tail = 1 else Q.tail = Q.tail + 1 data Q.head Q.tail Q.x Q.tail

13 Queue Operations Dequeue(Q) Q.x = Q[Q.head] if Q.head == Q.length Q.head = 1 else Q.head = Q.head + 1 return Q.x data Q.tail data Q.head Q.x

14 Queue Implementation Array Linked list

15 Queue Implementation 1023 13 Q.headQ.head 012345 67 Q.TailQ.Tail Q.TailQ.Tail

16 Queue Implementation 1023 13 Q.headQ.head 90 Q.TailQ.Tail Q.TailQ.Tail 012345

17 Complexity Pop(S) ASSERT(!IsEmpty(S)) S.top = S.top -1 return S[S.top + 1] Dequeue(Q) Q.x = Q[Q.head] if Q.head == Q.length Q.head = 1 else Q.head = Q.head + 1 return Q.x Q(1)

18 Questions?


Download ppt "Stacks and Queues Dr. Andrew Wallace PhD BEng(hons) EurIng"

Similar presentations


Ads by Google