SlideShare a Scribd company logo
1 of 102
1
C++ Programming: From Problem Analysis to Program Design, Eighth Edition
Chapter 18
Stacks and Queues
2
Objectives (1 of 2)
• In this chapter, you will:
• Learn about stacks
• Examine various stack operations
• Learn how to implement a stack as an array
• Learn how to implement a stack as a linked list
• Learn about infix, prefix, and postfix expressions, and how to use a stack to evaluate
postfix expressions
C© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or
service or otherwise on a password-protected website for classroom
++ Programming: From Problem Analysis to Program Design, Seventh Edition
2
3
Objectives (2 of 2)
• Learn how to use a stack to remove recursion
• Learn about queues
• Examine various queue operations
• Learn how to implement a queue as an array
• Learn how to implement a queue as a linked list
• Discover how to use queues to solve simulation problems
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
3
4
Stacks (1 of 4)
• Stack: a data structure in which elements are added and removed from one end
only
• Addition/deletion occur only at the top of the stack
• Last in first out (LIFO) data structure
• Operations:
• Push: to add an element onto the stack
• Pop: to remove an element from the stack
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
4
5
Stacks (2 of 4)
FIGURE 18-1 Various types of stacks
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
5
6
Stacks (3 of 4)
FIGURE 18-2 Empty stack
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
6
7
Stacks (4 of 4)
FIGURE 18-3 Stack operations
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
7
8
Stack Operations
• In the abstract class stackADT:
• initializeStack
• isEmptyStack
• isFullStack
• push
• top
• pop
FIGURE 18-4 UML class diagram of the class stackADT
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
8
9
Implementation of Stacks as Arrays (1 of 5)
• First element goes in first array position, second in the second position, etc.
• Top of the stack is index of the last element added to the stack
• Stack elements are stored in an array, which is a random access data structure
• Stack element is accessed only through top
• To track the top position, use a variable called stackTop
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
9
10
Implementation of Stacks as Arrays (2 of 5)
• Can dynamically allocate array
• Enables user to specify size of the array
• class stackType implements the functions of the abstract class
stackADT
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
10
11
Implementation of Stacks as Arrays (3 of 5)
FIGURE 18-5 UML class diagram of the class stackType
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
11
12
Implementation of Stacks as Arrays (4 of 5)
• C++ arrays begin with the index 0
• Must distinguish between:
- Value of stackTop
- Array position indicated by stackTop
• If stackTop is 0, stack is empty
• If stackTop is nonzero, stack is not empty
• Top element is given by stackTop - 1
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
12
13
Implementation of Stacks as Arrays (5 of 5)
FIGURE 18-6 Example of a stack
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
13
14
Initialize Stack
FIGURE 18-7 Empty stack
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
14
15
Empty Stack/Full Stack
• Stack is empty if stackTop = 0
• Stack is full if stackTop = maxStackSize
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
15
16
Push (1 of 3)
• Store the newItem in the array component indicated by stackTop
• Increment stackTop
• Overflow occurs if we try to add a new item to a full stack
C++ Progra© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain
product or service or otherwise on a password-protected website for classroom
mming: From Problem Analysis to Program Design, Seventh Edition
16
17
Push (2 of 3)
FIGURE 18-8 Stack before pushing y
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
17
18
Push (3 of 3)
FIGURE 18-9 Stack after pushing y
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
18
19
Return the Top Element
• top operation:
• Returns the top element of the stack
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
19
20
Pop (1 of 3)
• To remove an element from the stack, decrement stackTop by 1
• Underflow condition: trying to remove an item from an empty stack
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
20
21
Pop (2 of 3)
FIGURE 18-10 Stack before popping D
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
21
22
Pop (3 of 3)
FIGURE 18-11 Stack after popping D
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
22
23
Copy Stack
• copyStack function: copies a stack
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
23
24
Constructor and Destructor
• Constructor:
• Sets stack size to parameter value (or default value if not specified)
• Sets stackTop to 0
• Creates array to store stack elements
• Destructor:
• Deallocates memory occupied by the array
• Sets stackTop to 0
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
24
25
Copy Constructor
• Copy constructor:
• Called when a stack object is passed as a (value) parameter to a function
• Copies values of member variables from actual parameter to formal parameter
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
25
26
Overloading the Assignment Operator (=) (1 of 2)
• Assignment operator must be explicitly overloaded because of pointer member
variables
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
26
27
Stack Header File
• Place definitions of class and functions (stack operations) together in a file
• Called myStack.h
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
27
28
Linked Implementation of Stacks (1 of 2)
• Array only allows fixed number of elements
• If number of elements to be pushed exceeds array size, the program may
terminate
• Linked lists can dynamically organize data
• In a linked representation, stackTop is pointer to memory address of top
element in stack
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
28
29
Linked Implementation of Stacks (2 of 2)
FIGURE 18-12 Empty and nonempty linked stack
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
29
30
Default Constructor
• Initializes the stack to an empty state when a stack object is declared
– Sets stackTop to nullptr
template <class Type>
linkedStackType<Type>::linkedStackType()
{
stackTop = nullptr;
}
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
30
31
Empty Stack and Full Stack
• In a linked implementation of stacks, function isFullStack does not apply
• Logically, the stack is never full
• Stack is empty if stackTop is nullptr
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
31
32
Linked Stack: Initialize Stack
• initializeStack: reinitializes stack to an empty state
• Must deallocate memory occupied by current element
• Sets stackTop to nullptr
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
32
33
Push (1 of 2)
• newNode is added at the beginning of the linked list pointed to by stackTop
FIGURE 18-13 Stack before the push operation
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
33
34
Push (2 of 2)
FIGURE 18-14 Push operation
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
34
35
Linked Stack: Return the Top Element
template <class Type>
Type linkedStackType<Type>::top() const
{
assert(stackTop != nullptr); //if stack is empty,
//terminate the program
return stackTop->info; //return the top element
}//end top
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
35
36
Pop (1 of 2)
• Node pointed to by stackTop is removed
• Second element becomes top element
FIGURE 18-15 Stack before the pop operation
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
36
37
Pop (2 of 2)
FIGURE 18-16 Pop operation
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
37
38
Linked Stack: Copy Stack
• copyStack function: makes an identical copy of a stack
• Similar definition to copyList for linked lists
C++ Pro© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product
or service or otherwise on a password-protected website for classroom
gramming: From Problem Analysis to Program Design, Seventh Edition
38
39
Constructors and Destructors
• Copy constructor and destructor:
• Similar to those for linked lists
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
39
40
Overloading the Assignment Operator (=) (2 of 2)
• Overloading the assignment operator:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
40
41
Stack as Derived from the class unorderedLinkedList
• Implementation of push is similar to insertFirst for general lists
• Other similar functions:
• initializeStack and initializeList
• isEmptyList and isEmptyStack
• linkedStackType can be derived from linkedListType
• class linkedListType is abstract
• unorderedLinkedListType is derived from linkedListType
• Provides the definitions of the abstract functions of the class linkedListType
• linkedStackType is derived from unorderedLinkedListType
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
41
42
Application of Stacks: Postfix Expressions Calculator (1 of 8)
• Infix notation: usual notation for writing arithmetic expressions
• Operator is written between the operands
• Example: a + b
• Evaluates from left to right
• Operators have precedence
- Parentheses can be used to override precedence
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
42
43
Application of Stacks: Postfix Expressions Calculator (2 of 8)
• Prefix (Polish) notation: operators are written before the operands
• Introduced by the Polish mathematician Jan Lukasiewicz in early 1920s
• Parentheses can be omitted
• Example: + a b
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
43
44
Application of Stacks: Postfix Expressions Calculator (3 of 8)
• Reverse Polish notation: operators follow the operands (postfix operators)
• Proposed by Australian philosopher and early computer scientist Charles L. Hamblin in
the late 1950s
• Advantage: operators appear in the order required for computation
• Example: a + b * c becomes a b c * +
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
44
45
Application of Stacks: Postfix Expressions Calculator (4 of 8)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
45
46
Application of Stacks: Postfix Expressions Calculator (5 of 8)
• Postfix notation has important applications in computer science
• Many compilers first translate arithmetic expressions into postfix notation and then
translate this expression into machine code
• Evaluation algorithm:
• Scan expression from left to right
• When an operator is found, back up to get operands, perform the operation, and
continue
C© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or
service or otherwise on a password-protected website for classroom
+ Programming: From Problem Analysis to Program Design, Seventh Edition
46
47
Application of Stacks: Postfix Expressions Calculator (6 of 8)
FIGURE 18-17 Evaluating the postfix expression: 6 3 + 2 * =
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
47
48
Application of Stacks: Postfix Expressions Calculator (7 of 8)
• Symbols can be numbers or anything else:
• +, -, *, and / are operators, require two operands
- Pop stack twice and evaluate expression
- If stack has less than two elements  error
• If symbol is =, expression ends
- Pop and print answer from stack
- If stack has more than one element  error
• If symbol is anything else
- Expression contains an illegal operator
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
48
49
Application of Stacks: Postfix Expressions Calculator (8 of 8)
• Assume postfix expressions are in this form:
#6 #3 + #2 * =
• If symbol scanned is #, next input is a number
• If the symbol scanned is not #, then it is:
- An operator (may be illegal) or
- An equal sign (end of expression)
• Assume expressions contain only +, -, *, and / operators
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
49
50
Main Algorithm
• Pseudocode:
• Four functions are needed:
• evaluateExpression, evaluateOpr, discardExp, and printResult
C+© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or
service or otherwise on a password-protected website for classroom
+ Programming: From Problem Analysis to Program Design, Seventh Edition
50
51
Function evaluateExpression
• Function evaluateExpression:
• Evaluates each postfix expression
• Each expression ends with = symbol
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
51
52
Function evaluateOpr
• Function evaluateOpr:
• Evaluates an expression
• Needs two operands saved in the stack
- If less than two  error
• Also checks for illegal operations
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
52
53
Function discardExp
• Function discardExp:
• Called when an error is discovered in expression
• Reads and writes input data until the ‘=’
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
53
54
Function printResult
• The function printResult: If the postfix expression contains no errors, it
prints the result
• Otherwise, it outputs an appropriate message
• Result of the expression is in the stack, and output is sent to a file
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
54
55
• To print a list backward nonrecursively, first get to the last node of the list
• Problem: Links go in only one direction
• Solution: Save a pointer to each of node in a stack
- Uses the LIFO principle
• Since number of nodes is usually not known, use the linked implementation of
a stack
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
55
Nonrecursive Algorithm to Print a Linked List Backward (1 of 7)
56
Nonrecursive Algorithm to Print a Linked List Backward (2 of 7)
FIGURE 18-18 Linked list
FIGURE 18-19 List after the statement current = first; executes
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
ramming: From Problem Analysis to Program Design, Seventh Edition
56
57
Nonrecursive Algorithm to Print a Linked List Backward (3 of 7)
FIGURE 18-20 List and stack after the statements stack.push(current); and
current = current->link; execute
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
57
58
Nonrecursive Algorithm to Print a Linked List Backward (4 of 7)
FIGURE 18-21 List and stack after the statements stack.push(current); and
current = current->link; execute
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
58
59
Nonrecursive Algorithm to Print a Linked List Backward (5 of 7)
FIGURE 18-22 List and stack after the statements stack.push(current); and
current =current->link; execute
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
59
60
Nonrecursive Algorithm to Print a Linked List Backward (6 of 7)
FIGURE 18-23 List and stack after the statements current = stack.top(); and
stack. pop(); execute
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
60
61
Nonrecursive Algorithm to Print a Linked List Backward (7 of 7)
FIGURE 18-24 List and stack after the statements current=stack.top(); and
stack.pop(); execute
FIGURE 18-25 List and stack after the statements current=stack.top(); and
stack.pop(); execute
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
61
62
Queues
• Queue: set of elements of the same type
• Elements are:
• Added at one end (the back or rear)
• Deleted from the other end (the front)
• First In First Out (FIFO) data structure
• Middle elements are inaccessible
• Example:
• Waiting line in a bank
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
62
63
Queue Operations
• Queue operations include:
– initializeQueue
– isEmptyQueue
– isFullQueue
– front
– back
– addQueue
– deleteQueue
•Abstract class queueADT defines these operations
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
63
64
Implementation of Queues as Arrays (1 of 15)
• Need at least four (member) variables:
• Array to store queue elements
• queueFront and queueRear
- To track first and last elements
• maxQueueSize
- To specify maximum size of the queue
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
64
65
Implementation of Queues as Arrays (2 of 15)
• To add an element to the queue:
• Advance queueRear to next array position
• Add element to position pointed by queueRear
FIGURE 18-26 Queue after the first addQueue operation
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
65
66
Implementation of Queues as Arrays (3 of 15)
FIGURE 18-27 Queue after two more addQueue operations
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
66
67
Implementation of Queues as Arrays (4 of 15)
• To delete an element from the queue:
• Retrieve element pointed to by queueFront
• Advance queueFront to next queue element
FIGURE 18-28 Queue after the deleteQueue operation
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
67
68
Implementation of Queues as Arrays (5 of 15)
• Will this queue design work?
• Let A represent adding an element to the queue
• Let D represent deleting an element from the queue
• Consider the following sequence of operations:
- AAADADADADADADADA...
C++ Programming: From Problem © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom
Analysis to Program Design, Seventh Edition
68
69
Implementation of Queues as Arrays (6 of 15)
• This would eventually set queueRear to point to the last array position
• Giving the impression that the queue is full
FIGURE 18-29 Queue after the sequence of operations AAADADADADADA . . .
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
69
70
Implementation of Queues as Arrays (7 of 15)
• Solution 1: When queue overflows at rear (queueRear points to the last array
position):
• Check value of queueFront
• If queueFront indicates there is room at front of array, slide all queue elements
toward the first array position
• Problem: too slow for large queues
• Solution 2: Assume that the array is circular
C++ Program© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain
product or service or otherwise on a password-protected website for classroom
ming: From Problem Analysis to Program Design, Seventh Edition
71
Implementation of Queues as Arrays 8 of 15)
FIGURE 18-30 Circular queue
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
71
72
Implementation of Queues as Arrays (9 of 15)
FIGURE 18-31 Queue before and after the add operation
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
72
73
Implementation of Queues as Arrays (10 of 15)
• Deletion Case 1:
FIGURE 18-32 Queue before and after the delete operation
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
73
74
Implementation of Queues as Arrays (11 of 15)
• Deletion Case 2:
FIGURE 18-33 Queue before and after the add operation
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
74
75
Implementation of Queues as Arrays (12 of 15)
• Problem:
• Figures 18-32b and 18-33b have identical values for queueFront and queueRear
• However, Figure 18-32b represents an empty queue, whereas Figures 18-33b shows a
full queue
• Solution?
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
75
76
Implementation of Queues as Arrays (13 of 15)
• Solution 1: keep a count
• Incremented when a new element is added to the queue
• Decremented when an element is removed
• Initially set to 0
• Very useful if user (of queue) frequently needs to know the number of elements in
the queue
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
76
77
Implementation of Queues as Arrays (14 of 15)
• Solution 2: Let queueFront indicate index of array position preceding the first
element
• queueRear still indicates index of last one
• Queue empty if:
- queueFront == queueRear
• Slot indicated by queueFront is reserved
• Queue is full if next available space is the reserved slot indicated by queueFront
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
77
78
Implementation of Queues as Arrays (15 of 15)
FIGURE 18-34 Array to store the queue elements with a reserved slot
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
78
79
Empty Queue and Full Queue
• Queue is empty if count ==0
• Queue is full if count == maxQueueSize
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
79
80
Initialize Queue
• Initializes queue to empty state
• First element is added at first array position
• queueFront set to 0
• queueRear set to maxQueueSize -1
• count set to 0
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
80
FIGURE 18-35 Empty queue
81
Front and Back
• front operation: returns the first element of the queue
• If queue is empty, program terminates
• back operation: returns the last element of the queue
• If queue is empty, program terminates
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
81
82
addQueue
• addQueue operation:
• Advance queueRear to next array position
• Add new element to array position indicated by queueRear
• Increment count by 1
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
82
83
deleteQueue
• deleteQueue operation:
• Decrement count by 1
• Advance queueFront to next queue element
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
83
84
Queue: Constructors and Destructors
• Constructor:
• Sets maxQueueSize to user specification
• Creates array of size maxQueueSize (or default size)
• Initializes queueFront and queueRear to indicate empty queue
• Destructor:
• Deallocates memory occupied by array
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
84
85
Linked Implementation of Queues (1 of 2)
• Array implementation has issues:
• Array size is fixed: only a finite number of queue elements can be stored in it
• Requires array to be treated in a special way, together with queueFront and
queueRear
• Linked implementation of a queue simplifies many issues
• Queue is never full because memory is allocated dynamically
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
86
Linked Implementation of Queues (2 of 2)
• Elements are added at one end and removed from the other
• Need only two pointers to maintain the queue: queueFront and queueRear
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
86
87
Empty and Full Queue
• Queue is empty if queueFront is nullptr
• Queue is never full
• Unless the system runs out of memory
• Note: must provide isFullQueue function definition because it is an abstract
function in parent class queueADT
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
87
88
Linked Queue: Initialize Queue
• Initializes queue to an empty state
• Must remove all existing elements, if any
• Deallocates memory occupied by elements
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
88
89
addQueue, front, back, and deleteQueue operations
• addQueue operation: adds new element to end of queue
• front operation: returns first element of queue
• back operation: returns last element of queue
• deleteQueue operation: removes first element of queue
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
89
90
Linked Queue: Constructors and Destructors
• Constructor
• Accesses maxQueueSize, queueFront, and queueRear
• Destructor: destroys the queue
• Deallocates memory occupied by elements
• Copy constructor and overloading assignment operator:
• Similar to corresponding function for stack
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
90
91
Queue Derived from the class unorderedLinkedListType
• Linked implementation of queue: similar to implementation of a linked list
created in a forward manner
• addQueue similar to insertFirst
• initializeQueue is like initializeList
• isEmptyQueue similar to isEmptyList
• deleteQueue can be implemented as before
• queueFront is same as first
• queueRear is same as last
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
92
Application of Queues: Simulation
• Simulation: a technique in which one system models the behavior of another
system
• Computer models are used to study the behavior of real systems
• Queuing systems: computer simulations using queues as the data structure
• Queues of objects are waiting to be served
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
92
93
Designing a Queuing System (1 of 3)
• Server: object that provides the service
• Customer: object receiving the service
• Transaction time: service time, or the time it takes to serve a customer
• Model: system that consists of a list of servers and a waiting queue holding the
customers to be served
• Customer at front of queue waits for the next available server
C++ Programming: From © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website for classroom
Problem Analysis to Program Design, Seventh Edition
94
Designing a Queuing System (2 of 3)
• Need to know:
• Number of servers
• Expected arrival time of a customer
• Time between the arrivals of customers
• Number of events affecting the system
• Performance of system depends on:
• How many servers are available
• How long it takes to serve a customer
• How often a customer arrives
C++© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or
service or otherwise on a password-protected website for classroom
Programming: From Problem Analysis to Program Design, Seventh Edition
95
Designing a Queuing System (3 of 3)
• If it takes too long to serve a customer and customers arrive frequently, then
more servers are needed
• System can be modeled as a time-driven simulation
• Time-driven simulation: the clock is a counter
• The passage of one unit of time can be implemented by incrementing a counter by 1
• Simulation is run for a fixed amount of time
C++ Pro© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product
or service or otherwise on a password-protected website for classroom
gramming: From Problem Analysis to Program Design, Seventh Edition
96
Customer
FIGURE 18-36 UML class diagram of the class customerType
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
96
97
Server
FIGURE 18-37 UML class diagram of the class serverType
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
97
98
Server List
• Server list: a set of servers
• At any given time, a server is either free or busy
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
98
FIGURE 18-38 UML class diagram of the class serverListType
99
Waiting Customers Queue
• When customer arrives, he/she goes to end of queue
• When a server becomes available, customer at front of queue leaves to conduct
the transaction
• After each time unit, the waiting time of each customer in the queue is
incremented by 1
• Can use queueType but must add an operation to increment waiting time
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
99
100
Main Program
• Algorithm for main loop:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
100
101
Summary (1 of 2)
• Stack: items are added/deleted from one end
• Last In First Out (LIFO) data structure
• Operations: push, pop, initialize, destroy, check for empty/full stack
• Can be implemented as array or linked list
• Middle elements should not be accessed directly
• Postfix notation: operators are written after the operands (no parentheses
needed)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
101
102
Summary (2 of 2)
• Queue: items are added at one end and removed from the other end
• First In First Out (FIFO) data structure
• Operations: add, remove, initialize, destroy, check if queue is empty/full
• Can be implemented as array or linked list
• Middle elements should not be accessed directly
• Is a restricted version of array and linked list
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
102

