What is a List?

advertisement
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 Operations
Transformers
•
•
•
•
•
clearList
insertItem
removeItem
resetList
insertFirst
change state
Observers
•
•
•
•
•
•
isFull
isEmpty
isLength
retrieveNextItem
searchItem
atEnd
observe state
2
class UnsortedType
UnsortedType
clearList
~UnsortedType
retrieveNextItem
insertItem
removeItem
.
.
.
atEnd
Private data:
length
listData
currentPos
3
‘X’
‘C’
‘L’
#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;
NodeType* next;
};
// int may be replace with any type
//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 ( )
// Pre: None.
// Post: List is empty.
{
length = 0 ;
listData = NULL;
currentPos = NULL;
}
// constructor
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
Private data:
length
listData
currentPos
3
‘X’
‘C’
‘L’
item
‘B’
location = new NodeType;
location
Private data:
length
listData
currentPos
3
‘X’
‘C’
‘L’
item
‘B’
location->info = item ;
‘B’
location
Private data:
length
listData
currentPos
3
‘X’
‘C’
‘L’
item
‘B’
location->next = listData ;
‘B’
location
Private data:
length
listData
currentPos
3
‘X’
‘C’
‘L’
item
‘B’
listData = location ;
‘B’
location
Private data:
length
listData
currentPos
3
‘X’
‘C’
‘L’
item
length++ ;
‘B’
‘B’
location
Private data:
length
listData
currentPos
4
‘X’
‘C’
‘L’
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
SortedType
clearList
~SortedType
searchItem
insertItem
removeItem
.
.
.
retrieveNextItem
Private data:
length
listData
currentPos
3
‘C’
‘L’
‘X’
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
predLoc
location
Private data:
length
3
listData
currentPos
moreToSearch
‘C’
‘L’
‘X’
Finding proper position for ‘S’
predLoc
NULL
location
Private data:
length
3
‘C’
listData
currentPos
moreToSearch
true
‘L’
‘X’
Finding proper position for ‘S’
predLoc
location
Private data:
length
3
‘C’
listData
currentPos
moreToSearch
true
‘L’
‘X’
Finding proper position for ‘S’
predLoc
location
Private data:
length
3
‘C’
listData
currentPos
moreToSearch
false
‘L’
‘X’
Inserting ‘S’ into proper position
predLoc
location
Private data:
length
4
‘C’
listData
‘L’
‘X’
currentPos
‘S’
moreToSearch
false
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
Download