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
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.
ADT Unsorted List OperationsTransformers
• clearList • insertItem • removeItem• resetList• insertFirst
Observers • isFull• isEmpty• isLength• retrieveNextItem • searchItem• atEnd
change state
observe state
2
class UnsortedType
clearList
~UnsortedType
removeItem . . .
insertItem
UnsortedType
retrieveNextItem
atEnd
‘X’ ‘C’ ‘L’
Private data:length 3
listData
currentPos
#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
// 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
6
// 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
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
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
Inserting ‘B’ into an Unsorted List
‘X’ ‘C’ ‘L’
Private data:length 3
listData
currentPos
location = new NodeType;
‘X’ ‘C’ ‘L’
Private data:length 3
listData
currentPos
item
location
‘B’
location->info = item ;
‘X’ ‘C’ ‘L’
Private data:length 3
listData
currentPos
item
location
‘B’
‘B’
location->next = listData ;
‘X’ ‘C’ ‘L’
Private data:length 3
listData
currentPos
item
location
‘B’
‘B’
listData = location ;
‘X’ ‘C’ ‘L’
Private data:length 3
listData
currentPos
item
location
‘B’
‘B’
length++ ;
‘X’ ‘C’ ‘L’
Private data:length 4
listData
currentPos
item
location
‘B’
‘B’
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
class SortedType
clearList
~SortedType
removeItem . . .
insertItem
SortedType
searchItem
retrieveNextItem
‘C’ ‘L’ ‘X’
Private data:length 3
listData
currentPos
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.
Inserting ‘S’ into a Sorted List
‘C’ ‘L’ ‘X’
Private data:length 3
listData
currentPos
predLoc location
moreToSearch
Finding proper position for ‘S’
‘C’ ‘L’ ‘X’
Private data:length 3
listData
currentPos
predLoc locationNULL
moreToSearch true
‘C’ ‘L’ ‘X’
Private data:length 3
listData
currentPos
predLoc location
Finding proper position for ‘S’
moreToSearch true
‘C’ ‘L’ ‘X’
Private data:length 3
listData
currentPos
predLoc location
Finding proper position for ‘S’
moreToSearch false
‘C’ ‘L’ ‘X’
Private data:length 4
listData
currentPos
predLoc location
Inserting ‘S’ into proper position
moreToSearch false‘S’
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
//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