1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs...
-
Upload
regina-morton -
Category
Documents
-
view
222 -
download
0
Transcript of 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs...
![Page 1: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/1.jpg)
1
C++ Classes and Data StructuresJeffrey S. Childs
Chapter 8
Stacks and Queues
Jeffrey S. Childs
Clarion University of PA
© 2008, Prentice Hall
![Page 2: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/2.jpg)
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
![Page 3: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/3.jpg)
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 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/4.jpg)
4
Push
17
5
11
3
• Push means place a new data element at the top of the stack
![Page 5: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/5.jpg)
5
Push (cont.)
17
5
11
3
• Push means place a new data element at the top of the stack
![Page 6: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/6.jpg)
6
Push (cont.)
17
5
11
3
• Push means place a new data element at the top of the stack
![Page 7: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/7.jpg)
7
Push (cont.)
17
5
11
3
• Push means place a new data element at the top of the stack
![Page 8: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/8.jpg)
8
Pop
17
5
11
3
• Pop means take a data element off the top of the stack
![Page 9: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/9.jpg)
9
Pop (cont.)
17
5
11
3
• Pop means take a data element off the top of the stack
![Page 10: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/10.jpg)
10
Pop (cont.)
17
5
11
3
• Pop means take a data element off the top of the stack
![Page 11: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/11.jpg)
11
Pop (cont.)
17
5
11
3
• Pop means take a data element off the top of the stack
![Page 12: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/12.jpg)
12
Peek
17
5
11
3
• Peek means retrieve the top of the stack without removing it
![Page 13: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/13.jpg)
13
Peek (cont.)
17
5
11
3
3
• Peek means retrieve the top of the stack without removing it
![Page 14: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/14.jpg)
14
Peek (cont.)
17
5
11
3
3
• Peek means retrieve the top of the stack without removing it
![Page 15: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/15.jpg)
15
1 #include “Array.h”2
3 template <class DataType>4 class Stack5 {6 public:7 Stack( );8 void push( DataType elementToPush ); 9 bool pop( DataType & poppedElement );10 bool peek( DataType & topElement ); 11 bool isEmpty( ) const; 12 void makeEmpty( );13 private:14 Array<DataType> elements;15 int top;16 };1718 #include “Stack.cpp”
Stack Class Template
![Page 16: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/16.jpg)
16
1 #include “Array.h”2
3 template <class DataType>4 class Stack5 {6 public:7 Stack( );8 void push( DataType elementToPush ); 9 bool pop( DataType & poppedElement );10 bool peek( DataType & topElement ); 11 bool isEmpty( ) const; 12 void makeEmpty( );
13 private:14 Array<DataType> elements;15 int top;16 };17
18 #include “Stack.cpp”
Stack Class Template (cont.)
![Page 17: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/17.jpg)
17
1 #include “Array.h”2
3 template <class DataType>4 class Stack5 {6 public:7 Stack( );8 void push( DataType elementToPush ); 9 bool pop( DataType & poppedElement );10 bool peek( DataType & topElement ); 11 bool isEmpty( ) const; 12 void makeEmpty( );
13 private:14 Array<DataType> elements;15 int top;16 };17
18 #include “Stack.cpp”
Stack Class Template (cont.)
used as an index to the top of the stack
![Page 18: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/18.jpg)
18
The Actual 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 19: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/19.jpg)
19
The Actual 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…
![Page 20: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/20.jpg)
20
The Actual 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 21: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/21.jpg)
21
The Actual Pop(cont.)
125 25 200 70
elements
0 1 2 3 top
and top is decremented…
client
![Page 22: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/22.jpg)
22
The Actual Pop(cont.)
125 25 200 70
elements
0 1 2 3 top
and top is decremented…
client
![Page 23: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/23.jpg)
23
The Actual 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 24: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/24.jpg)
24
The Actual Push(cont.)
125 25 200 70
elements
0 1 2 3 top
First, top is incremented…
![Page 25: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/25.jpg)
25
The Actual Push(cont.)
125 25 200 70
elements
0 1 2 3 top
First, top is incremented…
![Page 26: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/26.jpg)
26
The Actual Push(cont.)
125 25 200 70
elements
0 1 2 3 top
Then, 63 is pushed into that position…
![Page 27: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/27.jpg)
27
The Actual Push(cont.)
125 25 200 63
elements
0 1 2 3 top
Then, 63 is pushed into that position…
![Page 28: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/28.jpg)
28
1 template <class DataType>2 Stack<DataType>::Stack( )3 : elements( 2 ), top( -1 )4 {5 }
Stack Constructor
![Page 29: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/29.jpg)
29
Shift Operators
• Shift operators are used in the array implementations of data structures
• They are appropriate when multiplying or dividing by powers of two
• They are faster than multiplication and division• Assume num is a positive integer.• num << n is the same as num * 2n
• num >> n is the same as num / 2n (using integer division)
![Page 30: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/30.jpg)
30
Push Code
6 template <class DataType>7 void Stack<DataType>::push( 8 DataType elementToPush )9 {10 if ( ++top == elements.length( ) )11 elements.changeSize( elements.length( ) << 1 ); 12 elements[ top ] = elementToPush;13 }
![Page 31: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/31.jpg)
31
Pop
• Recall…to conserve memory, if the number of used elements of an array drops to 25% of the capacity, we want to cut the capacity of the array in half
• On each pop, it is possible that we may want to reduce the size of the array
![Page 32: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/32.jpg)
32
Reducing Array Size
• When we call the changeSize function for the Array object, it is possible that there is not enough heap memory to reduce the array size
• A new, smaller dynamic array needs to be created to copy the elements of the old, larger dynamic array to it
• If the smaller array cannot be created the pop function should still succeed (stack is still completely functional)
![Page 33: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/33.jpg)
33
Reducing Array Size (cont.)
• If array size reduction does not succeed, it might succeed later on– Other dynamic memory may be freed– The number of used elements in the stack may drop
so low that a small dynamic array can be made
• Therefore, in the pop function, we should try to reduce the size to the smallest power of 2, which is:– At least twice the number of elements being used– At least 2
![Page 34: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/34.jpg)
34
Example
• Suppose there are 3 elements being used in a stack.
• top would be 2, so top + 1 gives the number of elements
• Suppose also that the capacity of the array is 32.
• The array’s capacity should be reduced to 8 (one quarter of the capacity instead of one half)
![Page 35: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/35.jpg)
35
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize:
![Page 36: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/36.jpg)
36
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize:
![Page 37: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/37.jpg)
37
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 32
![Page 38: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/38.jpg)
38
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 32
![Page 39: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/39.jpg)
39
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 32Number of elements: 3
![Page 40: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/40.jpg)
40
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 32Number of elements: 3
![Page 41: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/41.jpg)
41
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 32Number of elements: 3
trysize / 4 : 8
![Page 42: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/42.jpg)
42
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 32Number of elements: 3
trysize / 4 : 8
TRUE
![Page 43: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/43.jpg)
43
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 32Number of elements: 3
trysize / 4 : 8
TRUE
![Page 44: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/44.jpg)
44
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 32Number of elements: 3
trysize / 4 : 8
TRUE
TRUE
![Page 45: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/45.jpg)
45
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 32
![Page 46: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/46.jpg)
46
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 16
![Page 47: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/47.jpg)
47
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 16
![Page 48: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/48.jpg)
48
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 16
3 <= 4 : TRUE
![Page 49: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/49.jpg)
49
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 16
3 <= 4 : TRUE
![Page 50: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/50.jpg)
50
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 16
3 <= 4 : TRUETRUE
![Page 51: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/51.jpg)
51
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 16
![Page 52: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/52.jpg)
52
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 8
![Page 53: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/53.jpg)
53
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 8
![Page 54: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/54.jpg)
54
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 8
3 <= 2 : FALSE
![Page 55: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/55.jpg)
55
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 8
![Page 56: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/56.jpg)
56
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 8
trysize will be the new capacity that we attempt to use (might be out of heap memory)
![Page 57: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/57.jpg)
57
Example (cont.)
int trysize = elements.length( );while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
top: 2
capacity: 32
trysize: 8
If trysize would become 2, this part would be false, causing 2 to be the smallest possible capacity
![Page 58: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/58.jpg)
58
14 template <class DataType>15 bool Stack<DataType>::pop( 16 DataType & poppedElement )17 {18 if ( top == -1 ) 19 return false;20 poppedElement = elements[ top ];21 top--;22 int trysize = elements.length( );23 while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )24 trysize >>= 1;
Pop Code
Pop code continued…
![Page 59: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/59.jpg)
59
Pop Code (cont.)
25 if ( trysize < elements.length( ) ) {26 try {27 elements.changeSize( trysize );28 }29 catch( … ) { }30 }3132 return true;33 }
![Page 60: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/60.jpg)
60
Peek
34 // returns the element at the top of the stack in 35 // topElement without removing it. Returns false if 36 // called on an empty stack; otherwise, returns true37 template <class DataType>38 bool Stack<DataType>::peek( 39 DataType & topElement )40 {41 if ( top == -1 ) 42 return false;43 topElement = elements[ top ];44 return true;45 }
![Page 61: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/61.jpg)
61
46 template <class DataType>47 bool Stack<DataType>::isEmpty( ) const48 {49 return top == -1;50 }
isEmpty
![Page 62: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/62.jpg)
62
makeEmpty
51 template <class DataType>52 void Stack<DataType>::makeEmpty( )53 {54 top = -1;55 try {56 elements.changeSize( 2 );57 }58 catch( … ) { }59 }
![Page 63: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/63.jpg)
63
Linked-List Stack
• Stacks can also be implemented with a linked list
• The front node is the top of the stack
![Page 64: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/64.jpg)
64
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…
![Page 65: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/65.jpg)
65
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…
![Page 66: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/66.jpg)
66
Linked-List Stack(cont.)
top
To push, we place the new element in a node and insert it at the front of the linked list…
![Page 67: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/67.jpg)
67
Linked-List Stack(cont.)
To push, we place a new element in a node and insert it at the front of the linked list…
top
![Page 68: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/68.jpg)
68
The Queue ADT
• The queue is a data structure that is like a line of people, except that it is a line of elements
• The line of elements is the data upon which operations are performed
![Page 69: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/69.jpg)
69
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 70: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/70.jpg)
70
Queue
• A queue 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• A queue is a FIFO (first-in, first-out) data
structure• It is used in situations where a fair first-
come, first-serve basis is called for, like a print queue
![Page 71: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/71.jpg)
71
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 72: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/72.jpg)
72
Header Node
• A header node can simplify the code for a linked-list queue
• We make use of a header node in the queue implementation
![Page 73: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/73.jpg)
73
Dequeue Operation
front back
header
![Page 74: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/74.jpg)
74
Dequeue Operation(cont.)
front back
header
![Page 75: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/75.jpg)
75
Enqueue Operation
front back
header
![Page 76: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/76.jpg)
76
Enqueue Operation(cont.)
front back
header
![Page 77: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/77.jpg)
77
1 // queue.h -- class template for the linked list 2 // implementation of a queue3 // note: use of the copy constructor, overloaded 4 // assignment operator, or enqueue function can cause an 5 // exception to be thrown when heap memory is exhausted67 template <class DataType>8 struct Node {9 DataType info;10 Node<DataType> *next;11 };
Queue Specification File
Specification File continued…
![Page 78: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/78.jpg)
78
Queue Specification File (cont.)
12 template <class DataType>13 class Queue14 {15 public:16 Queue( );17 Queue( const Queue<DataType> & apqueue ); 18 ~Queue( );19 Queue<DataType> & operator =( 20 const Queue<DataType> & rqueue );
public section of Queue continued…
![Page 79: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/79.jpg)
79
Queue Specification File (cont.)
21 void enqueue( const DataType & element );22 bool dequeue( DataType & deqElement );23 bool peek( DataType & frontElement ); 24 bool isEmpty( ) const;25 void makeEmpty( );
private section of Queue is next…
![Page 80: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/80.jpg)
80
Queue Specification File (cont.)
26 private:27 Node<DataType> *front;28 Node<DataType> *back;29 Node<DataType> header;30 inline void deepCopy( 31 const Queue<DataType> & original );32 };3334 #include "queue.cpp"
![Page 81: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/81.jpg)
81
1 // queue.cpp 23 template <class DataType>4 Queue<DataType>::Queue( )5 {6 front = back = &header;7 }
Queue Constructor
![Page 82: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/82.jpg)
82
Queue Copy Constructor and Destructor
8 template <class DataType>9 Queue<DataType>::Queue( 10 const Queue<DataType> & apqueue )11 {12 deepCopy( apqueue );13 }1415 template <class DataType>16 Queue<DataType>::~Queue( )17 {18 makeEmpty( );19 }
![Page 83: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/83.jpg)
83
Queue OverloadedAssignment Operator
20 template <class DataType>21 Queue<DataType> & Queue<DataType>::22 operator =( const Queue<DataType> & rqueue )23 {24 if ( this == &rqueue )25 return *this;26 makeEmpty( );27 deepCopy( rqueue );28 return *this;29 }
![Page 84: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/84.jpg)
84
Enqueue
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
This section creates the new node to enqueue and places element within in it…
![Page 85: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/85.jpg)
85
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
This section creates the new node to enqueue and places element within in it…
ptr
![Page 86: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/86.jpg)
86
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Let’s consider a couple of cases with these next two lines.
ptr
![Page 87: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/87.jpg)
87
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Let’s consider a couple of cases with these next two lines.
ptr
![Page 88: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/88.jpg)
88
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Case 1: The queue is initially empty.
ptr
![Page 89: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/89.jpg)
89
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Case 1: The queue is initially empty.
front
ptr
header
back
![Page 90: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/90.jpg)
90
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Case 1: The queue is initially empty.
front
ptr
header
back
![Page 91: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/91.jpg)
91
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Case 1: The queue is initially empty.
front
ptr
header
back
![Page 92: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/92.jpg)
92
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Case 1: The queue is initially empty.
front
ptr
header
back
![Page 93: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/93.jpg)
93
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Case 1: The queue is initially empty.
front
ptr
headerback
![Page 94: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/94.jpg)
94
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Case 2: The queue has nodes.
ptr
![Page 95: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/95.jpg)
95
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Case 2: The queue has nodes.
ptr
front
header
back
![Page 96: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/96.jpg)
96
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Case 2: The queue has nodes.
ptr
front
header
back
![Page 97: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/97.jpg)
97
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Case 2: The queue has nodes.
ptr
front
header
back
![Page 98: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/98.jpg)
98
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Case 2: The queue has nodes.
ptr
front
header
back
![Page 99: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/99.jpg)
99
Enqueue (cont.)
30 template <class DataType>31 void Queue<DataType>::enqueue( 32 const DataType & element )33 {34 Node<DataType> *ptr = new Node<DataType>;35 ptr->info = element;36 back->next = ptr;37 back = ptr;38 }
Case 2: The queue has nodes.
ptr
front
headerback
![Page 100: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/100.jpg)
100
Dequeue
39 template <class DataType>40 bool Queue<DataType>::dequeue( 41 DataType & deqElement )42 {43 if ( front == back ) 44 return false;
Dequeue continued…
Returns false if client tries to dequeue an empty queue.
![Page 101: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/101.jpg)
101
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
![Page 102: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/102.jpg)
102
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
![Page 103: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/103.jpg)
103
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
![Page 104: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/104.jpg)
104
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
![Page 105: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/105.jpg)
105
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
deqElement:
passed in by reference
![Page 106: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/106.jpg)
106
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
deqElement:
![Page 107: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/107.jpg)
107
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
deqElement:
![Page 108: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/108.jpg)
108
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
deqElement:
![Page 109: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/109.jpg)
109
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
deqElement:
![Page 110: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/110.jpg)
110
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
deqElement:
![Page 111: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/111.jpg)
111
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
Let’s consider what happens if only one node is left to dequeue.
![Page 112: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/112.jpg)
112
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
Let’s consider what happens if only one node is left to dequeue.
![Page 113: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/113.jpg)
113
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
![Page 114: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/114.jpg)
114
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
![Page 115: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/115.jpg)
115
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
![Page 116: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/116.jpg)
116
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
deqElement:
![Page 117: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/117.jpg)
117
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
deqElement:
![Page 118: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/118.jpg)
118
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
deqElement:
![Page 119: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/119.jpg)
119
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
deqElement:
![Page 120: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/120.jpg)
120
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
backheader
ptr
deqElement:
![Page 121: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/121.jpg)
121
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
headerptr
deqElement:
back
![Page 122: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/122.jpg)
122
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
headerptr
deqElement:
back
![Page 123: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/123.jpg)
123
Dequeue (cont.)
45 Node<DataType> *ptr = front->next;46 deqElement = ptr->info;47 front->next = ptr->next;48 if ( back == ptr )49 back = front;50 delete ptr;5152 return true;53 }
front
headerptr
deqElement:
back
![Page 124: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/124.jpg)
124
Peek
54 template <class DataType>55 bool Queue<DataType>::peek( 56 DataType & frontElement )57 {58 if ( front == back )59 return false;60 frontElement = front->next->info;61 return true;62 }
![Page 125: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/125.jpg)
125
isEmpty and makeEmpty
63 template <class DataType>64 bool Queue<DataType>::isEmpty( ) const65 {66 return front == back;67 }6869 template <class DataType>70 void Queue<DataType>::makeEmpty( )71 {72 DataType temp;73 while ( dequeue( temp ) );74 }
![Page 126: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/126.jpg)
126
deepCopy75 template <class DataType>76 inline void Queue<DataType>::deepCopy( 77 const Queue<DataType> & original )78 {79 Node<DataType> *copyptr = front = &header; 80 Node<DataType> *originalptr = original.front;
front back
Original Copy
header header
![Page 127: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/127.jpg)
127
deepCopy75 template <class DataType>76 inline void Queue<DataType>::deepCopy( 77 const Queue<DataType> & original )78 {79 Node<DataType> *copyptr = front = &header; 80 Node<DataType> *originalptr = original.front;
front back
Original Copy
header header
![Page 128: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/128.jpg)
128
deepCopy75 template <class DataType>76 inline void Queue<DataType>::deepCopy( 77 const Queue<DataType> & original )78 {79 Node<DataType> *copyptr = front = &header; 80 Node<DataType> *originalptr = original.front;
front back
Original Copy
header header
frontcopyptr
![Page 129: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/129.jpg)
129
deepCopy75 template <class DataType>76 inline void Queue<DataType>::deepCopy( 77 const Queue<DataType> & original )78 {79 Node<DataType> *copyptr = front = &header; 80 Node<DataType> *originalptr = original.front;
front back
Original Copy
header header
frontcopyptr
![Page 130: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/130.jpg)
130
deepCopy75 template <class DataType>76 inline void Queue<DataType>::deepCopy( 77 const Queue<DataType> & original )78 {79 Node<DataType> *copyptr = front = &header; 80 Node<DataType> *originalptr = original.front;
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 131: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/131.jpg)
131
deepCopy75 template <class DataType>76 inline void Queue<DataType>::deepCopy( 77 const Queue<DataType> & original )78 {79 Node<DataType> *copyptr = front = &header; 80 Node<DataType> *originalptr = original.front;
front back
Original Copy
header header
frontoriginalptr copyptr
deepCopy function continued…
![Page 132: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/132.jpg)
132
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 133: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/133.jpg)
133
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 134: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/134.jpg)
134
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 135: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/135.jpg)
135
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 136: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/136.jpg)
136
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 137: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/137.jpg)
137
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 138: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/138.jpg)
138
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 139: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/139.jpg)
139
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 140: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/140.jpg)
140
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 141: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/141.jpg)
141
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 142: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/142.jpg)
142
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 143: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/143.jpg)
143
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 144: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/144.jpg)
144
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 145: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/145.jpg)
145
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 146: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/146.jpg)
146
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 147: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/147.jpg)
147
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 148: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/148.jpg)
148
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 149: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/149.jpg)
149
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 150: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/150.jpg)
150
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 151: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/151.jpg)
151
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 152: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/152.jpg)
152
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 153: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/153.jpg)
153
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 154: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/154.jpg)
154
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 155: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/155.jpg)
155
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 156: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/156.jpg)
156
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 157: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/157.jpg)
157
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 158: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/158.jpg)
158
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 159: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/159.jpg)
159
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 160: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/160.jpg)
160
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
![Page 161: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/161.jpg)
161
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
back
![Page 162: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/162.jpg)
162
deepCopy (cont.)81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
front back
Original Copy
header header
frontoriginalptr copyptr
back
Let’s consider the empty case…
![Page 163: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/163.jpg)
163
deepCopy (cont.)75 template <class DataType>76 inline void Queue<DataType>::deepCopy( 77 const Queue<DataType> & original )78 {79 Node<DataType> *copyptr = front = &header; 80 Node<DataType> *originalptr = original.front;
front back
Original Copy
header header
![Page 164: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/164.jpg)
164
deepCopy (cont.)75 template <class DataType>76 inline void Queue<DataType>::deepCopy( 77 const Queue<DataType> & original )78 {79 Node<DataType> *copyptr = front = &header; 80 Node<DataType> *originalptr = original.front;
front back
Original
header
Copy
header
frontcopyptr
![Page 165: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/165.jpg)
165
deepCopy (cont.)75 template <class DataType>76 inline void Queue<DataType>::deepCopy( 77 const Queue<DataType> & original )78 {79 Node<DataType> *copyptr = front = &header; 80 Node<DataType> *originalptr = original.front;
front back
Original
header
Copy
header
frontcopyptr
![Page 166: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/166.jpg)
166
deepCopy (cont.)75 template <class DataType>76 inline void Queue<DataType>::deepCopy( 77 const Queue<DataType> & original )78 {79 Node<DataType> *copyptr = front = &header; 80 Node<DataType> *originalptr = original.front;
front back
Original
header
Copy
header
frontcopyptr
originalptr
![Page 167: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/167.jpg)
167
deepCopy (cont.)75 template <class DataType>76 inline void Queue<DataType>::deepCopy( 77 const Queue<DataType> & original )78 {79 Node<DataType> *copyptr = front = &header; 80 Node<DataType> *originalptr = original.front;
front back
Original
header
Copy
header
frontcopyptr
originalptr
![Page 168: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/168.jpg)
168
deepCopy (cont.)
front back
Original
header
Copy
header
frontcopyptr
originalptr
81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
![Page 169: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/169.jpg)
169
deepCopy (cont.)
front back
Original
header
Copy
header
frontcopyptr
originalptr
81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
![Page 170: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/170.jpg)
170
deepCopy (cont.)
front back
Original
header
Copy
header
frontcopyptr
originalptr
81 while ( originalptr != original.back ) {82 originalptr = originalptr->next;83 copyptr->next = new Node<DataType>;84 copyptr = copyptr->next;85 copyptr->info = originalptr->info;86 }87 back = copyptr;88 }
back
![Page 171: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/171.jpg)
171
Array Implementation of a Queue
• Similar to the linked-list queue, there are two data members 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 172: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/172.jpg)
172
Enqueue / Dequeue
0 1 2 3 4 5 6 7
front back
![Page 173: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/173.jpg)
173
0 1 2 3 4 5 6 7
front back
DEQUEUE
Enqueue / Dequeue(cont.)
![Page 174: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/174.jpg)
174
0 1 2 3 4 5 6 7
front back
DEQUEUE
Enqueue / Dequeue(cont.)
![Page 175: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/175.jpg)
175
0 1 2 3 4 5 6 7
front back
DEQUEUE
Enqueue / Dequeue(cont.)
![Page 176: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/176.jpg)
176
0 1 2 3 4 5 6 7
front back
DEQUEUE
Enqueue / Dequeue(cont.)
![Page 177: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/177.jpg)
177
0 1 2 3 4 5 6 7
front back
ENQUEUE
Enqueue / Dequeue(cont.)
![Page 178: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/178.jpg)
178
0 1 2 3 4 5 6 7
front back
ENQUEUE
Enqueue / Dequeue(cont.)
![Page 179: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/179.jpg)
179
0 1 2 3 4 5 6 7
front back
ENQUEUE
Enqueue / Dequeue(cont.)
![Page 180: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/180.jpg)
180
0 1 2 3 4 5 6 7
front back
ENQUEUE
Enqueue / Dequeue(cont.)
![Page 181: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/181.jpg)
181
0 1 2 3 4 5 6 7
front back
DEQUEUE
Enqueue / Dequeue(cont.)
![Page 182: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/182.jpg)
182
0 1 2 3 4 5 6 7
front back
DEQUEUE
Enqueue / Dequeue(cont.)
![Page 183: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/183.jpg)
183
0 1 2 3 4 5 6 7
front back
DEQUEUE
Enqueue / Dequeue(cont.)
![Page 184: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/184.jpg)
184
0 1 2 3 4 5 6 7
front back
DEQUEUE
Enqueue / Dequeue(cont.)
![Page 185: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/185.jpg)
185
0 1 2 3 4 5 6 7
front back
ENQUEUE
Enqueue / Dequeue(cont.)
![Page 186: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/186.jpg)
186
0 1 2 3 4 5 6 7
front back
ENQUEUE
Enqueue / Dequeue(cont.)
![Page 187: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/187.jpg)
187
0 1 2 3 4 5 6 7
front back
DEQUEUE
Enqueue / Dequeue(cont.)
![Page 188: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/188.jpg)
188
0 1 2 3 4 5 6 7
front back
DEQUEUE
Enqueue / Dequeue(cont.)
![Page 189: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/189.jpg)
189
0 1 2 3 4 5 6 7
front back
DEQUEUE
Enqueue / Dequeue(cont.)
![Page 190: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/190.jpg)
190
0 1 2 3 4 5 6 7
front back
ENQUEUE
Enqueue / Dequeue(cont.)
![Page 191: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/191.jpg)
191
0 1 2 3 4 5 6 7
front back
ENQUEUE
?
Enqueue / Dequeue(cont.)
![Page 192: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/192.jpg)
192
0 1 2 3 4 5 6 7
front back
ENQUEUE
Enqueue / Dequeue(cont.)
We could double the size of the array here.
![Page 193: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/193.jpg)
193
0 1 2 3 4 5 6 7
front back
ENQUEUE
Enqueue / Dequeue(cont.)
But if we keep doing this, we may have a million elements in the Array, but only a few at the end are used!
![Page 194: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/194.jpg)
194
0 1 2 3 4 5 6 7
front back
ENQUEUE
Enqueue / Dequeue(cont.)
We handle this problem by having the back wrap around to the beginning of the array.
![Page 195: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/195.jpg)
195
0 1 2 3 4 5 6 7
frontback
ENQUEUE
Enqueue / Dequeue(cont.)
![Page 196: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/196.jpg)
196
0 1 2 3 4 5 6 7
frontback
The front also wraps to the beginning when it reaches the end of the array
Enqueue / Dequeue(cont.)
![Page 197: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/197.jpg)
197
How Do We Know When the Array is Full?
• We may still need to double the capacity of the array if it gets filled
• An array will be full when– back + 1 == front– OR– back + 1 == capacity AND front == 0
![Page 198: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/198.jpg)
198
A Full Array
0 1 2 3 4 5 6 7
frontback
![Page 199: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/199.jpg)
199
A Full Array
0 1 2 3 4 5 6 7
frontback
If the next operation is ENQUEUE, the array capacity will need to be doubled
![Page 200: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/200.jpg)
200
The Queue ClassTemplate
1 #include "Array.h"23 template <class DataType>4 class Queue5 {6 public:7 Queue( );8 void enqueue( DataType element );9 bool dequeue( DataType & deqElement ); 10 bool peek( DataType & frontElement );11 bool isEmpty( ) const;12 void makeEmpty( );
![Page 201: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/201.jpg)
201
The Queue ClassTemplate (cont.)
13 private:14 Array<DataType> elements;15 int front;16 int back;17 };1819 #include "queue.cpp"
![Page 202: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/202.jpg)
202
Queue Constructor
1 // queue.cpp 2 template <class DataType>3 Queue<DataType>::Queue( )4 : elements( 2 ), front( -1 ), back( -1 )5 {6 }
![Page 203: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/203.jpg)
203
Enqueue7 template <class DataType>8 void Queue<DataType>::enqueue( DataType element )9 {10 if ( back + 1 == front || 11 ( back == elements.length( ) - 1 && !front ) ) {12 elements.changeSize( elements.length( ) << 1 );13 // if front end was last part of array, readjust14 if ( back < front ) {15 int i = elements.length( ) - 1;16 for ( int j = ((i + 1) >> 1) - 1; j >= front; i--, j-- )17 elements[ i ] = elements[ j ];18 front = i + 1;19 }20 }
Enqueue continued…
![Page 204: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/204.jpg)
204
Enqueue (cont.)
21 if ( back == -1 ) // queue is empty22 front = 0;23 back = (back == elements.length( ) - 1)? 24 0 : back + 1;25 elements[ back ] = element;26 }
![Page 205: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/205.jpg)
205
Dequeue
27 template <class DataType>28 bool Queue<DataType>::dequeue( 29 DataType & deqElement )30 {31 if (front == -1 )32 return false;
Dequeue continued…
![Page 206: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/206.jpg)
206
Dequeue (cont.)
33 deqElement = elements[ front ];34 if ( front == back ) // only one element was in queue35 front = back = -1;36 else37 front = (front == elements.length( ) - 1)? 38 0 : front + 1;
Dequeue continued…
![Page 207: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/207.jpg)
207
Dequeue (cont.)
39 // try to reduce the size of the array40 int trysize = elements.length( );41 int numElements = (front <= back)? 42 back - front + 1 : back + trysize - front + 1;43 while ( ( numElements <= trysize >> 2 ) && trysize > 2 ) 44 trysize >>= 1;
Dequeue continued…
![Page 208: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/208.jpg)
208
Dequeue (cont.)
45 if ( trysize < elements.length( ) ) {46 // readjust so we won't lose elements when 47 // shrinking the array size48 int i, j;49 if ( front > back ) {
If true, we can try to change the size of the array…
The code for the “front > back” case is shown next…
![Page 209: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/209.jpg)
209
Dequeue (cont.)
50 for ( i = trysize - 1, j = elements.length( ) - 1; j >= front; i--, j-- )51 elements[ i ] = elements[ j ];52 front = i + 1;53 try {54 elements.changeSize( trysize );55 }56 catch( … ) {57 for ( i = elements.length( ) - 1, j = trysize - 1; j >= front; i--, j-- )58 elements[ i ] = elements[ j ];59 front = i + 1;60 }
If we can’t change the size, we’ll have to undo the realignment on lines 50-52.
![Page 210: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/210.jpg)
210
Dequeue (cont.)
61 return true;62 }63 else if ( front <= back && back >= trysize ) {64 for ( i = 0, j = front; j <= back; i++, j++ )65 elements[ i ] = elements[ j ];66 front = 0;67 back = i - 1;68 }
We need to return for the “front > back” case
We won’t need to undo this realignment if we can’t change the size.
We’re ready for the changeSize attempt…
![Page 211: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/211.jpg)
211
Dequeue (cont.)
69 try {70 elements.changeSize( trysize );71 }72 catch( … ) { }73 }7475 return true;76 }
This changeSize attempt handles the “front <= back && back >= trysize” case AND the case where no realignment is necessary:
“front <= back && back < trysize”
![Page 212: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/212.jpg)
212
Peek
77 template <class DataType>78 bool Queue<DataType>::peek( 79 DataType & frontElement )80 {81 if (front == -1 )82 return false;83 frontElement = elements[ front ];84 return true;85 }
![Page 213: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/213.jpg)
213
isEmpty
86 template <class DataType>87 bool Queue<DataType>::isEmpty( ) const88 {89 return front == -1;90 }
![Page 214: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649e4c5503460f94b41315/html5/thumbnails/214.jpg)
214
makeEmpty
91 template <class DataType>92 void Queue<DataType>::makeEmpty( )93 {94 front = back = -1;95 try {96 elements.changeSize( 2 );97 }98 catch( … ) { }99 }