1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The...

25
1 Chapter 7 The Linked List as a Data Structure

Transcript of 1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The...

Page 1: 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.

1

Chapter 7The Linked List as a

Data Structure

Page 2: 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.

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.

Page 3: 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.

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.

Page 4: 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.

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...

Page 5: 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.

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.

Page 6: 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.

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.

Page 7: 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.

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).

Page 8: 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.

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.

Page 9: 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.

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.

Page 10: 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.

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”.

Page 11: 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.

11

LinkedListImplementation

• 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.

Page 12: 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.

12

LinkedList.hpp

1 template <template T>2 struct Node {3 T info;4 Node<T> *next;5 };67 template <template T>8 class LinkedList {9 Node<T> *start;10 public:11 LinkedList();

Page 13: 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.

13

LinkedList.hpp (cont.)

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

Page 14: 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.

14

Constructor & Destructor

LinkedList.cpp12 LinkedList<T>::LinkedList ( )3 {4 start = NULL;5 }67 LinkedList<T>:: ~LinkedList( )8 {9 makeEmpty( );10 }

Page 15: 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.

15

insert

20 void insertFront (T & newElement)21 {22 Node<T> *newNode = new Node<T>;23 newNode->info = newElement;24 newNode->next = start;25 start = newNode;26 } Inserting at the

beginning of the linked list.

Page 16: 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.

16

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

Inserting at the end of the linked list.

Page 17: 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.

17

first

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

Page 18: 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.

18

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

Overloaded operator if T is a struct object

Page 19: 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.

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<T>::remove( T & element )106 {107 if ( start == NULL )108 return false;109 Node<T> *ptr = start;110 if ( ptr->info == element ) {111 start = start->next;112 delete ptr;113 return true;114 }

Page 20: 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.

20

remove105 bool LinkedList<T>::remove( T & element )106 {107 if ( start == NULL )108 return false;109 Node<T> *ptr = start;110 if ( ptr->info == element ) {111 start = start->next;112 delete ptr;113 return true;114 }

remove continued…

Case 1: List is empty

Page 21: 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.

21

remove105 bool LinkedList<T>::remove( T & element )106 {107 if ( start == NULL )108 return false;109 Node<T> *ptr = start;110 if ( ptr->info == element ) {111 start = start->next;112 delete ptr;113 return true;114 }

remove continued…

Case 2: Element is the first element of the list

Page 22: 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.

22

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

Case 3

Page 23: 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.

23

isEmpty

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

Page 24: 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.

24

makeEmpty

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

Page 25: 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.

Reference

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

25