More Related Content

What's hot

9781337102087 ppt ch10
9781337102087 ppt ch109781337102087 ppt ch10
9781337102087 ppt ch10Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath CommunityRohit Radhakrishnan
 
Certification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptxCertification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptxRohit Radhakrishnan
 
Certification preparation - Error Handling and Troubleshooting recap.pptx
Certification preparation - Error Handling and Troubleshooting recap.pptxCertification preparation - Error Handling and Troubleshooting recap.pptx
Certification preparation - Error Handling and Troubleshooting recap.pptxRohit Radhakrishnan
 

What's hot (6)

9781337102087 ppt ch10
9781337102087 ppt ch109781337102087 ppt ch10
9781337102087 ppt ch10
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 
.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community
 
Certification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptxCertification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptx
 
Certification preparation - Error Handling and Troubleshooting recap.pptx
Certification preparation - Error Handling and Troubleshooting recap.pptxCertification preparation - Error Handling and Troubleshooting recap.pptx
Certification preparation - Error Handling and Troubleshooting recap.pptx
 

Similar to 9781337102087 ppt ch18

Software Development, Data Types, and Expressions
Software Development, Data Types, and ExpressionsSoftware Development, Data Types, and Expressions
Software Development, Data Types, and Expressionspullaravikumar
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docxsleeperharwell
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsAmitaSuri
 
