SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Chapter 7
The Linked List as a
  Data Structure



                       1
The List ADT
• A list is a list of elements.
• The list of elements consist of the data
  acted upon by list operations.
• A current position (or active position) in the
  list is also acted upon by list operations.




                                               2
List ADT Operations
• insert, to insert a new item into the list; there is
  no current position after an insertion.
• an iterator, for retrieving (by copy instead of
  removal) each item from the list, one at a time;
  at any particular moment when an item is
  retrieved, that item becomes the current position
  in the list.
• find, to determine whether or not a certain item
  exists in a list; if the item exists, it becomes the
  current position.
• retrieve, to retrieve (by copy instead of removal)
  a certain item; that item becomes the current
  position.
• more…
                                                     3
List ADT Operations
                  (cont.)
• replace, to replace the item at the current
  position with another item; the current position
  remains unchanged.
• remove, to remove an item from a list; there is
  no current position after a removal.
• an operation to determine whether or not the list
  is empty; the current position is unchanged.
• an operation to empty out the list; the current
  position is lost.

                                                      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
Retrieve Function
              Implementation
• How should we pass a key into the retrieve
  function and return the object?
• Approach 1: Pass in a key as a parameter and
  pass in an object as a reference parameter (to
  return the object result).
• Approach 2: Pass in an object by reference
  which has its key set to the key to search for;
  when the object is found in the linked list, it is
  assigned to the object passed in by reference.

                                                       12
Advantages of
                 Approach 2
• The client must declare an object, which will hold
  the data retrieved from the linked list.
  – approach 2 relieves the client of also having to
    declare a key; a key is already in the object.
• If approach 1 is used, two T’s are needed
  (say, T1 and T2) for the object type and the key
  type
• approach 2 will also be used for the find and
  remove functions.

                                                       13
The Retrieval Process
• An object is created in the main program.
• The representative asks the customer for the
  key.
• The representative types in the key.
• The object’s data member is set to the key
  value; no other data members in the object are
  set.
• The object (let’s say obj1) is passed into the
  retrieve function by reference.


                                                   14
The Retrieval Process
                  (cont.)
• The struct for obj1 has an overloaded
  operator, used by the retrieve function for finding
  the object with the key:
   Example: if ( obj1 == ptr->info ) // found
• The other information is placed in obj1
   obj1 = ptr->info;
• The retrieve function returns true (indicating a
  find) and obj1 is returned by reference
  parameter.

                                                     15
The Iterator
• first – returns the first element in the linked list.
• getNext – returns the next element in the linked
  list, after the first function call or previous
  getNext function call
   – Implemented by maintaining a current pointer in the
     private section.
   – The current pointer is advanced every time getNext is
     called.
   – Returns false when the client tries to get an element
     beyond the end of the list (otherwise returns true).


                                                          16
Find and Replace
                Functions
• find – returns true only if an element with
  the key was found – the element itself is
  not returned.
• replace – replaces the element at the
  current position with the element passed in
  – find and replace functions will often be used
    together.



                                                    17
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.
                                               18
LinkedList.hpp
1 template <template T>
2 struct Node {
3     T info;
4     Node<T> *next;
5 };
6
7 template <template T>
8 class LinkedList
9 {
10 public:
11    LinkedList( ) { … }



                                 19
LinkedList.hpp (cont.)
15    ~LinkedList( ) { … }
16    void insert( const T & element ) { … }
17    bool first( T & listEl ) { … }
18    inline bool getNext( T & listEl ) { … }
19    bool find ( const T & element ) { … }
20    bool retrieve( T & element ) { … }
21    bool replace( const T & newElement ) { … }
22    bool remove( T & element ) { … }
23    bool isEmpty( ) const { … }
24    void makeEmpty( ) { … }
25 private:
26    Node<T> *start;
27    Node<T> *current;
28 };
                                                   20
Constructor & Destructor
2    LinkedList( )
3    {
4       start = current = NULL;
5    }
6
7    ~LinkedList( )
8    {
9       makeEmpty( );
10   }




                                     21
insert
30 void insert( const T & element )
31 {
32    current = NULL;
33    Node<T> *newNode = new Node<T>;
34    newNode->info = element;
35    newNode->next = start;
36    start = newNode;
                                    Inserting at the
37 }
                                    beginning of the
                                    linked list.



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




                                   23
getNext
60 bool getNext( T & listEl )
61 {
62   if ( current == NULL )
63            return false;
64   if ( current->next == NULL ) {
65            current = NULL;
66            return false;
67   }
68   current = current->next;
69   listEl = current->info;
70   return true;
71 }


                                      24
