1 chapter 7 stacks and queues. 2 stack adt recall that adt is abstract data type, a set of data and...

70
1 Chapter 7 Stacks and Queues

Upload: douglas-day

Post on 03-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

1

Chapter 7

Stacks and Queues

Page 2: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

2

Stack ADT

• Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data.

• In a stack, the set of data is the stack of elements.

• Stack is known as a LIFO (last-in-first-out) data structure because the last data to enter the stack is the first to exit the stack.

Page 3: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

3

Stack ADT Operations

• push: places an element onto the top of a stack.

• pop: removes an element from the top of the stack.

• peek: which retrieves (copies) a value from the top of the stack without removing it.

• an operation to determine whether or not the stack is empty.

• an operation to empty out a stack.

Page 4: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

4

Push

17

5

11

3

• Push means place a new data element at the top of the stack

stack

Page 5: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

5

Push (cont.)

17

5

11

3

• Push means place a new data element at the top of the stack

stack

Page 6: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

6

Push (cont.)

17

5

11

3

• Push means place a new data element at the top of the stack

stack

Page 7: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

7

Pop

17

5

11

3

• Pop means take a data element off the top of the stack

stack

Page 8: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

8

Pop (cont.)

17

5

11

3

• Pop means take a data element off the top of the stack

stack

Page 9: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

9

Pop (cont.)

17

5

11

3

• Pop means take a data element off the top of the stack

stack

Page 10: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

10

Peek

17

5

11

3

3

• Peek means retrieve the top of the stack without removing it

stack

Page 11: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

11

