Linked ListsTopicsThe LinkedList ClassLinkedList Data MembersThe LinkedListNode ClassLinkedListNode Data MembersLinkedListNode Member FunctionsSlide 8Validity Requirements of a Linked ListSlide 10Linked ListsLecture 18Mon, Mar 1, 200401/15/19 List Implementations 2TopicsThe LinkedList classThe LinkedListNode classValidity requirementsThe implementation of the member functions01/15/19 List Implementations 3The LinkedList ClassFor each element of the list, a node is allocated dynamically when that element is added to the list.The LinkedList always uses exactly the amount of memory it needs.The LinkedList is more efficient than the ArrayList in some ways and less efficient in others.01/15/19 List Implementations 4LinkedList Data Membersint sizeThe number of elements in the list.LinkedListNode* headA pointer to the first node (which contains the first element).01/15/19 List Implementations 5The LinkedListNode ClassWe create a separate class, LinkedListNode, as a data type that stores one element of a LinkedList.Each node contains a pointer that links it to the next node.01/15/19 List Implementations 6LinkedListNode Data MembersA linked list node has two components.T dataThe list element ai.LinkedListNode* nextA pointer that points to the node containing list element ai + 1. Or, NULL if ai is the last element.01/15/19 List Implementations 7LinkedListNode Member FunctionsThe node constructors are private.They can be invoked only by friend classes. Public interface T& Value();Returns a reference to the data member of the node. LinkedListNode* Next();Returns a copy of the pointer to the next node.01/15/19 List Implementations 8The LinkedListNode ClassThe LinkedListNode classlinkedlistnode.h01/15/19 List Implementations 9Validity Requirements of a Linked Listsize >= 0.If size == 0, then head == NULL.If size > 0, then head != NULL.For every i from 1 to size – 1, in node i, next != NULL.In node size, next == NULL.01/15/19 List Implementations 10The LinkedList ClassThe LinkedList
View Full Document