find
73 bool find( const T & element )
75 {
76   T item;
77   if ( !first( item ) )          Overloaded
78             return false;        operator if T is
79   do if ( item == element )      a struct object
80             return true;
81   while ( getNext( item ) );
82
83   return false;
84 }



                                                       25
retrieve
87   bool retrieve( T & element )
88   {
89     if ( !find( element ) )
90               return false;
91     element = current->info;
92     return true;
93   }




                                    26
replace
96 bool replace( const T & newElement )
97 {
98    if ( current == NULL )
99             return false;
100 current->info = newElement;
101 return true;
102 }




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

                                                       28
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        }
126
127 return false;
128 }


                                                    29
isEmpty

132 bool isEmpty( ) const
133 {
134 return start == NULL;
135 }




                              30
makeEmpty
137 void makeEmpty( )
138 {
139 while ( start != NULL ) {
140        current = start;
141        start = start->next;
142        delete current;
143 }
144
145 current = NULL;
146 }


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




                                               32

Weitere ähnliche Inhalte

Was ist angesagt?

Core java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaCore java by a introduction sandesh sharma
Core java by a introduction sandesh sharma
Sandesh Sharma
 

Was ist angesagt? (20)

Java Collections
Java  Collections Java  Collections
Java Collections
 
Clojure class
Clojure classClojure class
Clojure class
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
 
Core java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaCore java by a introduction sandesh sharma
Core java by a introduction sandesh sharma
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
TeraSort
TeraSortTeraSort
TeraSort
 
Generics
GenericsGenerics
Generics
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
C# Generics
C# GenericsC# Generics
C# Generics
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
C# p9
C# p9C# p9
C# p9
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 

Ähnlich wie Lecture07 the linked-list_as_a_data_structure_v3

Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
Getachew Ganfur
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
reddy19841
 
Review of basic data structures
Review of basic data structuresReview of basic data structures
Review of basic data structures
Deepa Rani
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
Niraj Agarwal
 
Data structures linked list introduction.pptx
Data structures linked list introduction.pptxData structures linked list introduction.pptx
Data structures linked list introduction.pptx
Kalpana Mohan
 

Ähnlich wie Lecture07 the linked-list_as_a_data_structure_v3 (20)

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
 
12888239 (2).ppt
12888239 (2).ppt12888239 (2).ppt
12888239 (2).ppt
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Unsorted Sorted List_Array.pptx
Unsorted Sorted List_Array.pptxUnsorted Sorted List_Array.pptx
Unsorted Sorted List_Array.pptx
 
singly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malavsingly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malav
 
Review of basic data structures
Review of basic data structuresReview of basic data structures
Review of basic data structures
 
Data structures
Data structuresData structures
Data structures
 
00-review.ppt
00-review.ppt00-review.ppt
00-review.ppt
 
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresSearching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptx
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Data structures linked list introduction.pptx
Data structures linked list introduction.pptxData structures linked list introduction.pptx
Data structures linked list introduction.pptx
 
Funddamentals of data structures
Funddamentals of data structuresFunddamentals of data structures
Funddamentals of data structures
 

Mehr von Hariz Mustafa

Lecture04 polymorphism
Lecture04 polymorphismLecture04 polymorphism
Lecture04 polymorphism
Hariz Mustafa
 
Lecture03 inheritance
Lecture03 inheritanceLecture03 inheritance
Lecture03 inheritance
Hariz Mustafa
 
Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programming
Hariz Mustafa
 
Topic6decisionmaking
Topic6decisionmakingTopic6decisionmaking
Topic6decisionmaking
Hariz Mustafa
 
Topic5cognition and problem_solving
Topic5cognition and problem_solvingTopic5cognition and problem_solving
Topic5cognition and problem_solving
Hariz Mustafa
 
Problem solving activities
Problem solving activitiesProblem solving activities
Problem solving activities
Hariz Mustafa
 
Exercise answers chapter 1, 2 & 3
Exercise answers chapter 1, 2 & 3Exercise answers chapter 1, 2 & 3
Exercise answers chapter 1, 2 & 3
Hariz Mustafa
 
Decision making scenarios
Decision making scenariosDecision making scenarios
Decision making scenarios
Hariz Mustafa
 
Cognition and problem_solving
Cognition and problem_solvingCognition and problem_solving
Cognition and problem_solving
Hariz Mustafa
 