Access 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationAccess 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationdgdotson
 
Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving ProcessChapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Processmshellman
 
MySQL 8.0: What Is New in Optimizer and Executor?
MySQL 8.0: What Is New in Optimizer and Executor?MySQL 8.0: What Is New in Optimizer and Executor?
MySQL 8.0: What Is New in Optimizer and Executor?Norvald Ryeng
 
“Quantum” Performance Effects: beyond the Core
“Quantum” Performance Effects: beyond the Core“Quantum” Performance Effects: beyond the Core
“Quantum” Performance Effects: beyond the CoreC4Media
 
Chapter 6 - More on the Selection Structure
Chapter 6 - More on the Selection StructureChapter 6 - More on the Selection Structure
Chapter 6 - More on the Selection Structuremshellman
 

Similar to 9781337102087 ppt ch18 (20)

Software Development, Data Types, and Expressions
Software Development, Data Types, and ExpressionsSoftware Development, Data Types, and Expressions
Software Development, Data Types, and Expressions
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
Sql9e ppt ch08
Sql9e ppt ch08Sql9e ppt ch08
Sql9e ppt ch08
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
 
ltspice basic practice.pptx
ltspice basic practice.pptxltspice basic practice.pptx
ltspice basic practice.pptx
 
Sql9e ppt ch05
Sql9e ppt ch05 Sql9e ppt ch05
Sql9e ppt ch05
 
