Linked Lists Lecture 15 Sections 17.1 - 17.3 Robb T. Koether
by user
Comments
Transcript
Linked Lists Lecture 15 Sections 17.1 - 17.3 Robb T. Koether
Linked Lists Lecture 15 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney College Wed, Feb 24, 2016 Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 1 / 24 1 Linked Lists 2 The LinkedList Class 3 The LinkedListNode Class 4 Random Access vs. Sequential Access 5 Assignment Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 2 / 24 Outline 1 Linked Lists 2 The LinkedList Class 3 The LinkedListNode Class 4 Random Access vs. Sequential Access 5 Assignment Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 3 / 24 Linked Lists In a linked list, each node is allocated dynamically when the element is added to the list and deallocated dynamically when the element is removed from the list. A linked list always uses exactly the amount of memory it needs. A linked list is more efficient than an array list in some ways and less efficient in others. The name of the linked list class is LinkedList. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 4 / 24 Linked Lists a0 a1 Robb T. Koether (Hampden-Sydney College) a2 Linked Lists ... an - 1 Wed, Feb 24, 2016 5 / 24 Linked Lists a0 a1 Robb T. Koether (Hampden-Sydney College) a2 Linked Lists ... an - 1 Wed, Feb 24, 2016 5 / 24 Outline 1 Linked Lists 2 The LinkedList Class 3 The LinkedListNode Class 4 Random Access vs. Sequential Access 5 Assignment Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 6 / 24 The LinkedList Class LinkedList Data Members int mSize; LinkedListNode* head; mSize – The number of elements in the list. head – A pointer to the first node (which contains the first element). Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 7 / 24 Outline 1 Linked Lists 2 The LinkedList Class 3 The LinkedListNode Class 4 Random Access vs. Sequential Access 5 Assignment Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 8 / 24 The LinkedListNode Class We create a separate class, LinkedListNode, as a data type that stores a single element of a LinkedList object. Each LinkedListNode contains a data item and a pointer that links it to the next node. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 9 / 24 LinkedListNode Data Members LinkedListNode Data Members T mValue; LinkedListNode* next; mValue – The list element ai . next – A pointer to the node containing list element ai+1 or NULL if ai is the last element. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 10 / 24 The LinkedListNode Class mSize head value next value next value next LinkedList LinkedListNode LinkedListNode LinkedListNode Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 11 / 24 LinkedListNode Member Functions LinkedListNode Public Member Functions T value() const; T& value(); LinkedListNode* nextNode(); T value() – Returns a copy of the data member of the node (r -value). T& value(); – Returns a reference to the data member of the node (l-value). nextNode(); – Returns a copy of the pointer to the next node. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 12 / 24 LinkedListNode Member Functions LinkedListNode Private Member Functions LinkedListNode(const T& value = T()); LinkedListNode(T&); – Constructs a linked list node containing the specified value. It can be invoked only by friend classes. The only friend class (for now) will be the LinkedList class. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 13 / 24 LinkedListNode Destructor LinkedListNode Destructor ~LinkedListNode(); What should the LinkedListNode destructor do? Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 14 / 24 The LinkedListNode Class The LinkedListNode Class The header file linkedlistnode.h. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 15 / 24 Validity Requirements of a Linked List The linked list structure is valid provided mSize >= 0. If mSize == 0, then head == NULL. If mSize > 0, then head != NULL. For every i from 0 to mSize - 2, in node i, next != NULL. In node mSize - 1, next == NULL. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 16 / 24 Outline 1 Linked Lists 2 The LinkedList Class 3 The LinkedListNode Class 4 Random Access vs. Sequential Access 5 Assignment Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 17 / 24 Random Access vs. Sequential Access A linked list allows sequential access, but not random access. To find the list element in position n, we must begin at the head of the list and move sequentially through positions 1, 2, . . . , n − 1 before reaching position n. An array allows random, or direct, access. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 18 / 24 Random Access vs. Sequential Access Sequential access is much slower than random access if the list elements are accessed randomly. However, sequential access may be faster than random access if the list elements are accessed sequentially. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 19 / 24 The Technique of Chasing Pointers Chasing Pointers LinkedListNode* node = head; for (int i = 0; i < pos; i++) node = node->next; Technique of chasing pointers to locate position pos. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 20 / 24 Random Access vs. Sequential Access Random Access vs. Sequential Access for (int i = 0; i < list.size(); i++) list[i] = 0; How would the performance of the above for loop differ between the ArrayList and LinkedList implementations? Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 21 / 24 The LinkedList Class The LinkedList Class linkedlistnode.h. linkedlist.h. List Test.cpp. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 22 / 24 Outline 1 Linked Lists 2 The LinkedList Class 3 The LinkedListNode Class 4 Random Access vs. Sequential Access 5 Assignment Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 23 / 24 Assignment Assignment Read Sections 17.1 - 17.3. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 24, 2016 24 / 24