Chapter 6 logical_fallacies_ii
Chapter 6 logical_fallacies_iiChapter 6 logical_fallacies_ii
Chapter 6 logical_fallacies_ii
Hariz Mustafa
 
Ch08 evaluating arguments
Ch08 evaluating argumentsCh08 evaluating arguments
Ch08 evaluating arguments
Hariz Mustafa
 
Chapter 5 logical_fallacies_i
Chapter 5 logical_fallacies_iChapter 5 logical_fallacies_i
Chapter 5 logical_fallacies_i
Hariz Mustafa
 
Ch03 basic logical_concepts
Ch03 basic logical_conceptsCh03 basic logical_concepts
Ch03 basic logical_concepts
Hariz Mustafa
 

Mehr von Hariz Mustafa (20)

Lecture10 trees v3
Lecture10 trees v3Lecture10 trees v3
Lecture10 trees v3
 
Lecture09 recursion
Lecture09 recursionLecture09 recursion
Lecture09 recursion
 
Lecture04 polymorphism
Lecture04 polymorphismLecture04 polymorphism
Lecture04 polymorphism
 
Lecture03 inheritance
Lecture03 inheritanceLecture03 inheritance
Lecture03 inheritance
 
Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programming
 
Topic6decisionmaking
Topic6decisionmakingTopic6decisionmaking
Topic6decisionmaking
 
Topic5cognition and problem_solving
Topic5cognition and problem_solvingTopic5cognition and problem_solving
Topic5cognition and problem_solving
 
Topic2 argument
Topic2 argumentTopic2 argument
Topic2 argument
 
Topic2
Topic2Topic2
Topic2
 
Topic 1
Topic 1Topic 1
Topic 1
 
Problem solving activities
Problem solving activitiesProblem solving activities
Problem solving activities
 
Exercise answers chapter 1, 2 & 3
Exercise answers chapter 1, 2 & 3Exercise answers chapter 1, 2 & 3
Exercise answers chapter 1, 2 & 3
 
Decision making scenarios
Decision making scenariosDecision making scenarios
Decision making scenarios
 
Decision making
Decision makingDecision making
Decision making
 
Cognition and problem_solving
Cognition and problem_solvingCognition and problem_solving
Cognition and problem_solving
 
Chapter 6 logical_fallacies_ii
Chapter 6 logical_fallacies_iiChapter 6 logical_fallacies_ii
Chapter 6 logical_fallacies_ii
 
Chapter 4 language
Chapter 4 languageChapter 4 language
Chapter 4 language
 
Ch08 evaluating arguments
Ch08 evaluating argumentsCh08 evaluating arguments
Ch08 evaluating arguments
 
Chapter 5 logical_fallacies_i
Chapter 5 logical_fallacies_iChapter 5 logical_fallacies_i
Chapter 5 logical_fallacies_i
 
Ch03 basic logical_concepts
Ch03 basic logical_conceptsCh03 basic logical_concepts
Ch03 basic logical_concepts
 