Lesson 8 more on repitition structure
Lesson 8 more on repitition structureLesson 8 more on repitition structure
Lesson 8 more on repitition structure
 
Python Fundamentals
Python FundamentalsPython Fundamentals
Python Fundamentals
 
Sql9e ppt ch02
Sql9e ppt ch02 Sql9e ppt ch02
Sql9e ppt ch02
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha Projects
 
Sql9e ppt ch03
Sql9e ppt ch03 Sql9e ppt ch03
Sql9e ppt ch03
 
Access 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationAccess 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentation
 
Sql9e ppt ch04
Sql9e ppt ch04 Sql9e ppt ch04
Sql9e ppt ch04
 
Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving ProcessChapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Process
 
MySQL 8.0: What Is New in Optimizer and Executor?
MySQL 8.0: What Is New in Optimizer and Executor?MySQL 8.0: What Is New in Optimizer and Executor?
MySQL 8.0: What Is New in Optimizer and Executor?
 
Sql9e ppt ch07
Sql9e ppt ch07 Sql9e ppt ch07
Sql9e ppt ch07
 
“Quantum” Performance Effects: beyond the Core
“Quantum” Performance Effects: beyond the Core“Quantum” Performance Effects: beyond the Core
“Quantum” Performance Effects: beyond the Core
 
