Professional Documents
Culture Documents
Nell Dale
Chapter 5,6
2
ADT Unsorted List Operations
Transformers
MakeEmpty
change state
InsertItem
DeleteItem
Observers
IsFull observe state
LengthIs
RetrieveItem
Iterators
process all
ResetList
3
GetNextItem
#include “ItemType.h” // unsorted.h
struct NodeType
{
ItemType info;
NodeType* next;
}
class UnsortedType
{
public : // LINKED LIST IMPLEMENTATION
UnsortedType ( ) ;
~UnsortedType ( ) ;
void MakeEmpty ( ) ;
bool IsFull ( ) const ;
int LengthIs ( ) const ;
void RetrieveItem ( ItemType& item, bool& found ) ;
void InsertItem ( ItemType item ) ;
void DeleteItem ( ItemType item ) ;
void ResetList ( );
void GetNextItem ( ItemType& item ) ;
private :
NodeType* listData;
int length;
NodeType* currentPos;
} ;
4
class UnsortedType
UnsortedType
Private data:
MakeEmpty
length 3
~UnsortedType
listData ‘X’ ‘C’ ‘L’
RetrieveItem
InsertItem currentPos
DeleteItem
.
.
.
GetNextItem
5
// LINKED LIST IMPLEMENTATION ( unsorted.cpp )
#include “itemtype.h”
UnsortedType::UnsortedType ( ) // constructor
// Pre: None.
// Post: List is empty.
{
length = 0 ;
listData = NULL;
}
9
Inserting ‘B’ into an Unsorted List
Private data:
length 3
10
‘B’
location = new NodeType;
item
location
Private data:
length 3
11
‘B’
location->info = item ;
item
location ‘B’
Private data:
length 3
12
‘B’
location->next = listData ;
item
location ‘B’
Private data:
length 3
13
‘B’
listData = location ;
item
location ‘B’
Private data:
length 3
14
‘B’
length++ ;
item
location ‘B’
Private data:
length 4
15
void UnsortedType::DeletetItem ( ItemType item )
// Pre: list is not full and item is not in list.
// Post: item is in the list; length has been incremented.
{
NodeType* location = listData ;
NodeType* tempLocation;
if (item == listData->info)
{ tempLocation = location;
listData = listData->next;
}
else
{
while (!(item==(location->next)->info)
location = location->next;
tempLocation = location->next;
location->next= (location->next)->next;
}
delete tempLocation;
length-- ;
}
16
class SortedType
SortedType
Private data:
MakeEmpty
length 3
~SortedType
listData ‘C’ ‘L’ ‘X’
RetrieveItem
InsertItem currentPos
DeleteItem
.
.
.
GetNextItem
17
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.
18
Implementing SortedType
member function InsertItem
// LINKED LIST IMPLEMENTATION (sorted.cpp)
#include “ItemType.h”
19
Inserting ‘S’ into a Sorted List
predLoc location
Private data:
length 3
moreToSearch
20
Finding proper position for ‘S’
predLoc location
NULL
Private data:
length 3
moreToSearch true
21
Finding proper position for ‘S’
predLoc location
Private data:
length 3
moreToSearch true
22
Finding proper position for ‘S’
predLoc location
Private data:
length 3
moreToSearch false
23
Inserting ‘S’ into proper position
predLoc location
Private data:
length 4
‘S’
moreToSearch false
24
ADT Sorted List Operations
Transformers
MakeEmpty
change state
InsertItem
DeleteItem
Observers
IsFull observe state
LengthIs
RetrieveItem
Iterators
process all
ResetList
GetNextItem
class SortedType<char>
SortedType
Private data:
MakeEmpty
length 3
~SortedType
listData ‘C’ ‘L’ ‘X’
RetrieveItem
InsertItem currentPos
DeleteItem
.
.
.
GetNextItem
26
What is a Circular Linked List?
A circular linked list is a list in which
every node has a successor; the “last”
element is succeeded by the “first”
element.
listData ‘B’ ‘C’ ‘L’ ‘T’ ‘V’ ‘Y’
27
void FindItem ( NodeType * listData, ItemType item,
NodeType *& location, NodeType *&predLoc)
{ bool moreToSearch=true;
location = listData->next;
predLoc = listData;
found = false;
while (moreToSearch && !found)
{ if (item < location->info)
moreToSearch = false;
else
if (item = location->info)
found = true;
else
{ predLoc = location;
location = location->next;
moreToSearch = (location!=listData->next);
}
}
}
28
void SortedType::InsertItem ( ItemType item )
// Pre: list is not full and item is not in list.
// Post: item is in the list; length has been incremented.
{
NodeType *newNode, *predLoc, *location ;
bool found;
newNode = new NodeType ;
newNode->info = item ;
if (listData!=NULL)
{ FindItem(listData, item, location, predLoc, found)
newNode-> next = predLoc->next;
predLoc->next = newNode;
if (listData->info < item)
listData = newNode;
}
else
{ listData= newNode ;
newNode->next = newNode;
}
length++ ;
}
29
What is a Doubly Linked List?
A doubly linked list is a list in which each
node is linked to both its successor and
its predecessor.
30
Each node contains two pointers
template< class ItemType >
struct NodeType {
ItemType info; // Data member
NodeType * back; // Pointer to predecessor
NodeType * next; // Pointer to successor
};