what is a list? a list is a homogeneous collection of elements, with a linear relationship between...
DESCRIPTION
class UnsortedType clearList ~UnsortedType removeItem. insertItem UnsortedType retrieveNextItem atEnd ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPosTRANSCRIPT
![Page 1: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/1.jpg)
What is a List?• A list is a homogeneous collection of
elements, with a linear relationship between elements.
• Each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor.
![Page 2: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/2.jpg)
ADT Unsorted List OperationsTransformers
• clearList • insertItem • removeItem• resetList• insertFirst
Observers • isFull• isEmpty• isLength• retrieveNextItem • searchItem• atEnd
change state
observe state
2
![Page 3: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/3.jpg)
class UnsortedType
clearList
~UnsortedType
removeItem . . .
insertItem
UnsortedType
retrieveNextItem
atEnd
‘X’ ‘C’ ‘L’
Private data:length 3
listData
currentPos
![Page 4: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/4.jpg)
#include “NodeType.h” // unsorted.h . . .class UnsortedType{public : // LINKED LIST IMPLEMENTATION
UnsortedType ( ) ;~UnsortedType ( ) ;void clearList ( ) ;bool isFull ( ) const ;
bool isEmpty ( ) const ; int isLength ( ) const ; void searchItem ( NodeType& item, bool& found ) ;void insertItem ( NodeType item ) ;
void insertFirst ( NodeType item ) ; void removeItem ( NodeType item ) ; void resetList ( );void retrieveNextItem ( NodeType& item ) ;
bool atEnd ( ) const; private :
NodeType* listData;int length;NodeType* currentPos;
} ;4
![Page 5: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/5.jpg)
// NodeType.h . . .class NodeType{ public :
int info; // int may be replace with any type NodeType* next;
} ;
//Note: Will be using system default constructor //Note: No methods unless type for info is not a built –in type
5
![Page 6: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/6.jpg)
6
![Page 7: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/7.jpg)
// LINKED LIST IMPLEMENTATION ( unsorted.cpp )#include “NodeType.h”
UnsortedType::UnsortedType ( ) // constructor// Pre: None.// Post: List is empty.{
length = 0 ;listData = NULL;
currentPos = NULL;}
int UnsortedType::isLength ( ) const// Post: Function value = number of items in the list.{
return length;}
7
![Page 8: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/8.jpg)
void UnsortedType::searchItem( NodeType& item, bool& found ) // Pre: Key member of item is initialized.// Post: If found, item’s key matches an element’s key in the list and a copy // of that element has been stored in item; otherwise, item is unchanged.{ bool moreToSearch ; NodeType* location ; location = listData ;
found = false ;moreToSearch = ( location != NULL ) ;
while ( moreToSearch && !found ) { if ( item == location->info ) // match here
{ found = true ; item = location->info ; }else // advance pointer { location = location->next ; moreToSearch = ( location != NULL ) ;
}
} }
8
![Page 9: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/9.jpg)
void UnsortedType::insertItem ( NodeType item ) // Pre: list is not full and item is not in list.// Post: item is in the list at front; length has been
incremented; current position is not changed//NOTE: Same code for insertFirst{
NodeType* location ; // obtain and fill a node location = new NodeType ;
location->info = item ;location->next = listData ;listData = location ;length++ ;
}9
![Page 10: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/10.jpg)
Inserting ‘B’ into an Unsorted List
‘X’ ‘C’ ‘L’
Private data:length 3
listData
currentPos
![Page 11: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/11.jpg)
location = new NodeType;
‘X’ ‘C’ ‘L’
Private data:length 3
listData
currentPos
item
location
‘B’
![Page 12: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/12.jpg)
location->info = item ;
‘X’ ‘C’ ‘L’
Private data:length 3
listData
currentPos
item
location
‘B’
‘B’
![Page 13: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/13.jpg)
location->next = listData ;
‘X’ ‘C’ ‘L’
Private data:length 3
listData
currentPos
item
location
‘B’
‘B’
![Page 14: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/14.jpg)
listData = location ;
‘X’ ‘C’ ‘L’
Private data:length 3
listData
currentPos
item
location
‘B’
‘B’
![Page 15: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/15.jpg)
length++ ;
‘X’ ‘C’ ‘L’
Private data:length 4
listData
currentPos
item
location
‘B’
‘B’
![Page 16: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/16.jpg)
void UnsortedType::removeItem ( NodeType item ) // Pre: item’s key has been initialized and an element in the list has a key that matches item’s key.// Post: No element in the list has a key that matches item’s{ NodeType * location = listData; NodeType * tempLocation;
//Location node to be deleted if (item == listData->info) { tempLocation = location; listData = listData->next; } else { while (!(item == (location->next)->info)) location = location->next;
//Delete node at location->next tempLocation = location->next; location->next = (location->next)->next; } delete tempLocation; length--;}
}16
![Page 17: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/17.jpg)
class SortedType
clearList
~SortedType
removeItem . . .
insertItem
SortedType
searchItem
retrieveNextItem
‘C’ ‘L’ ‘X’
Private data:length 3
listData
currentPos
![Page 18: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/18.jpg)
insertItem algorithm for Sorted Linked List
• Find proper position for the new element in the sorted list using two pointers predLoc and location, where predLoc trails behind location.
• Obtain a node for insertion and place item in it.
• Insert the node by adjusting pointers.
• Increment length.
![Page 19: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/19.jpg)
Inserting ‘S’ into a Sorted List
‘C’ ‘L’ ‘X’
Private data:length 3
listData
currentPos
predLoc location
moreToSearch
![Page 20: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/20.jpg)
Finding proper position for ‘S’
‘C’ ‘L’ ‘X’
Private data:length 3
listData
currentPos
predLoc locationNULL
moreToSearch true
![Page 21: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/21.jpg)
‘C’ ‘L’ ‘X’
Private data:length 3
listData
currentPos
predLoc location
Finding proper position for ‘S’
moreToSearch true
![Page 22: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/22.jpg)
‘C’ ‘L’ ‘X’
Private data:length 3
listData
currentPos
predLoc location
Finding proper position for ‘S’
moreToSearch false
![Page 23: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/23.jpg)
‘C’ ‘L’ ‘X’
Private data:length 4
listData
currentPos
predLoc location
Inserting ‘S’ into proper position
moreToSearch false‘S’
![Page 24: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/24.jpg)
void SortedType::insertItem ( NodeType item ) {
NodeType* newNode ; NodeType* predLoc ; NodeType* location ;
location = listData; predLoc = NULL; moreToSearch = (location != NULL);
//find insertion point while (moreToSearch) { if (location->info < item) { predLoc = location; location = location->next; moreToSearch = (location != NULL); } else moreToSearch = false; }
24
![Page 25: What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a](https://reader034.vdocument.in/reader034/viewer/2022052514/5a4d1aed7f8b9ab05997bf25/html5/thumbnails/25.jpg)
//Prepare node for insertion newNode = new NodeType; newNode->info = item;
//Insert node into list if (predLoc == NULL) //Insert as first { newNode->next = listData; listData = newNode; } else { newNode->next = location; predLoc->next = newNode; } length++;}
removeItem is an exercise
25