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

Post on 05-Jan-2016

223 views 4 download

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

1

Chapter 7The 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 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

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

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

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

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

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

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

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

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

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.

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();

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 };

14

Constructor & Destructor

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

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.

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.

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 }

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

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 }

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

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

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

23

isEmpty

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

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 }

Reference

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

25