Chapter 6 - More on the Selection Structure
Chapter 6 - More on the Selection StructureChapter 6 - More on the Selection Structure
Chapter 6 - More on the Selection Structure
 

More from Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10Terry Yoast
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09Terry Yoast
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08Terry Yoast
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07Terry Yoast
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06Terry Yoast
 

More from Terry Yoast (17)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06
 

Recently uploaded

Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 

Recently uploaded (20)

Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 

9781337102087 ppt ch18

  • 1. 1 C++ Programming: From Problem Analysis to Program Design, Eighth Edition Chapter 18 Stacks and Queues
  • 2. 2 Objectives (1 of 2) • In this chapter, you will: • Learn about stacks • Examine various stack operations • Learn how to implement a stack as an array • Learn how to implement a stack as a linked list • Learn about infix, prefix, and postfix expressions, and how to use a stack to evaluate postfix expressions C© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom ++ Programming: From Problem Analysis to Program Design, Seventh Edition 2
  • 3. 3 Objectives (2 of 2) • Learn how to use a stack to remove recursion • Learn about queues • Examine various queue operations • Learn how to implement a queue as an array • Learn how to implement a queue as a linked list • Discover how to use queues to solve simulation problems © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 3
  • 4. 4 Stacks (1 of 4) • Stack: a data structure in which elements are added and removed from one end only • Addition/deletion occur only at the top of the stack • Last in first out (LIFO) data structure • Operations: • Push: to add an element onto the stack • Pop: to remove an element from the stack © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 4
  • 5. 5 Stacks (2 of 4) FIGURE 18-1 Various types of stacks © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 5
  • 6. 6 Stacks (3 of 4) FIGURE 18-2 Empty stack © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 6
  • 7. 7 Stacks (4 of 4) FIGURE 18-3 Stack operations © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 7
  • 8. 8 Stack Operations • In the abstract class stackADT: • initializeStack • isEmptyStack • isFullStack • push • top • pop FIGURE 18-4 UML class diagram of the class stackADT © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 8
  • 9. 9 Implementation of Stacks as Arrays (1 of 5) • First element goes in first array position, second in the second position, etc. • Top of the stack is index of the last element added to the stack • Stack elements are stored in an array, which is a random access data structure • Stack element is accessed only through top • To track the top position, use a variable called stackTop © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 9
  • 10. 10 Implementation of Stacks as Arrays (2 of 5) • Can dynamically allocate array • Enables user to specify size of the array • class stackType implements the functions of the abstract class stackADT © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 10
  • 11. 11 Implementation of Stacks as Arrays (3 of 5) FIGURE 18-5 UML class diagram of the class stackType © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 11
  • 12. 12 Implementation of Stacks as Arrays (4 of 5) • C++ arrays begin with the index 0 • Must distinguish between: - Value of stackTop - Array position indicated by stackTop • If stackTop is 0, stack is empty • If stackTop is nonzero, stack is not empty • Top element is given by stackTop - 1 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 12
  • 13. 13 Implementation of Stacks as Arrays (5 of 5) FIGURE 18-6 Example of a stack © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 13
  • 14. 14 Initialize Stack FIGURE 18-7 Empty stack © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 14
  • 15. 15 Empty Stack/Full Stack • Stack is empty if stackTop = 0 • Stack is full if stackTop = maxStackSize © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 15
  • 16. 16 Push (1 of 3) • Store the newItem in the array component indicated by stackTop • Increment stackTop • Overflow occurs if we try to add a new item to a full stack C++ Progra© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom mming: From Problem Analysis to Program Design, Seventh Edition 16
  • 17. 17 Push (2 of 3) FIGURE 18-8 Stack before pushing y © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 17
  • 18. 18 Push (3 of 3) FIGURE 18-9 Stack after pushing y © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 18
  • 19. 19 Return the Top Element • top operation: • Returns the top element of the stack © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 19
  • 20. 20 Pop (1 of 3) • To remove an element from the stack, decrement stackTop by 1 • Underflow condition: trying to remove an item from an empty stack © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 20
  • 21. 21 Pop (2 of 3) FIGURE 18-10 Stack before popping D © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 21
  • 22. 22 Pop (3 of 3) FIGURE 18-11 Stack after popping D © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 22
  • 23. 23 Copy Stack • copyStack function: copies a stack © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 23
  • 24. 24 Constructor and Destructor • Constructor: • Sets stack size to parameter value (or default value if not specified) • Sets stackTop to 0 • Creates array to store stack elements • Destructor: • Deallocates memory occupied by the array • Sets stackTop to 0 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 24
  • 25. 25 Copy Constructor • Copy constructor: • Called when a stack object is passed as a (value) parameter to a function • Copies values of member variables from actual parameter to formal parameter © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 25
  • 26. 26 Overloading the Assignment Operator (=) (1 of 2) • Assignment operator must be explicitly overloaded because of pointer member variables © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 26
  • 27. 27 Stack Header File • Place definitions of class and functions (stack operations) together in a file • Called myStack.h © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 27
  • 28. 28 Linked Implementation of Stacks (1 of 2) • Array only allows fixed number of elements • If number of elements to be pushed exceeds array size, the program may terminate • Linked lists can dynamically organize data • In a linked representation, stackTop is pointer to memory address of top element in stack © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 28
  • 29. 29 Linked Implementation of Stacks (2 of 2) FIGURE 18-12 Empty and nonempty linked stack © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 29
  • 30. 30 Default Constructor • Initializes the stack to an empty state when a stack object is declared – Sets stackTop to nullptr template <class Type> linkedStackType<Type>::linkedStackType() { stackTop = nullptr; } © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 30
  • 31. 31 Empty Stack and Full Stack • In a linked implementation of stacks, function isFullStack does not apply • Logically, the stack is never full • Stack is empty if stackTop is nullptr © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 31
  • 32. 32 Linked Stack: Initialize Stack • initializeStack: reinitializes stack to an empty state • Must deallocate memory occupied by current element • Sets stackTop to nullptr © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 32
  • 33. 33 Push (1 of 2) • newNode is added at the beginning of the linked list pointed to by stackTop FIGURE 18-13 Stack before the push operation © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 33
  • 34. 34 Push (2 of 2) FIGURE 18-14 Push operation © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 34
  • 35. 35 Linked Stack: Return the Top Element template <class Type> Type linkedStackType<Type>::top() const { assert(stackTop != nullptr); //if stack is empty, //terminate the program return stackTop->info; //return the top element }//end top © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 35
  • 36. 36 Pop (1 of 2) • Node pointed to by stackTop is removed • Second element becomes top element FIGURE 18-15 Stack before the pop operation © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 36
  • 37. 37 Pop (2 of 2) FIGURE 18-16 Pop operation © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 37
  • 38. 38 Linked Stack: Copy Stack • copyStack function: makes an identical copy of a stack • Similar definition to copyList for linked lists C++ Pro© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom gramming: From Problem Analysis to Program Design, Seventh Edition 38
  • 39. 39 Constructors and Destructors • Copy constructor and destructor: • Similar to those for linked lists © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 39
  • 40. 40 Overloading the Assignment Operator (=) (2 of 2) • Overloading the assignment operator: © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 40
  • 41. 41 Stack as Derived from the class unorderedLinkedList • Implementation of push is similar to insertFirst for general lists • Other similar functions: • initializeStack and initializeList • isEmptyList and isEmptyStack • linkedStackType can be derived from linkedListType • class linkedListType is abstract • unorderedLinkedListType is derived from linkedListType • Provides the definitions of the abstract functions of the class linkedListType • linkedStackType is derived from unorderedLinkedListType © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 41
  • 42. 42 Application of Stacks: Postfix Expressions Calculator (1 of 8) • Infix notation: usual notation for writing arithmetic expressions • Operator is written between the operands • Example: a + b • Evaluates from left to right • Operators have precedence - Parentheses can be used to override precedence © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 42
  • 43. 43 Application of Stacks: Postfix Expressions Calculator (2 of 8) • Prefix (Polish) notation: operators are written before the operands • Introduced by the Polish mathematician Jan Lukasiewicz in early 1920s • Parentheses can be omitted • Example: + a b © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 43
  • 44. 44 Application of Stacks: Postfix Expressions Calculator (3 of 8) • Reverse Polish notation: operators follow the operands (postfix operators) • Proposed by Australian philosopher and early computer scientist Charles L. Hamblin in the late 1950s • Advantage: operators appear in the order required for computation • Example: a + b * c becomes a b c * + © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 44
  • 45. 45 Application of Stacks: Postfix Expressions Calculator (4 of 8) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 45
  • 46. 46 Application of Stacks: Postfix Expressions Calculator (5 of 8) • Postfix notation has important applications in computer science • Many compilers first translate arithmetic expressions into postfix notation and then translate this expression into machine code • Evaluation algorithm: • Scan expression from left to right • When an operator is found, back up to get operands, perform the operation, and continue C© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom + Programming: From Problem Analysis to Program Design, Seventh Edition 46
  • 47. 47 Application of Stacks: Postfix Expressions Calculator (6 of 8) FIGURE 18-17 Evaluating the postfix expression: 6 3 + 2 * = © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 47
  • 48. 48 Application of Stacks: Postfix Expressions Calculator (7 of 8) • Symbols can be numbers or anything else: • +, -, *, and / are operators, require two operands - Pop stack twice and evaluate expression - If stack has less than two elements  error • If symbol is =, expression ends - Pop and print answer from stack - If stack has more than one element  error • If symbol is anything else - Expression contains an illegal operator © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 48
  • 49. 49 Application of Stacks: Postfix Expressions Calculator (8 of 8) • Assume postfix expressions are in this form: #6 #3 + #2 * = • If symbol scanned is #, next input is a number • If the symbol scanned is not #, then it is: - An operator (may be illegal) or - An equal sign (end of expression) • Assume expressions contain only +, -, *, and / operators © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 49
  • 50. 50 Main Algorithm • Pseudocode: • Four functions are needed: • evaluateExpression, evaluateOpr, discardExp, and printResult C+© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom + Programming: From Problem Analysis to Program Design, Seventh Edition 50
  • 51. 51 Function evaluateExpression • Function evaluateExpression: • Evaluates each postfix expression • Each expression ends with = symbol © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 51
  • 52. 52 Function evaluateOpr • Function evaluateOpr: • Evaluates an expression • Needs two operands saved in the stack - If less than two  error • Also checks for illegal operations © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 52
  • 53. 53 Function discardExp • Function discardExp: • Called when an error is discovered in expression • Reads and writes input data until the ‘=’ © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 53
  • 54. 54 Function printResult • The function printResult: If the postfix expression contains no errors, it prints the result • Otherwise, it outputs an appropriate message • Result of the expression is in the stack, and output is sent to a file © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 54
  • 55. 55 • To print a list backward nonrecursively, first get to the last node of the list • Problem: Links go in only one direction • Solution: Save a pointer to each of node in a stack - Uses the LIFO principle • Since number of nodes is usually not known, use the linked implementation of a stack © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 55 Nonrecursive Algorithm to Print a Linked List Backward (1 of 7)
  • 56. 56 Nonrecursive Algorithm to Print a Linked List Backward (2 of 7) FIGURE 18-18 Linked list FIGURE 18-19 List after the statement current = first; executes © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom ramming: From Problem Analysis to Program Design, Seventh Edition 56
  • 57. 57 Nonrecursive Algorithm to Print a Linked List Backward (3 of 7) FIGURE 18-20 List and stack after the statements stack.push(current); and current = current->link; execute © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 57
  • 58. 58 Nonrecursive Algorithm to Print a Linked List Backward (4 of 7) FIGURE 18-21 List and stack after the statements stack.push(current); and current = current->link; execute © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 58
  • 59. 59 Nonrecursive Algorithm to Print a Linked List Backward (5 of 7) FIGURE 18-22 List and stack after the statements stack.push(current); and current =current->link; execute © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 59
  • 60. 60 Nonrecursive Algorithm to Print a Linked List Backward (6 of 7) FIGURE 18-23 List and stack after the statements current = stack.top(); and stack. pop(); execute © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 60
  • 61. 61 Nonrecursive Algorithm to Print a Linked List Backward (7 of 7) FIGURE 18-24 List and stack after the statements current=stack.top(); and stack.pop(); execute FIGURE 18-25 List and stack after the statements current=stack.top(); and stack.pop(); execute © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 61
  • 62. 62 Queues • Queue: set of elements of the same type • Elements are: • Added at one end (the back or rear) • Deleted from the other end (the front) • First In First Out (FIFO) data structure • Middle elements are inaccessible • Example: • Waiting line in a bank © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 62
  • 63. 63 Queue Operations • Queue operations include: – initializeQueue – isEmptyQueue – isFullQueue – front – back – addQueue – deleteQueue •Abstract class queueADT defines these operations © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 63
  • 64. 64 Implementation of Queues as Arrays (1 of 15) • Need at least four (member) variables: • Array to store queue elements • queueFront and queueRear - To track first and last elements • maxQueueSize - To specify maximum size of the queue © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 64
  • 65. 65 Implementation of Queues as Arrays (2 of 15) • To add an element to the queue: • Advance queueRear to next array position • Add element to position pointed by queueRear FIGURE 18-26 Queue after the first addQueue operation © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 65
  • 66. 66 Implementation of Queues as Arrays (3 of 15) FIGURE 18-27 Queue after two more addQueue operations © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 66
  • 67. 67 Implementation of Queues as Arrays (4 of 15) • To delete an element from the queue: • Retrieve element pointed to by queueFront • Advance queueFront to next queue element FIGURE 18-28 Queue after the deleteQueue operation © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 67
  • 68. 68 Implementation of Queues as Arrays (5 of 15) • Will this queue design work? • Let A represent adding an element to the queue • Let D represent deleting an element from the queue • Consider the following sequence of operations: - AAADADADADADADADA... C++ Programming: From Problem © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom Analysis to Program Design, Seventh Edition 68
  • 69. 69 Implementation of Queues as Arrays (6 of 15) • This would eventually set queueRear to point to the last array position • Giving the impression that the queue is full FIGURE 18-29 Queue after the sequence of operations AAADADADADADA . . . © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 69
  • 70. 70 Implementation of Queues as Arrays (7 of 15) • Solution 1: When queue overflows at rear (queueRear points to the last array position): • Check value of queueFront • If queueFront indicates there is room at front of array, slide all queue elements toward the first array position • Problem: too slow for large queues • Solution 2: Assume that the array is circular C++ Program© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom ming: From Problem Analysis to Program Design, Seventh Edition
  • 71. 71 Implementation of Queues as Arrays 8 of 15) FIGURE 18-30 Circular queue © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 71
  • 72. 72 Implementation of Queues as Arrays (9 of 15) FIGURE 18-31 Queue before and after the add operation © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 72
  • 73. 73 Implementation of Queues as Arrays (10 of 15) • Deletion Case 1: FIGURE 18-32 Queue before and after the delete operation © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 73
  • 74. 74 Implementation of Queues as Arrays (11 of 15) • Deletion Case 2: FIGURE 18-33 Queue before and after the add operation © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 74
  • 75. 75 Implementation of Queues as Arrays (12 of 15) • Problem: • Figures 18-32b and 18-33b have identical values for queueFront and queueRear • However, Figure 18-32b represents an empty queue, whereas Figures 18-33b shows a full queue • Solution? © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 75
  • 76. 76 Implementation of Queues as Arrays (13 of 15) • Solution 1: keep a count • Incremented when a new element is added to the queue • Decremented when an element is removed • Initially set to 0 • Very useful if user (of queue) frequently needs to know the number of elements in the queue © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 76
  • 77. 77 Implementation of Queues as Arrays (14 of 15) • Solution 2: Let queueFront indicate index of array position preceding the first element • queueRear still indicates index of last one • Queue empty if: - queueFront == queueRear • Slot indicated by queueFront is reserved • Queue is full if next available space is the reserved slot indicated by queueFront © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 77
  • 78. 78 Implementation of Queues as Arrays (15 of 15) FIGURE 18-34 Array to store the queue elements with a reserved slot © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 78
  • 79. 79 Empty Queue and Full Queue • Queue is empty if count ==0 • Queue is full if count == maxQueueSize © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 79
  • 80. 80 Initialize Queue • Initializes queue to empty state • First element is added at first array position • queueFront set to 0 • queueRear set to maxQueueSize -1 • count set to 0 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 80 FIGURE 18-35 Empty queue
  • 81. 81 Front and Back • front operation: returns the first element of the queue • If queue is empty, program terminates • back operation: returns the last element of the queue • If queue is empty, program terminates © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 81
  • 82. 82 addQueue • addQueue operation: • Advance queueRear to next array position • Add new element to array position indicated by queueRear • Increment count by 1 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 82
  • 83. 83 deleteQueue • deleteQueue operation: • Decrement count by 1 • Advance queueFront to next queue element © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 83
  • 84. 84 Queue: Constructors and Destructors • Constructor: • Sets maxQueueSize to user specification • Creates array of size maxQueueSize (or default size) • Initializes queueFront and queueRear to indicate empty queue • Destructor: • Deallocates memory occupied by array © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 84
  • 85. 85 Linked Implementation of Queues (1 of 2) • Array implementation has issues: • Array size is fixed: only a finite number of queue elements can be stored in it • Requires array to be treated in a special way, together with queueFront and queueRear • Linked implementation of a queue simplifies many issues • Queue is never full because memory is allocated dynamically © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 86. 86 Linked Implementation of Queues (2 of 2) • Elements are added at one end and removed from the other • Need only two pointers to maintain the queue: queueFront and queueRear © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 86
  • 87. 87 Empty and Full Queue • Queue is empty if queueFront is nullptr • Queue is never full • Unless the system runs out of memory • Note: must provide isFullQueue function definition because it is an abstract function in parent class queueADT © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 87
  • 88. 88 Linked Queue: Initialize Queue • Initializes queue to an empty state • Must remove all existing elements, if any • Deallocates memory occupied by elements © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 88
  • 89. 89 addQueue, front, back, and deleteQueue operations • addQueue operation: adds new element to end of queue • front operation: returns first element of queue • back operation: returns last element of queue • deleteQueue operation: removes first element of queue © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 89
  • 90. 90 Linked Queue: Constructors and Destructors • Constructor • Accesses maxQueueSize, queueFront, and queueRear • Destructor: destroys the queue • Deallocates memory occupied by elements • Copy constructor and overloading assignment operator: • Similar to corresponding function for stack © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 90
  • 91. 91 Queue Derived from the class unorderedLinkedListType • Linked implementation of queue: similar to implementation of a linked list created in a forward manner • addQueue similar to insertFirst • initializeQueue is like initializeList • isEmptyQueue similar to isEmptyList • deleteQueue can be implemented as before • queueFront is same as first • queueRear is same as last © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 92. 92 Application of Queues: Simulation • Simulation: a technique in which one system models the behavior of another system • Computer models are used to study the behavior of real systems • Queuing systems: computer simulations using queues as the data structure • Queues of objects are waiting to be served © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 92
  • 93. 93 Designing a Queuing System (1 of 3) • Server: object that provides the service • Customer: object receiving the service • Transaction time: service time, or the time it takes to serve a customer • Model: system that consists of a list of servers and a waiting queue holding the customers to be served • Customer at front of queue waits for the next available server C++ Programming: From © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom Problem Analysis to Program Design, Seventh Edition
  • 94. 94 Designing a Queuing System (2 of 3) • Need to know: • Number of servers • Expected arrival time of a customer • Time between the arrivals of customers • Number of events affecting the system • Performance of system depends on: • How many servers are available • How long it takes to serve a customer • How often a customer arrives C++© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom Programming: From Problem Analysis to Program Design, Seventh Edition
  • 95. 95 Designing a Queuing System (3 of 3) • If it takes too long to serve a customer and customers arrive frequently, then more servers are needed • System can be modeled as a time-driven simulation • Time-driven simulation: the clock is a counter • The passage of one unit of time can be implemented by incrementing a counter by 1 • Simulation is run for a fixed amount of time C++ Pro© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom gramming: From Problem Analysis to Program Design, Seventh Edition
  • 96. 96 Customer FIGURE 18-36 UML class diagram of the class customerType © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 96
  • 97. 97 Server FIGURE 18-37 UML class diagram of the class serverType © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 97
  • 98. 98 Server List • Server list: a set of servers • At any given time, a server is either free or busy © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 98 FIGURE 18-38 UML class diagram of the class serverListType
  • 99. 99 Waiting Customers Queue • When customer arrives, he/she goes to end of queue • When a server becomes available, customer at front of queue leaves to conduct the transaction • After each time unit, the waiting time of each customer in the queue is incremented by 1 • Can use queueType but must add an operation to increment waiting time © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 99
  • 100. 100 Main Program • Algorithm for main loop: © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 100
  • 101. 101 Summary (1 of 2) • Stack: items are added/deleted from one end • Last In First Out (LIFO) data structure • Operations: push, pop, initialize, destroy, check for empty/full stack • Can be implemented as array or linked list • Middle elements should not be accessed directly • Postfix notation: operators are written after the operands (no parentheses needed) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom C++ Programming: From Problem Analysis to Program Design, Seventh Edition 101
  • 102. 102 Summary (2 of 2) • Queue: items are added at one end and removed from the other end • First In First Out (FIFO) data structure • Operations: add, remove, initialize, destroy, check if queue is empty/full • Can be implemented as array or linked list • Middle elements should not be accessed directly • Is a restricted version of array and linked list © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom 102