C++ Classes and Data Structures Jeffrey S. Childs

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked Lists Geletaw S..
Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Linked Lists CENG 213 Data Structures.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Review Learn about linked lists
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Doubly Linked Lists CS 308 – Data Structures. Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Queues CS 302 – Data Structures Sections 5.3 and 5.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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
C++ Classes and Data Structures Jeffrey S. Childs
C++ Plus Data Structures
Doubly linked lists.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 16-2 Linked Structures
C++ Classes and Data Structures Jeffrey S. Childs
C++ Plus Data Structures
Doubly Linked List Implementation
Chapter 16 Linked Structures
CENG 218 Classes and Data Structures
Lists CMSC 202, Version 4/02.
Doubly Linked List Implementation
Presentation transcript:

C++ Classes and Data Structures Jeffrey S. Childs Chapter 10 The Linked List as a Data Structure Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

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

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…

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

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

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

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

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)

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

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

List Implementation In C++, lists can be implemented with arrays or linked lists Recall two 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”

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

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 DataType’s are needed (say, DataType1 and DataType2) for the object type and the key type approach 2 will also be used for the find and remove functions

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

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

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)

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

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

LinkedList Implementation (cont.) We won’t use a header node Arrays of linked lists are used in other data structures, such as HashTables and Graphs – many linked lists could be empty A header node would simplify code, but an empty linked list with a header node would use more memory space a dynamic header node wouldn’t help – the LinkedList constructor would create the header node

LinkedList.h 1 template <class DataType> 2 struct Node { 3 DataType info; 4 Node<DataType> *next; 5 }; 6 7 template <class DataType> 8 class LinkedList 9 { 10 public: 11 LinkedList( ); 12 LinkedList( const LinkedList<DataType> & aplist ); LinkedList.h continued…

LinkedList.h (cont.) 13 ~LinkedList( ); 14 LinkedList<DataType> & operator =( 15 const LinkedList<DataType> & rlist ); 16 void insert( const DataType & element ); 17 bool first( DataType & listEl ); 18 inline bool getNext( DataType & listEl ); 19 bool find ( const DataType & element ); 20 bool retrieve( DataType & element ); 21 bool replace( const DataType & newElement ); 22 bool remove( DataType & element ); 23 bool isEmpty( ) const; 24 void makeEmpty( ); private section next…

LinkedList.h (cont.) 25 private: 26 Node<DataType> *start; 27 Node<DataType> *current; 28 inline void deepCopy( 29 const LinkedList<DataType> & original ); 30 }; 31 32 #include "LinkedList.cpp"

Constructor / Copy Constructor 1 template <class DataType> 2 LinkedList<DataType>::LinkedList( ) 3 { 4 start = current = NULL; 5 } 6 7 template <class DataType> 8 LinkedList<DataType>::LinkedList( 9 const LinkedList<DataType> & aplist ) 10 { 11 deepCopy( aplist ); 12 }

Destructor 13 template <class DataType> 14 LinkedList<DataType>::~LinkedList( ) 15 { 16 makeEmpty( ); 17 }

Overloaded Assignment Operator 18 template <class DataType> 19 LinkedList<DataType> & LinkedList<DataType>:: 20 operator =( const LinkedList<DataType> & rlist ) 21 { 22 if ( this == &rlist ) 23 return *this; 24 makeEmpty( ); 25 deepCopy( rlist ); 26 return *this; 27 }

insert 28 template <class DataType> 29 void LinkedList<DataType>::insert( 30 const DataType & element ) 31 { 32 current = NULL; 33 Node<DataType> *newNode = new Node<DataType>; 34 newNode->info = element; 35 newNode->next = start; 36 start = newNode; 37 } Inserting at the beginning of the linked list makes this a ( 1 ) function.

first 48 template <class DataType> 49 bool LinkedList<DataType>::first( DataType & listEl ) 50 { 51 if ( start == NULL ) 52 return false; 53 54 current = start; 55 listEl = start->info; 56 return true; 57 }

