Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17 Linked Lists At a Glance Instructor’s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms 17-1 C++ Programming: Program Design Including Data Structures, Fourth Edition 17-2 Lecture Notes Overview Chapter 17 introduces students to the linked list data structure. Until this point, students have used sequential lists, or arrays, to manipulate a group of items of the same type. In this chapter, they will explore how to use linked lists to overcome some of the performance and efficiency issues of arrays. Students will learn how to build and manipulate a linked list, as well as examine how to perform various operations on them. They will also be introduced to various types of linked lists, including the doubly linked list and the circular linked list. Objectives In this chapter, the student will: Learn about linked lists Become aware of the basic properties of linked lists Explore the insertion and deletion operations on linked lists Discover how to build and manipulate a linked list Learn how to construct a doubly linked list Teaching Tips Linked Lists Teaching Tip Review the performance of various array operations before discussing linked lists. Explain that insertions and deletions are inefficient on arrays, and that searching can be inefficient as well if the array is not already sorted. 1. Define a linked list as a collection of nodes. Explain that each node contains the data as well as the address of the next node in the list. 2. Use Figures 17-1 through 17-3 to illustrate a linked list graphically. Teaching Tip Discuss the differences between a dynamic array and a linked list. Note that although a dynamic array’s size does not have to be allocated until run time, it remains fixed after it is allocated. In addition, emphasize that the memory for an array is contiguous. With linked lists, the data is only connected by the link provided in each node. C++ Programming: Program Design Including Data Structures, Fourth Edition 17-3 3. Define the terms head and link as they relate to linked lists. 4. Examine the definition of a node structure. Teaching Tip The node structure may be intimidating to students on first reading. Emphasize that the pointer type on the node structure is of the node type because it is pointing to another node. Verify that students understand the difference between a “link” pointer and a pointer to an array’s base address. Linked Lists: Some Properties 1. Describe in detail how arrow notation is used in accessing items in a linked list. Use Figures 17-4 through 17-6 to clarify. Teaching Tip Make sure students understand the use of multiple arrows in a statement to move through a list, and remind them that indexing is not used to access items in a linked list. 2. Explain how to traverse a list with node pointers. Emphasize that the head pointer cannot be used to traverse a list because we would lose the nodes in the list. Instead, another pointer is set to the same address as the head pointer and used to traverse the list. Teaching Tip Explain that the identifier current is commonly used by programmers to refer to a node pointer that is used to traverse the list, and the identifier link is commonly used to refer to the node pointer member of a node. The identifier next is also often used to denote the node pointer member. 3. Describe how to insert a new node into a list using the code in this section. Use Figures 17-8 through 17-14 to illustrate how the node links must be rearranged to accommodate the new node. 4. Using Figures 17-15 through 17-19, describe how to delete a node from a linked list. Remind students to deallocate memory during this process. Teaching Tip Insertion into and deletion from linked lists is tricky, particularly with the pointer rearrangements. Assure students that after they become familiar with the code for each process, it will become instinctive. C++ Programming: Program Design Including Data Structures, Fourth Edition 17-4 Building a Linked List 1. Describe how to build a linked list forward. First, discuss node declarations and initializations with Figures 17-21 through 17-24 as a graphical illustration. Then walk through the function in this section that builds a complete list. Teaching Tip Verify that students understand why so many node pointers are needed to build a list by explaining the purpose of each pointer. 2. Describe how to build a list backward. Note the differences in the code from the function for building a list forward. Quick Quiz 1 1. What are the two components of a node in a linked list? Answer: The data and a link to the next node in the list 2. The address of the first node in a list is stored in a separate location, called the ____________________. Answer: head, first 3. True or False: The data type of a node pointer is the node type itself. Answer: True 4. The link of the last node in a linked list has the value ____________________. Answer: NULL Linked List as an ADT 1. Discuss why a linked list is typically implemented as an ADT. 2. List the typical operations for a linked list ADT. Teaching Tip Compare the set of operations of the linked list ADT to the set of operations of the array ADT in Chapter 14. Ask students to comment on their similarities and differences. 3. Explain why the linked list ADT in this chapter is implemented as an abstract class. 4. Briefly describe the two classes that will be derived from the linked list ADT and note the operations that will be implemented differently. C++ Programming: Program Design Including Data Structures, Fourth Edition 17-5 Structure of Linked List Nodes 1. Examine the node structure of the linked list class. Note the use of the Type parameter. Teaching Tip Explain why a struct is used for this implementation of a node rather than a class. Do your students think implementing a list template with a node structure is easier than with a node class? Why or why not? Member Variables of the class linkedListType 1. Discuss the three member variables of the class linkedListType — first, last, and count — and describe their data type and purpose. Linked List Iterators 1. Define the term iterator and explain its use in linked lists. Describe the typical operations of an iterator. Teaching Tip Explain that using an iterator to process a list or other type of generic container is a standard technique in object-oriented programming languages. 2. Examine the linkedListIterator class definition, UML diagram, and implementation in detail. Note the use of operator overloading, the this pointer, and the private node pointer. 3. Next, examine the class definition and UML diagram of the abstract class linkedListType. Explain why the copy constructor is declared as private, and why the member variables are declared as protected. 4. Examine the constructor and initialization functions for the linkedListType class. 5. Explain the purpose of the destroyList function and examine its implementation. Print the List 1. Discuss the implementation of the print function in the linkedListType class. Teaching Tip Ask students why they think the print function is not implemented as an overloaded stream friend function. Note the parameter types in the stream functions. C++ Programming: Program Design Including Data Structures, Fourth Edition 17-6 Length of a List 1. Briefly look at the implementation of the length function in the linkedListType class. Retrieve the Data of the First Node 1. Examine the implementation of the front function in the linkedListType class. Retrieve the Data of the Last Node 1. Examine the implementation of the back function in the linkedListType class. Teaching Tip Note that the first and last pointers provide constant time access to the first and last nodes. Begin and End 1. Discuss the implementations of the begin and end functions in the linkedListType class. Explain that these functions return an iterator to the list. Copy the List 1. Examine the implementation of the copyList function of the linkedListType class. Teaching Tip Ask students if this function creates a shallow or deep copy of the list, primarily in order to give them the opportunity to take a closer look at code using node pointers. Destructor 1. Discuss the implementation of the destructor function in the linkedListType class. Teaching Tip Discuss why the destroyList function is implemented as a separate function rather than simply placing its code in the destructor. Copy Constructor 1. Discuss the implementation of the copy constructor function of the linkedListType class. Explain why the pointer first must be set to NULL. C++ Programming: Program Design Including Data Structures, Fourth Edition 17-7 Overloading the Assignment Operator 1. Briefly examine the overloaded assignment operator of the linkedListType class. Quick Quiz 2 1. In general, what are the two types of linked lists? Answer: sorted and unsorted 2. The node of a linked list is implemented as a(n) ____________________ in this chapter. Answer: struct 3. A(n) ____________________ is an object that produces each element of a container, such as a linked list, one element at a time. Answer: iterator 4. True or False: The linkedListType class presented in this chapter has two member variables. Answer: False Unordered Linked Lists 1. Examine the class definition and UML diagram of the unorderedLinkedList class. Search the List 1. Discuss the implementation of the search function in the unorderedLinkedList class. Teaching Tip Note that this search must be implemented sequentially. Insert the First Node 1. Discuss the implementation of the insertFirst function in the unorderedLinkedList class. Insert the Last Node 1. Discuss the implementation of the insertLast and the deleteNode functions in the unorderedLinkedList class. C++ Programming: Program Design Including Data Structures, Fourth Edition Teaching Tip 17-8 The deleteNode function contains a complicated set of cases and pointer assignments. Step through the code slowly and verify that students understand the implementation. Header File of the Unordered Linked List 1. Review the header file for the unorderedLinkedList class. Remind students that both the function declarations and definitions are in the header file because the class is a template. Ordered Linked Lists 1. Examine the class definition and UML diagram of the orderedLinkedList class. Search the List 1. Discuss the search function in the orderedLinkedList class. Note that the item in the list is compared to the search item only after the loop has executed. Insert a Node 1. Examine the insert function in the orderedLinkedList class in detail. Teaching Tip Like the deleteNode function in the unorderedList class, this function has several cases involving complex pointer assignments. Step through the code carefully with Figures 17-38 through 17-45 to clarify how the function works. Insert First and Insert Last 1. Discuss the insertFirst and insertLast functions in the orderedListType class. Teaching Tip Since the functions above do not apply to ordered lists, ask your students if they can think of a way to design the class hierarchy to avoid implementing them. Delete a Node 1. Examine the deleteNode function of the orderedLinkedList class in detail. C++ Programming: Program Design Including Data Structures, Fourth Edition Teaching Tip 17-9 Ask students to work in groups and compare this delete function with the unordered list implementation and discuss their findings. Header File of the Ordered Linked List 1. Briefly review the header file of the orderedLinkedList class. Print a Linked List in Reverse Order (Recursion Revisited) 1. Discuss the recursive algorithm to print a list in reverse order. Note that the base case is hidden. Clarify the recursive process with Figure 17-47. Teaching Tip Note that this function would not be necessary with a list that has pointers in both directions. printListReverse 1. Explain how to start the recursive print process by calling it from a caller print function. Doubly Linked Lists 1. Define a doubly linked list as a list with a next and back pointer. Emphasize that a doubly linked list can be traversed in either direction. 2. Note that the operations for a doubly linked list are the same as for a singly linked list. 3. Examine the node structure for a node in a doubly linked list. 4. Briefly display the class definition for an ordered doubly linked list. Teaching Tip Discuss the implementation of the copy constructor, overloaded assignment operator, and the destructor in preparation for the programming assignment at the end of the chapter. Default Constructor 1. Examine the implementation of the default constructor in the doublyLinkedList class. C++ Programming: Program Design Including Data Structures, Fourth Edition 17-10 isEmptyList 1. Examine the implementation of the isEmptyList function in the doublyLinkedList class. Teaching Tip Discuss why this function does not have to verify that the back pointer is NULL as well. Destroy the List 1. Examine the implementation of the destroy function in the doublyLinkedList class. Note that last is explicitly set to NULL. Initialize the List 1. Briefly describe the implementation of the initializeList function in the doublyLinkedList class. Length of the List 1. Note the implementation of the length function in the doublyLinkedList class. Print the List 1. Note the implementation of the print function in the doublyLinkedList class. Reverse Print the List 1. Examine the implementation of the reversePrint function in the doublyLinkedList class. Compare it to the corresponding implementation in the singly linked list. Search the List 1. Explain that this search implementation is the same as that of the ordered singly linked list. First and Last Elements 1. Briefly note the implementation of the front and back functions in the doublyLinkedList class. 2. Examine the implementation of the insert and deleteNode functions of the doublyLinkedList class. Use Figures 17-49 through 17-53 to clarify the various cases that should be considered. C++ Programming: Program Design Including Data Structures, Fourth Edition Teaching Tip 17-11 Ask the students to work in groups and compare the doubly linked list functions to their singly linked list counterparts. The primary function of this exercise is to acquaint your students with the code for linked lists. Ask them to consider if one list is easier to conceptually understand than the other. Is the additional second pointer a factor? Circular Linked Lists 1. Define a circular linked list and illustrate how the first and last node are connected using Figure 17-56. 2. Note that the operations on a circular list are the same as the other types of lists described in this chapter. 3. Encourage students to work through the “Video Store” Programming Example at the end of the chapter. Quick Quiz 3 1. What operations does the unorderedLinkedList class in this chapter implement? Answer: search, insertFirst, insertLast, and deleteNode 2. The deleteNode function of an unordered list considers ____________________ general cases. Answer: four 3. Define a circular linked list. Answer: A circular linked list is one in which the last node points to the first node. 4. True or False: The list implementations in this chapter use a binary search algorithm for the search function. Answer: False Class Discussion Topics 1. Discuss the factors that are involved in choosing a particular list implementation. For example, is the overhead from the additional pointer in a doubly linked list a consideration in whether to use a singly or doubly linked list? Is it always a better choice to use the most flexible list implementation, which is, in the case of this chapter, the circular doubly linked list? C++ Programming: Program Design Including Data Structures, Fourth Edition 17-12 2. Discuss the disadvantages of using a list data structure as opposed to other containers, particularly in terms of its inefficiency in searches because of the lack of random access. 3. Introduce your students to the list container in the C++ Standard Template Library. You can find an overview of its functionality here: www.sgi.com/tech/stl/List.html. Discuss the similarities and differences between the STL list interface and the lists presented in this chapter. Additional Projects 1. The birthday program you created in Chapter 11 (and modified most recently in Chapter 14) uses a dynamic array of gift objects. Rewrite this program to use a sorted linked list implementation. You may use the orderedLinkedList class presented in this chapter. 2. The orderedLinkedList presented in this chapter allows for duplicates. Modify the search function of this class to return the number of items that match the search item rather than a Boolean value. You may use an additional count variable to keep track of the number of matched items. Once you find a matching search item, continue to traverse the list until an item is no longer a match. Additional Resources 1. STL Lists: www.cprogramming.com/tutorial/stl/stllist.html 2. Linked List: http://en.wikipedia.org/wiki/Linked_list 3. Linked Lists: http://gethelp.devx.com/techtips/cpp_pro/10min/10min0599.asp 4. Linked List: www.nist.gov/dads/HTML/linkedList.html Key Terms Circular linked list: linked list in which the last node points to the first node Head (first): location where the address of the first node in the list is stored Iterator: object that produces each element of a container, such as a linked list, one element at a time Link: component used to store the address of the next node in a linked list Linked list: list of items, called nodes, in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming: Program Design Including Data Structures, Fourth Edition Nodes: components of a linked list 17-13