Lecture07 the linked-list_as_a_data_structure_v3

  • 1. Chapter 7 The Linked List as a Data Structure 1
  • 2. The List ADT • A list is a list of elements. • The list of elements consist of the data acted upon by list operations. • A current position (or active position) in the list is also acted upon by list operations. 2
  • 3. List ADT Operations • insert, to insert a new item into the list; there is no current position after an insertion. • an iterator, for retrieving (by copy instead of removal) each item from the list, one at a time; at any particular moment when an item is retrieved, that item becomes the current position in the list. • find, to determine whether or not a certain item exists in a list; if the item exists, it becomes the current position. • retrieve, to retrieve (by copy instead of removal) a certain item; that item becomes the current position. • more… 3
  • 4. List ADT Operations (cont.) • replace, to replace the item at the current position with another item; the current position remains unchanged. • remove, to remove an item from a list; there is no current position after a removal. • an operation to determine whether or not the list is empty; the current position is unchanged. • an operation to empty out the list; the current position is lost. 4
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. Retrieve Function Implementation • How should we pass a key into the retrieve function and return the object? • Approach 1: Pass in a key as a parameter and pass in an object as a reference parameter (to return the object result). • Approach 2: Pass in an object by reference which has its key set to the key to search for; when the object is found in the linked list, it is assigned to the object passed in by reference. 12
  • 13. Advantages of Approach 2 • The client must declare an object, which will hold the data retrieved from the linked list. – approach 2 relieves the client of also having to declare a key; a key is already in the object. • If approach 1 is used, two T’s are needed (say, T1 and T2) for the object type and the key type • approach 2 will also be used for the find and remove functions. 13
  • 14. The Retrieval Process • An object is created in the main program. • The representative asks the customer for the key. • The representative types in the key. • The object’s data member is set to the key value; no other data members in the object are set. • The object (let’s say obj1) is passed into the retrieve function by reference. 14
  • 15. The Retrieval Process (cont.) • The struct for obj1 has an overloaded operator, used by the retrieve function for finding the object with the key: Example: if ( obj1 == ptr->info ) // found • The other information is placed in obj1 obj1 = ptr->info; • The retrieve function returns true (indicating a find) and obj1 is returned by reference parameter. 15
  • 16. The Iterator • first – returns the first element in the linked list. • getNext – returns the next element in the linked list, after the first function call or previous getNext function call – Implemented by maintaining a current pointer in the private section. – The current pointer is advanced every time getNext is called. – Returns false when the client tries to get an element beyond the end of the list (otherwise returns true). 16
  • 17. Find and Replace Functions • find – returns true only if an element with the key was found – the element itself is not returned. • replace – replaces the element at the current position with the element passed in – find and replace functions will often be used together. 17
  • 18. 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. 18
  • 19. LinkedList.hpp 1 template <template T> 2 struct Node { 3 T info; 4 Node<T> *next; 5 }; 6 7 template <template T> 8 class LinkedList 9 { 10 public: 11 LinkedList( ) { … } 19
  • 20. LinkedList.hpp (cont.) 15 ~LinkedList( ) { … } 16 void insert( const T & element ) { … } 17 bool first( T & listEl ) { … } 18 inline bool getNext( T & listEl ) { … } 19 bool find ( const T & element ) { … } 20 bool retrieve( T & element ) { … } 21 bool replace( const T & newElement ) { … } 22 bool remove( T & element ) { … } 23 bool isEmpty( ) const { … } 24 void makeEmpty( ) { … } 25 private: 26 Node<T> *start; 27 Node<T> *current; 28 }; 20
  • 21. Constructor & Destructor 2 LinkedList( ) 3 { 4 start = current = NULL; 5 } 6 7 ~LinkedList( ) 8 { 9 makeEmpty( ); 10 } 21
  • 22. insert 30 void insert( const T & element ) 31 { 32 current = NULL; 33 Node<T> *newNode = new Node<T>; 34 newNode->info = element; 35 newNode->next = start; 36 start = newNode; Inserting at the 37 } beginning of the linked list. 22
  • 23. first 49 bool first( T & listEl ) 50 { 51 if ( start == NULL ) 52 return false; 53 54 current = start; 55 listEl = start->info; 56 return true; 57 } 23
  • 24. getNext 60 bool getNext( T & listEl ) 61 { 62 if ( current == NULL ) 63 return false; 64 if ( current->next == NULL ) { 65 current = NULL; 66 return false; 67 } 68 current = current->next; 69 listEl = current->info; 70 return true; 71 } 24
  • 25. find 73 bool find( const T & element ) 75 { 76 T item; 77 if ( !first( item ) ) Overloaded 78 return false; operator if T is 79 do if ( item == element ) a struct object 80 return true; 81 while ( getNext( item ) ); 82 83 return false; 84 } 25
  • 26. retrieve 87 bool retrieve( T & element ) 88 { 89 if ( !find( element ) ) 90 return false; 91 element = current->info; 92 return true; 93 } 26
  • 27. replace 96 bool replace( const T & newElement ) 97 { 98 if ( current == NULL ) 99 return false; 100 current->info = newElement; 101 return true; 102 } 27
  • 28. remove 105 bool remove( T & element ) 106 { 107 current = NULL; 108 if ( start == NULL ) 109 return false; We need to keep 110 Node<T> *ptr = start; ptr one node in 111 if ( ptr->info == element ) { front of the node to 112 start = start->next; remove, so the first 113 delete ptr; node is a special 114 return true; case. 115 } remove continued… 28
  • 29. 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 } 126 127 return false; 128 } 29
  • 30. isEmpty 132 bool isEmpty( ) const 133 { 134 return start == NULL; 135 } 30
  • 31. makeEmpty 137 void makeEmpty( ) 138 { 139 while ( start != NULL ) { 140 current = start; 141 start = start->next; 142 delete current; 143 } 144 145 current = NULL; 146 } 31
  • 32. Reference • Childs, J. S. (2008). The Linked List as a Data Structure. C++ Classes and Data Structures. Prentice Hall. 32