Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.

Similar presentations


Presentation on theme: "1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list."— Presentation transcript:

1 1 Chapter 7 The Linked List as a Data Structure

2 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list operations. Abstract data type (ADT) is a specification of a set of data and the set of operations that can be performed on the data. It supports support abstraction, encapsulation, and information hiding.

3 3 List ADT Operations first, returns the first element in the linked list. insertFront, to insert a new item into the front of the list. insertBack, to insert a new item into the back of the list. find, to determine whether or not a certain item exists in a list. remove, to remove an item from a list. IsEmpty, to determine whether or not the list is empty. makeEmpty, to empty out the list.

4 4 Retrieving Elements When the client needs to retrieve an element in the list, the main practical reason is because it contains information that the client doesn’t have. Yet, the clients must know something about it; otherwise, they would not be able to tell the List object to look for it. The clients know about the key...

5 5 Keys A key is a value that uniquely identifies an object –If objects are people, a good key would be the SSN –books – ISBN key –parts – part number key The elements in a list ADT are usually objects – the key is just a single data member of the object.

6 6 An Example A customer of an insurance company has a problem with the amount paid by the insurance company for an operation. The customer calls the insurance company. The insurance company asks the customer for the claim number (the key). The customer provides the claim number.

7 7 An Example (cont.) The insurance company representative types the claim number (key) into the computer. The claim number is typed into a program which is using one or more data structures. The retrieve function of a data structure is called, passing in the claim number (key).

8 8 An Example (cont.) The retrieve function searches the data structure for the object that has the key. The retrieve function finds the object and returns the object. All the data in the object is now provided to the main program. The main program shows all the data on the screen.

9 9 An Example (cont.) The insurance company representative looks at the data. The insurance company representative can now see what the customer is complaining about.

10 10 List Implementation In C++, lists can be implemented with arrays or linked lists. Recall 2 advantages of linked lists –conserve memory for large objects (such as objects with keys). –can easily remove an element from the middle. So, we’ll focus on using the linked list. Instead of saying “linked-list implementation of a list”, we’ll just say “linked list”.

11 11 LinkedList Implementation A general linked list is more involved than the linked list queue or the linked list stack. The client must be able to access, change, or remove any element in the linked list at any time. It should be implemented to handle key- oriented types of objects, but also be general enough to handle other objects without keys, like strings.

12 12 LinkedList.hpp 1 template 2 struct Node { 3T info; 4Node *next; 5 }; 6 7 template 8 class LinkedList { 9 Node *start; 10 public: 11LinkedList();

13 13 LinkedList.hpp (cont.) 15~LinkedList( ); 16 void insertFront ( T & element ); 17 void insertBack ( T & element ); 18bool first( T & listEl ); 19bool find ( T & element ); 20bool remove( T & element ); 21bool isEmpty( ); 22void makeEmpty( ); 23 };

14 14 Constructor & Destructor LinkedList.cpp 1 2 LinkedList ::LinkedList ( ) 3 { 4start = NULL; 5 } 6 7 LinkedList :: ~LinkedList( ) 8 { 9makeEmpty( ); 10 }

15 15 insert 20 void insertFront (T & newElement) 21 { 22Node *newNode = new Node ; 23newNode->info = newElement; 24newNode->next = start; 25start = newNode; 26 } Inserting at the beginning of the linked list.

16 16 insertBack 30 void LinkedList ::insertBack (T & newElement) 31 { 32Node *newNode = new Node ; 33newNode->info = newElement; 34newNode->next = NULL; 35if (start == NULL) { // list is empty. 36start = newNode; 37return; 38} 39Node * ptr = start; 40while (ptr->next != NULL) // list is not empty. 41ptr = ptr->next; 42ptr->next = newNode; 43 } Inserting at the end of the linked list.

17 17 first 49 bool LinkedList ::first( T & listEl ) 50 { 51if ( start == NULL ) 52return false; 53 54listEl = start->info; 55return true; 56 }

18 18 find 73 bool LinkedList ::find( T & element ) 74 { 75bool found = false; 76Node * ptr = start; 77while (ptr != NULL && !found) { 78if (ptr->info == target) 79found = true; 80if (!found) 81ptr = ptr->next; 82} 83return found; 84 } Overloaded operator if T is a struct object

19 19 remove We need to keep ptr one node in front of the node to remove, so the first node is a special case. remove continued… 105 bool LinkedList ::remove( T & element ) 106 { 107if ( start == NULL ) 108return false; 109Node *ptr = start; 110if ( ptr->info == element ) { 111start = start->next; 112delete ptr; 113return true; 114}

20 20 remove 105 bool LinkedList ::remove( T & element ) 106 { 107if ( start == NULL ) 108return false; 109Node *ptr = start; 110if ( ptr->info == element ) { 111start = start->next; 112delete ptr; 113return true; 114} remove continued… Case 1: List is empty

21 21 remove 105 bool LinkedList ::remove( T & element ) 106 { 107if ( start == NULL ) 108return false; 109Node *ptr = start; 110if ( ptr->info == element ) { 111start = start->next; 112delete ptr; 113return true; 114} remove continued… Case 2: Element is the first element of the list

22 22 remove (cont.) 117while ( ptr->next != NULL ) { 118if ( ptr->next->info == element ) { 119Node *tempPtr = ptr->next; 120ptr->next = tempPtr->next; 121delete tempPtr; 122return true; 123} 124ptr = ptr->next; 125} 126 127return false; 128 } Case 3

23 23 isEmpty 132 bool LinkedList ::isEmpty( ) 133 { 134return start == NULL; 135 }

24 24 makeEmpty 137 void LinkedList ::makeEmpty( ) 138 { 139while ( start != NULL ) { 140Node * ptr = start; 141start = start->next; 142delete ptr; 143} 144 }

25 Reference Childs, J. S. (2008). The Linked List as a Data Structure. C++ Classes and Data Structures. Prentice Hall. 25


Download ppt "1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list."

Similar presentations


Ads by Google