Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18 Stacks and Queues At a Glance Instructor’s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms 18-1 C++ Programming: Program Design Including Data Structures, Fourth Edition 18-2 Lecture Notes Overview Chapter 18 introduces students to two very useful data structures, stacks and queues. Students will examine the various operations of stacks and queues. Then, they will learn how to implement these structures as an array and as a linked list. Finally, students will explore some of the numerous applications in which stacks and queues are used. Objectives In this chapter, the student 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 Discover stack applications 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 queue applications Teaching Tips Stacks Teaching Tip Review the stack rewinding of functions during exception propagation (Chapter 15) as an introduction to the stack data structure. Note that keeping track of function calls is an important application of stacks. 1. Define the stack data structure. Mention that it is frequently used to convert recursive algorithms into nonrecursive algorithms. 2. Explain the purpose of the element of a stack called the top. C++ Programming: Program Design Including Data Structures, Fourth Edition Teaching Tip 18-3 Illustrate everyday stacks with the examples in Figure 18-1, and then ask students to think of more examples in which the top element of a stack is always the one the user would want to retrieve first. 3. Define the term LIFO (Last In First Out) and explain why a stack is a LIFO data structure. 4. Describe the push, pop, and top operations of a stack using Figures 18-2 and 18-3. 5. Explain why the isEmpty, isFull, and initializeStack operations are necessary for a stack implementation. Stack Operations Teaching Tip Explain why the stack ADT is implemented as an abstract class. 1. Examine the stack ADT definition and UML class diagram. Review the purpose of the member functions. Quick Quiz 1 1. What is a LIFO data structure? Answer: A Last In First Out (LIFO) data structure is a data structure in which the item that is added last will be removed first. 2. The addition and deletion of elements in a stack only occur at one end, called the ____________________ of the stack. Answer: top 3. True or False: A stack element can be accessed either at the top or the bottom of the stack. Answer: False 4. The ____________________ operation of a stack removes and stores the top element of the stack. Answer: pop C++ Programming: Program Design Including Data Structures, Fourth Edition 18-4 Implementation of Stacks as Arrays Teaching Tip Emphasize that the primary property of a stack is that an element can only be accessed from the top. In other words, the client cannot access elements in any other location of the stack. 1. Examine the stack ADT definition and UML class diagram for an array implementation of a stack. Note the use of a dynamic array. 2. Discuss the function of the variable stackTop. Use Figure 18-6 to illustrate. Teaching Tip Verify that students understand the possible values of stackTop and how they correspond to the array index. Initialize Stack 1. Explain how this function initializes a stack using Figure 18-7 and the function definition. Empty Stack 1. Briefly examine the definition of the isEmptyStack function. Full Stack 1. Note the definition of the isFullStack function. Push 1. Describe the two-step process to push an element onto a stack. Use Figures 18-8 and 18-9 to illustrate. 2. Examine the definition of the push function. 3. Discuss how to check for overflow in a stack. Return the Top Element 1. Note the definition of the top function. Pop 1. Describe the process of popping an element off a stack using Figures 18-10 and 18-11. C++ Programming: Program Design Including Data Structures, Fourth Edition 18-5 2. Examine the definition of the pop function. 3. Discuss how to check for underflow in a stack. Teaching Tip Remind students that the STL vector class uses stack terminology to insert and remove elements at the end. Note, however, that it provides these operations for both the top and bottom, and it also allows access to other elements. Copy Stack 1. Examine the definition of the copyStack function. Constructor and Destructor 1. Describe how an array-based stack allocates and deallocates memory in the constructor and destructor, respectively. Copy Constructor 1. Note the definition of the copy constructor in an array-based stack. Overloading the Assignment Operator (=) 1. Note the definition of the overloaded assignment operator in an array-based stack. Teaching Tip Students may be pleasantly surprised by the simplicity of the stack implementation. Explain that this is due to the fact that only the top element is manipulated in most cases. Stack Header File 1. Examine the header file for the array-based stack ADT. 2. Walk through the sample program in Example 18-1 to illustrate how an array-based stack is used by a client program. 3. Encourage your students to work through the “Highest GPA” Programming Example to become more familiar with stacks. Linked Implementation of Stacks 1. Review the advantages of using a linked implementation of a data structure rather than an array-based implementation. C++ Programming: Program Design Including Data Structures, Fourth Edition 18-6 2. Explain how the use of the stackTop member variable differs in the linked implementation of a stack. Teaching Tip Ask your students if they can think of any advantages to using a stack as an array rather than as a linked list. 3. Examine the definition of the linked stack ADT. 4. Use Figures 18-12 and 18-13 to illustrate how a linked stack works. Default Constructor 1. Note the definition of the default constructor of the linked stack ADT. Empty Stack and Full Stack 1. Briefly examine the definitions of the isEmpty and isFull functions. Teaching Tip Note that in a linked stack implementation, the isFullStack function only applies if computer memory runs out. Mention that this is a typical implementation. Ask your students if they can think of another approach to designing the class hierarchy that would not require the implementation of this function in the linked stack class. Initialize Stack 1. Examine the initializeStack function in the linked stack ADT and compare it to the corresponding function in the array-based stack ADT. Push 1. Illustrate the execution of the push function in a linked stack using Figures 18-14 through 18-17. Examine the definition of the push function. Teaching Tip Ask students to comment on the differences between this function and the insert functions of the linked list classes. Note that this function does not provide an assert statement for failure to allocate memory. Return the Top Element 1. Note the straightforward definition of the top function in a linked stack. C++ Programming: Program Design Including Data Structures, Fourth Edition 18-7 Pop 1. Illustrate the execution of the pop function in a linked stack using Figures 18-18 through 18-20. Examine the definition of the pop function. Copy Stack 1. Note that the definition of copyStack is similar to the definition of copyList for linked lists. Constructors and Destructors 1. Examine the constructor and destructor definitions and remind students that these functions are also similar to the corresponding functions in a linked list. Overloading the Assignment Operator (=) 1. Note the definition of the overloaded assignment operator of a linked stack ADT. 2. Remind students that this is a generic stack and that the definition and implementation of the ADT will be placed in a header file. 3. Illustrate how the linked stack can be used by a client program using Example 18-3. Stack as Derived from the class unorderedLinkedList 1. Review the similarities between the operations of a linked implementation of a stack and the analogous operations of a linked list. 2. Explain how to derive a stack class from the unorderedLinkedList class of Chapter 17 using the class definition in this section. Point out the calls to the base class in the stack operations. Teaching Tip Discuss whether this implementation violates the restrictions of a stack by allowing access to the linked list operations of its base class, unorderedLinkedList. Note that the stack ADT interface does not list the underlying base class operations, but the base class is inherited as a public class. Application of Stacks: Postfix Expressions Calculator 1. Describe infix, prefix, and postfix notation. C++ Programming: Program Design Including Data Structures, Fourth Edition Teaching Tip 18-8 Ask students to convert a few postfix expressions into infix by hand before discussing the stack application. 2. Discuss the importance of postfix notation in computer science applications. 3. Explain how postfix expressions can be evaluated using a stack with Figures 18-21 through 18-28. Main Algorithm 1. Review the algorithm that is implemented in this section for evaluating a postfix expression. Function evaluateExpression 1. Explain how this function evaluates an entire postfix expression. 2. Examine the pseudocode and the implementation of this function. Teaching Tip This function has several parameters. Verify that students understand the purpose of each one. Function evaluateOpr 1. Describe how this function evaluates expressions using a stack. 2. Examine the pseudocode and the implementation of this function. Teaching Tip Step through the conditional statements in this function carefully to make sure students understand the various evaluation paths. Function discardExp 1. Briefly note the implementation of this function. Function printResult 1. Explain how the result of a postfix expression is output. 2. Illustrate how to use these functions in a program with listing in this section. C++ Programming: Program Design Including Data Structures, Fourth Edition Teaching Tip 18-9 Ask your students how this program might be implemented without a pound sign preceding each number. Quick Quiz 2 1. Which stack function is a private member of the class? Answer: copyStack 2. What is the position of the top element of a stack in an array-based implementation? Answer: stackTop - 1 3. Removing an element from an empty stack results in ____________________. Answer: underflow 4. True or False: In a linked implementation of a stack, the stack is only full if computer memory runs out. Answer: True Removing Recursion: Nonrecursive Algorithm to Print a Linked List Backward 1. Explain how a stack can be used to print a linked list backwards nonrecursively. Teaching Tip This section discusses the improvement in execution and processing time that results from using a stack instead of recursion. What about the memory overhead of recursive calls versus the memory overhead of an additional stack? Ask students to share their opinions on this balance. Queues 1. Define the queue data structure and explain that it is a FIFO (First In First Out) data structure. 2. Discuss the numerous applications of queues. 3. Define the terms back (or rear) and front as they relate to queues. Queue Operations 1. Discuss the typical operations of a queue, noting that the two key operations are to add an element to the rear of a queue and remove an element from the front of the queue. C++ Programming: Program Design Including Data Structures, Fourth Edition 18-10 2. Examine the definition of the abstract class queueADT. Teaching Tip Ask students if they think LIFO or FIFO structures are used more in everyday life. Implementation of Queues as Arrays 1. Discuss the issues involved with using an array-based implementation of a queue. 2. Describe the various solutions to the problems of queue overflow to the rear, and determining correctly whether a queue is empty or full. Use Figures 18-40 through 1850 to illustrate these issues. Teaching Tip Make sure students understand why a circular array is typically used in arraybased queue implementations, and that they understand the formula for testing whether a queue is empty or full when using a circular array. 3. Examine the class definition of an array-based queue ADT. 4. Discuss the implementations of the array-based queue ADT, including the functions isEmptyQueue, isFullQueue, initializeQueue, front, back, addQueue, deleteQueue, the constructors, and the destructor. In particular, note the operations that involve manipulating the locations of the front and back of the queue. Linked Implementation of Queues 1. Explain the advantages of using a linked rather than an array-based implementation of a queue. Teaching Tip Note that since the array-based implementation of queues has so many special cases involving the front and rear, a linked list implementation is preferable in most cases. Explain that the array version is shown here for completeness and to illustrate queue concepts. 2. Examine the class definition of the class linkedQueueType. 3. Step through the implementation of the class linkedQueueType in this section, including the functions isEmptyQueue, isFullQueue, initializeQueue, front, back, addQueue, and deleteQueue. 4. Illustrate how this class can be used by a client program using Example 18-5. C++ Programming: Program Design Including Data Structures, Fourth Edition 18-11 Queues Derived from the class unorderedLinkedListType 1. Explain how the linked implementation of a queue is similar to the linked list implementation of a list created in a forward manner. Note that the implementation of this class is left as a programming exercise at the end of the chapter. Application of Queues: Simulation Teaching Tip Ask your students to read this section before class so that they understand the implementation issues discussed in class. 1. Define the term simulation and explain the advantages of using simulation techniques to model real-world applications. 2. Explain what a queuing system is and how it is used to model everyday situations. Designing a Queuing System 1. Introduce the terms server, customer, and the transaction time as they relate to a queuing system. 2. Explain the requirements of a queuing system. 3. Discuss time-driven simulations and how they can be implemented with a queuing system. Customer 1. Describe the purpose of the customerType class and its member functions. 2. Examine the class definition, UML class diagram, and the partial function definitions of the class customerType. Server 1. Describe the purpose of the serverType and its member functions. 2. Examine the class definition, UML class diagram, and the partial function definitions of the class serverType. Server List 1. Describe the purpose of the serverListType and its member functions. C++ Programming: Program Design Including Data Structures, Fourth Edition 18-12 2. Examine the class definition, UML class diagram, and the partial function definitions of the class serverListType. Waiting Customers Queue 1. Describe the purpose of the waitingCustomerQueueType and its member functions. 2. Examine the class definition, UML class diagram, and the partial function definitions of the class waitingCustomerQueueType. Main Program 1. Describe the simulation parameters that the main program needs to obtain in order to run the simulation. 2. Discuss how to determine how many customers are arriving at a given time and how long it takes to serve them. 3. Describe the algorithm for the main loop in the simulation. Quick Quiz 3 1. A(n) ____________________ is a system in which a queue of objects is waiting to be served by various servers. Answer: queuing system 2. A(n) ____________________ array can be used in an array implementation of a queue to avoid an overflow error at the rear of the queue when the queue is not full. Answer: circular 3. True or False: A queue is a First In First Out data structure. Answer: True 4. True or False: Elements from a queue are deleted from the end known as the back. Answer: False Class Discussion Topics 1. Why do the linked stack and queue implementations not include iterators, as in the linked list classes of Chapter 17? Are there cases in which a stack or queue might need to be traversed privately, even if access cannot be provided publicly? If so, should an iterator be used for these cases? C++ Programming: Program Design Including Data Structures, Fourth Edition 18-13 2. Introduce your students to the stack and queue containers in the C++ Standard Template Library. You can find an overview of their functionality here: www.sgi.com/tech/stl/queue.html www.sgi.com/tech/stl/stack.html Discuss the operations that are available for these classes. Note that both classes use an underlying container for their implementation, the deque class by default. Given this fact, discuss why the STL provides a separate stack or queue class at all. Additional Projects 1. Write a private function for the stack that checks for duplicates. Use the derived version of the stack class, so that you have the functions of the unorderedlinkedList at your disposal. This private function should only be called by the push operation, and should send a message if the user tries to add a duplicate. 2. Write a program that allows a user to see a recipe, one step at a time. A recipe class will contain a queue to hold the steps of the recipe and a list to hold the ingredients. The constructor creates the recipe from an input file. The test program will first print the ingredients list and then give the user options to display a step of the recipe, move on to the next step (in which the step is removed from the queue), list the ingredients again, or exit the program. Additional Resources 1. An Introduction to Data Structures in C++: Stacks: http://library.thinkquest.org/C005618/text/stacks.htm 2. An Introduction to Data Structures in C++: Queues: http://library.thinkquest.org/C005618/text/queues.htm 3. The Queue Data Structure: www.cprogramming.com/tutorial/computersciencetheory/queue.html 4. Stack (data structure): http://en.wikipedia.org/wiki/Stack_%28data_structure%29 Key Terms addQueue: adds a new element to the rear of the queue Back (rear): location where elements are added to a queue; also a queue operation (back) Customer: in a queuing system, refers to the object receiving the service deleteQueue: removes the front element from the queue C++ Programming: Program Design Including Data Structures, Fourth Edition 18-14 destroyStack: removes all the elements from the stack, leaving the stack empty Front: location where elements are deleted from the queue; also a queue operation (front) Infix notation: usual notation for writing arithmetic expressions in which the operator is written between the operands initializeQueue: initializes the queue to an empty state initializeStack: initializes the stack to an empty state isEmptyQueue: checks whether the queue is empty isEmptyStack: checks whether the stack is empty isFullQueue: checks whether the queue is full isFullStack: checks whether the stack is full Last In First Out (LIFO) data structure: data structure in which the item that is added last will be removed first Overflow: results from attempting to push an item onto a full stack pop: removes and stores the top element of the stack Prefix (Polish notation): operators are written before the operands push: adds a new element to the top of the stack Queue: data structure in which the elements are added at one end, called the rear, and deleted from the other end queueFront: pointer to keep track of the front of the queue queueRear: pointer to keep track of the rear of the queue Queuing systems: systems where queues of objects are waiting to be served by various servers Server: in a queuing system, refers to the object that provides the service Simulation: technique in which one system models the behavior of another system Stack: data structure in which the elements are added and removed from one end only Postfix (Reverse Polish) notation: operators are written after the operands Time-driven simulation: simulation in which the clock is implemented as a counter and the passage of one minute; can be implemented by incrementing the counter by one Top: end of the stack where the addition and deletion of elements occur; also a stack operation (top) Transaction time: in a queuing system, the time it takes to serve a customer Underflow: results from popping, or removing, an element from an empty stack