CH 05
CH 05
CH 05
Chapter 5
Linked Lists
Objectives
Output
– Backward
• New node always inserted at the beginning of the list
• See example on page 277
• See function buildListBackward on page 278
• 11 basic operations
• Two types of linked lists: sorted and unsorted
• class linkedListType
– Implements basic linked list operations as an ADT
– Derive two classes using inheritance
• unorderedLinkedList and orderedLinkedList
• Unordered linked list functions
– buildListForward and buildListBackward
– Two more functions accommodate both operations
• insertFirst and insertLast
• Destructor
– When class object goes out of scope
• Deallocates memory occupied by list nodes
– Memory allocated dynamically
• Resetting pointers first and last
– Does not deallocate memory
– Must traverse list starting at first node
• Delete each node in the list
– Calling destroyList() destroys the list
• Copy constructor
– Makes identical copy of the linked list
– Function copyList checks whether original list
empty
• Checks value of first
– Must initialize first to NULL
• Before calling the function copyList
• Overloading the assignment operator
– Similar to copy constructor definition
}//end else
}//end else
}//end deleteNode
Data Structures Using C++ 2E 53
Unordered Linked Lists (cont’d.)
TABLE 5-7 Time-complexity of the operations of the
class unorderedLinkedList
• Insert a node
– Find place where new item goes
• Insert item in the list
– See code on page 304
• Definition of the function insert
//3b
//3a
• Delete a node
– Several cases to consider
– See function deleteNode code on page 306
• isEmptyList
• Insert a node
– Four cases
• Case 1: Insertion in an empty list
• Case 2: Insertion at the beginning of a nonempty list
• Case 3: Insertion at the end of a nonempty list
• Case 4: Insertion somewhere in a nonempty list
• Cases 1 and 2 requirement: Change value of the
pointer first
• Cases 3 and 4: After inserting an item, count
incremented by one