getNext getNext continued… 58 template <class DataType> 59 inline bool LinkedList<DataType>::getNext( 60 DataType & listEl ) 61 { 62 if ( current == NULL ) 63 return false; 64 if ( current->next == NULL ) { 65 current = NULL; 66 return false; 67 } getNext continued…

getNext (cont.) 68 current = current->next; 69 listEl = current->info; 70 return true; 71 }

find 72 template <class DataType> 73 bool LinkedList<DataType>::find( 74 const DataType & element ) 75 { 76 DataType item; 77 if ( !first( item ) ) 78 return false; 79 do if ( item == element ) 80 return true; 81 while ( getNext( item ) ); 82 83 return false; 84 } Overloaded operator if DataType is a struct object

find (cont.) 72 template <class DataType> 73 bool LinkedList<DataType>::find( 74 const DataType & element ) 75 { 76 DataType item; 77 if ( !first( item ) ) 78 return false; 79 do if ( item == element ) 80 return true; 81 while ( getNext( item ) ); 82 83 return false; 84 } Note that this is a ( n ) function.

retrieve 85 template <class DataType> 86 bool LinkedList<DataType>::retrieve( 87 DataType & element ) 88 { 89 if ( !find( element ) ) 90 return false; 91 element = current->info; 92 return true; 93 }

replace 94 template <class DataType> 95 bool LinkedList<DataType>::replace( 96 const DataType & newElement ) 97 { 98 if ( current == NULL ) 99 return false; 100 current->info = newElement; 101 return true; 102 }

remove 103 template <class DataType> 104 bool LinkedList<DataType>::remove( 105 DataType & element ) 106 { 107 current = NULL; 108 if ( start == NULL ) 109 return false; remove continued…

remove (cont.) 110 Node<DataType> *ptr = start; 111 if ( ptr->info == element ) { 112 element = ptr->info; 113 start = start->next; 114 delete ptr; 115 return true; 116 } We need to keep ptr one node in front of the node to remove, so the first node is a special case. remove continued…

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

isEmpty 130 template <class DataType> 131 bool LinkedList<DataType>::isEmpty( ) const 132 133 { 134 return start == NULL; 135 }

makeEmpty 136 template <class DataType> 137 void LinkedList<DataType>::makeEmpty( ) 138 { 139 while ( start != NULL ) { 140 current = start; 141 start = start->next; 142 delete current; 143 } 144 145 current = NULL; 146 }

deepCopy 147 template <class DataType> 148 inline void LinkedList<DataType>::deepCopy( 149 const LinkedList<DataType> & original ) 150 { 151 start = current = NULL; 152 if ( original.start == NULL ) 153 return; 154 Node<DataType> *copyptr = start = 155 new Node<DataType>; 156 Node<DataType> *originalptr = original.start; 157 copyptr->info = originalptr->info; deepCopy continued…

deepCopy (cont.) 158 if ( originalptr == original.current ) 159 current = copyptr; 160 while ( originalptr->next != NULL ) { 161 originalptr = originalptr->next; 162 copyptr->next = new Node<DataType>; 163 copyptr = copyptr->next; 164 copyptr->info = originalptr->info; 165 if ( originalptr == original.current ) 166 current = copyptr; 167 } 168 copyptr->next = NULL; 169 } Special code to set current pointer in copy correctly.

Sorted Linked List A sorted linked list is one in which the elements are placed in order (usually by key value) start 3 4 5 7 9

Sorted Linked List (cont.) Insertion is no longer ( 1 ), but is ( n ) on average Find and retrieve functions are faster, on average, if the element is not in the list Once you reach a point in the list where the element you are looking for is greater, then you know it is not in the list Still ( n ) on average

Circular Linked List current

Doubly-Linked List … start Given a pointer to a node in a doubly-linked list, we can remove the node in ( 1 ) time. This isn’t possible in a singly-linked list, since we must have a pointer to the node in front of the one we want to remove.

Doubly-Linked List … start template <class DataType> struct DLNode { DataType info; DLNode<DataType> *next; DLNode<DataType> *back; }; Each node is made from a struct that looks something like this.

Doubly-Linked List start …

Doubly-Linked List start ptr …

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;