3 template <typename T>4 class Stack {5 public:6 Stack() { … }7 ~Stack( ) { … }8 void push( T& elementToPush ) { … }9 bool pop( T& poppedElement ) { … }10 bool peek( T& topElement ) { … }11 bool isEmpty( ) { … }12 void makeEmpty( ) { … }13 private:14 T* elements; // dynamic array15 int size;16 int top;17 };

Array Stack Class Template

used as an index to the top of the stack

Page 12: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

12

3 Stack () : top(-1), size(0)4 {5 elements = NULL;6 }7 8 ~Stack( )9 {10 makeEmpty( );11 }

Array Stack Constructor and Destructor

Page 13: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

13

Array StackisEmpty and makeEmpty

65 bool isEmpty( )66 {67 return top == -1;68 }6970 void makeEmpty( )71 {72 size = 0;73 top = -1; 74 delete [] elements;75 }

Page 14: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

14

Array Stack Pop

125 25 200 70

elements

0 1 2 3 top

An element can’t really be removed from an array, as one would think pop would achieve.

Page 15: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

15

Array Stack Pop(cont.)

125 25 200 70

elements

0 1 2 3 top

The element 70 is at the top of the stack, and what really happens during a pop, is that 70 is returned to the client…

client

Page 16: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

16

Array Stack Pop(cont.)

125 25 200 70

elements

0 1 2 3 top

and top is decremented…

client

Page 17: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

17

Array Stack Pop(cont.)

125 25 200 70

elements

0 1 2 3 top

The element 70 is still in the array, but it is no longer accessible. The next push will overwrite it. Say, we would like to push 63…

client

Page 18: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

18

Array Stack Push

125 25 200 70

elements

0 1 2 3 top

First, top is incremented…

Page 19: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

19

Array Stack Push(cont.)

125 25 200 63

elements

0 1 2 3 top

Then, 63 is pushed into that position…

Page 20: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

20

Is the Array Stack Full/Empty?• An array stack is full when

– top == size - 1• An array stack is empty when

– top == -1

Page 21: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

21

Linked-List Stack

• Stacks can also be implemented with a linked list.

• The front node is the top of the stack.

Page 22: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

22

Bo b Ali

Linked-List Stack(cont.)

top

To pop, we remove the node at the front of the linked list, and return the element to the client…

Ali top

Page 23: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

23

Linked-List Stack(cont.)

top

To push Cat, we place the new element in a node and insert it at the front of the linked list…

Cat top Ali

Ali

Page 24: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

24

Linked-List Stack Class Template

13 template <typename T>14 class Stack {15 public:16 Stack( ) { … }17 ~Stack( ) { … }18 void push( T & element ) { … }19 bool pop( T & element ) { … }20 bool peek( T & element ) { … }21 bool isEmpty( ) { … } 22 void makeEmpty( ) { … }23 private:24 Node<T> *top;25 };

Page 25: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

25

8 Stack( ) : top(NULL) { }7 8 ~Stack( )9 {10 makeEmpty( );11 }

Linked-List Stack Constructor and Destructor

Page 26: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

26

Linked-List Push

32 void push( T& element ) {33 Node<T> *newNode = new Node<T>;34 newNode->info = element;35 newNode->next = top;36 top = newNode;37 }

newNode

Page 27: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

27

Linked-List Push into Empty Stack

32 void push( T& element ) {33 Node<T> *newNode = new Node<T>;34 newNode->info = element;35 newNode->next = top;36 top = newNode;37 }

top

newNode

NULL

Page 28: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

28

Linked-List Push into Empty Stack (cont.)

32 void push( T& element ) {33 Node<T> *newNode = new Node<T>;34 newNode->info = element;35 newNode->next = top;36 top = newNode;37 }

top

newNode

Page 29: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

29

Linked-List Push into Non-Empty Stack

32 void push( T& element ) {33 Node<T> *newNode = new Node<T>;34 newNode->info = element;35 newNode->next = top;36 top = newNode;37 }

newNode

top

Page 30: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

30

Linked-List Push into Non-Empty Stack (cont.)

32 void push( T& element ) {33 Node<T> *newNode = new Node<T>;34 newNode->info = element;35 newNode->next = top;36 top = newNode;37 }

newNode

top

Page 31: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

31

Linked-List Peek

56 bool peek( T& element )57 {58 if ( top == NULL )59 return false;60 element = top->info;61 return true;62 }

Page 32: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

32

Linked-List Pop

56 bool pop( T& element )57 {58 if ( top == NULL )59 return false;60 element = top->info;61 Node<T> *ptr = top;62 top = top->next;63 delete ptr;64 return true;65 }

Page 33: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

33

Linked-List StackisEmpty and makeEmpty

65 bool isEmpty( ) 66 {67 return top == NULL;68 }6970 void makeEmpty( )71 {72 T temp;73 while ( pop( temp ) );74 }

Page 34: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

34

The Queue ADT

• The queue is a data structure that is like a line of people– When people join the line, they go at the end– When people are served, they come off the

front of the line

• Queue is known as a FIFO (first-in, first-out) data structure because the last data to enter the queue is the last to exit from the queue.

Page 35: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

35

Queue ADT Operations

• enqueue: add an element to the end of the line

• dequeue: take an element from the front of the line

• peek: retrieve (copy) the element at the front of the line without removing it

• an operation to determine whether or not the queue is empty

• an operation that will empty out the queue

Page 36: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

36

Queue (cont.)

• In addition to a pointer at the beginning of the linked list (called front), a pointer to the end of the linked list (called back) is also maintained in the private section

• The back pointer makes it fast to add new elements to the end of the queue – you don’t have to use a loop to go all the way through the queue to find the last node

Page 37: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

37

Linked-List Dequeue

front back

Bob Ali

Page 38: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

38

Linked-List Dequeue(cont.)

front back

Ali

Page 39: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

39

Linked-List Enqueue

front back

Page 40: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

40

Linked-List Enqueue(cont.)

front back

Page 41: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

41

Linked-List Queue Class Template13 template <typename T>14 class Queue {15 public:16 Queue( );17 ~Queue( );18 void enqueue( T & element );19 bool dequeue( T & deqElement );20 bool peek( T & frontElement ); 21 bool isEmpty( ) ;22 void makeEmpty( );23 private:24 Node<T> *front;25 Node<T> *back;26 };

Page 42: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

42

4 Queue( )5 {6 front = back = NULL;7 }8 9 ~Queue( )10 {11 makeEmpty( );12 }

Linked-List Queue Constructor and Destructor

Page 43: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

43

Linked-List Queue Enqueue22 void enqueue( T & element )23 {

24 Node<T> *newNode = new Node<T>;25 newNode->info = element;26 newNode->next = NULL;27 if (front == NULL) { // list is empty.28 front = newNode;29 back = front;30 }31 else { // list is not empty.32 back->next = newNode;33 back = newNode;34 }35 }

newNode

Page 44: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

44

Linked-List Queue Enqueue (cont.)22 void enqueue( T & element )23 {24 Node<T> *newNode = new Node<T>;25 newNode->info = element;26 newNode->next = NULL;

27 if (front == NULL) { // list is empty.28 front = newNode;29 back = front;30 }31 else { // list is not empty.32 back->next = newNode;33 back = newNode;34 }35 }

newNode

Case 1: The queue is initially empty.

front

back

Page 45: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

45

Linked-List Queue Enqueue (cont.)22 void enqueue( T & element )23 {24 Node<T> *newNode = new Node<T>;25 newNode->info = element;26 newNode->next = NULL;27 if (front == NULL) { // list is empty.28 front = newNode;29 back = front;30 }

31 else { // list is not empty.32 back->next = newNode;33 back = newNode;34 }35 }

newNode

Case 2: The queue has nodes.

front

back

Page 46: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

46

Linked-List Queue Enqueue (cont.)22 void enqueue( T & element )23 {24 Node<T> *newNode = new Node<T>;25 newNode->info = element;26 newNode->next = NULL;27 if (front == NULL) { // list is empty.28 front = newNode;29 back = front;30 }31 else { // list is not empty.32 back->next = newNode;

33 back = newNode;34 }35 }

newNode

Case 2: The queue has nodes.

front

back

Page 47: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

47

Linked-List Queue Dequeue

41 bool dequeue( T & deqElement )42 {43 if ( front == NULL) 44 return false;

Dequeue continued…

Returns false if client tries to dequeue an empty queue.

Page 48: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

48

Linked-List Queue Dequeue (cont.)

45 deqElement = front->info;46 Node<T> *ptr = front;47 front = front->next;48 delete ptr;49 return true;50 }

front backptr

deqElement:

passed in by reference

Page 49: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

49

Linked-List Queue Dequeue (cont.)

45 deqElement = front->info;46 Node<T> *ptr = front;47 front = front->next;48 delete ptr;49 return true;50 }

front backptr

deqElement:

Page 50: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

50

Linked-List Queue Dequeue (cont.)

45 deqElement = front->info;46 Node<T> *ptr = front;47 front = front->next;48 delete ptr;49 return true;50 }

front backptr

deqElement:

Page 51: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

51

Linked-List Queue Peek

56 bool peek( T & frontElement )57 {58 if ( front == NULL)59 return false;60 frontElement = front->info;61 return true;62 }

Page 52: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

52

Linked-List Queue isEmpty and makeEmpty

65 bool isEmpty( ) 66 {67 return front == NULL;68 }6970 void makeEmpty( )71 {72 T temp;73 while ( dequeue( temp ) );74 }

Page 53: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

53

Array Queue

• Similar to the linked-list queue, there are 2 attributes called front and back, but they are indexes into an array instead of pointers.

• When enqueuing, the back index is incremented, and when dequeuing, the front index is incremented.

Page 54: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

54

Array Queue Class Template3 template <typename T>4 class Queue {5 public:6 Queue( ) { … }7 ~Queue( ) { … }8 void enqueue( T element ) { … 9 bool dequeue( T & deqElement ) { … }10 bool peek( T & frontElement ) { … }11 bool isEmpty( ) { … }12 void makeEmpty( ) { … }13 private:14 T *elements;15 int size16 int front;17 int back;18 };

Page 55: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

55

Array Queue Enqueue / Dequeue

0 1 2 3 4 5 6 7

front back

DEQUEUEDEQUEUEENQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUE

Page 56: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

56

0 1 2 3 4 5 6 7

front back

Array Queue Enqueue / Dequeue (cont.)

DEQUEUEDEQUEUEENQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUE

Page 57: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

57

0 1 2 3 4 5 6 7

front back

Array Queue Enqueue / Dequeue (cont.)

DEQUEUEDEQUEUEENQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUE

Page 58: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

58

0 1 2 3 4 5 6 7

front back

Array Queue Enqueue / Dequeue (cont.)

DEQUEUEDEQUEUEENQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUE

Page 59: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

59

0 1 2 3 4 5 6 7

front back

Array Queue Enqueue / Dequeue (cont.)

DEQUEUEDEQUEUEENQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUE

Page 60: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

60

0 1 2 3 4 5 6 7

front back

Array Queue Enqueue / Dequeue (cont.)

DEQUEUEDEQUEUEENQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUE

Page 61: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

61

0 1 2 3 4 5 6 7

front back

Array Queue Enqueue / Dequeue (cont.)

DEQUEUEDEQUEUEENQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUE

Page 62: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

62

0 1 2 3 4 5 6 7

front back

Array Queue Enqueue / Dequeue (cont.)

DEQUEUEDEQUEUEENQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUE

Page 63: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

63

0 1 2 3 4 5 6 7

front back

Array Queue Enqueue / Dequeue (cont.)

DEQUEUEDEQUEUEENQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUE

Page 64: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

64

0 1 2 3 4 5 6 7

front back

Array Queue Enqueue / Dequeue (cont.)

DEQUEUEDEQUEUEENQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUE

Page 65: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

65

0 1 2 3 4 5 6 7

front back

We have reached the end of array. How to enqueue? ?

Array Queue Enqueue / Dequeue (cont.)

DEQUEUEDEQUEUEENQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUE

Page 66: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

66

0 1 2 3 4 5 6 7

front back

Array Queue Enqueue / Dequeue (cont.)

We could double the size of the array here.

But if we keep doing this, we may have a million elements in the array, but only a few at the end are used!

DEQUEUEDEQUEUEENQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUE

Page 67: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

67

0 1 2 3 4 5 6 7

frontback

We handle this problem by having the back wrap around to the beginning of the array.

The front also wraps to the beginning when it reaches the end of the array

Array Queue Enqueue / Dequeue (cont.)

DEQUEUEDEQUEUEENQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUEDEQUEUEDEQUEUEENQUEUE

Page 68: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

68

Is Array Queue Full/Empty?• An array queue is empty when

– front = -1• An array queue has one element when

– front = back• An array queue is full when

– back + 1 = front

Page 69: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

69

A Full Array Queue

0 1 2 3 4 5 6 7

frontback

If the next operation is ENQUEUE, the array capacity will need to be doubled

Page 70: 1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,

Reference

• Childs, J. S. (2008). Stack and Queue. C++ Classes and Data Structures. Prentice Hall.

70