linked lists - lecture 16 sections 17.1 -...
TRANSCRIPT
Linked ListsLecture 16
Sections 17.1 - 17.3
Robb T. Koether
Hampden-Sydney College
Wed, Feb 22, 2017
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 1 / 24
1 Linked Lists
2 The LinkedListNode Class
3 The LinkedList Class
4 Random Access vs. Sequential Access
5 Assignment
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 2 / 24
Outline
1 Linked Lists
2 The LinkedListNode Class
3 The LinkedList Class
4 Random Access vs. Sequential Access
5 Assignment
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 3 / 24
Linked Lists
In a linked list, each node is allocated dynamically when theelement is added to the list and deallocated dynamically when it isremoved 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 andless efficient in others.The name of the linked list class is LinkedList.
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 4 / 24
Linked Lists
a0 a1 a2 ... an - 1
The abstract concept
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 5 / 24
Linked Lists
a0 a1 a2 ... an - 1
The linked implementation
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 5 / 24
Outline
1 Linked Lists
2 The LinkedListNode Class
3 The LinkedList Class
4 Random Access vs. Sequential Access
5 Assignment
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 6 / 24
The LinkedListNode Class
We create a separate class, LinkedListNode, as a data typethat stores a single element of a linked list.Each LinkedListNode contains a data item and a pointer thatlinks it to the next node.
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 7 / 24
LinkedListNode Data Members
LinkedListNode Data MembersT m_value;
LinkedListNode* m_next;
m_value – The element ai .m_next – A pointer to the node containing the element ai+1 orNULL if ai is the last element.
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 8 / 24
LinkedListNode Member Functions
LinkedListNode Public Member FunctionsT value() const;
T& value();
LinkedListNode* next();
T value() – Returns a copy of the data member of the node(r -value).T& value(); – Returns a reference to the data member of thenode (l-value).next(); – Returns a copy of the pointer to the next node.
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 9 / 24
LinkedListNode Member Functions
LinkedListNode Private Member FunctionsLinkedListNode(const T& val = T());
LinkedListNode(T&); – Constructs a linked list nodecontaining 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 22, 2017 10 / 24
LinkedListNode Destructor
LinkedListNode Destructor~LinkedListNode();
What should the LinkedListNode destructor do?
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 11 / 24
The LinkedListNode Class
The LinkedListNode ClassThe header file linkedlistnode.h.
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 12 / 24
Outline
1 Linked Lists
2 The LinkedListNode Class
3 The LinkedList Class
4 Random Access vs. Sequential Access
5 Assignment
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 13 / 24
The LinkedList Class
LinkedList Data Membersint m_size;
LinkedListNode* m_head;
m_size – The number of elements in the list.m_head – A pointer to the first node (which contains the firstelement).
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 14 / 24
The LinkedListNode Class
m_size
m_head m_value m_next
LinkedList LinkedListNode LinkedListNode LinkedListNode
m_value m_next m_value m_next
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 15 / 24
Validity Requirements of a Linked List
The linked list structure is valid providedm_size >= 0.If m_size == 0, then m_head == NULL.If m_size > 0, then m_head != NULL.For every i from 0 to m_size - 2, in node i, m_next != NULL.In node m_size - 1, m_next == NULL.
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 16 / 24
Outline
1 Linked Lists
2 The LinkedListNode Class
3 The LinkedList Class
4 Random Access vs. Sequential Access
5 Assignment
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 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 ofthe list and move sequentially through positions 1,2, . . . ,n − 1before reaching position n.An array allows random, or direct, access.
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 18 / 24
Random Access vs. Sequential Access
Sequential access is much slower than random access if the listelements are accessed randomly.However, sequential access may be faster than random access ifthe list elements are accessed sequentially.
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 19 / 24
The Technique of Chasing Pointers
Chasing PointersLinkedListNode* node = m_head;for (int i = 0; i < pos; i++)
node = node->m_next;
Technique of chasing pointers to locate position pos.
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 20 / 24
Random Access vs. Sequential Access
Random Access vs. Sequential Accessfor (int i = 0; i < list.size(); i++)
list[i] = 0;
How would the performance of the above for loop differ betweenthe ArrayList and LinkedList implementations?
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 21 / 24
The LinkedList Class
The LinkedList Classlinkedlistnode.h.linkedlist.h.ListTest.cpp.
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 22 / 24
Outline
1 Linked Lists
2 The LinkedListNode Class
3 The LinkedList Class
4 Random Access vs. Sequential Access
5 Assignment
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 23 / 24
Assignment
AssignmentRead Sections 17.1 - 17.3.
Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 24 / 24