You are on page 1of 31

C++ Plus Data Structures

Nell Dale
Chapter 5,6

Linked Unsorted List


Linked Sorted List
Circular Linked List
Doubly Linked List

Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus 1


What is a List?

 A list is a homogeneous collection of


elements, with a linear relationship
between elements.

 That is, each list element (except the


first) has a unique predecessor, and
each element (except the last) has a
unique successor.

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;
}

int UnsortedType::LengthIs ( ) const


// Post: Function value = number of items in the list.
{
return length;
}
6
void UnsortedType::MakeEmpty()
{
NodeType *tempPtr;
While (listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete temPtr;
}
length = 0;
}
UnsortedType::~ UnsortedType()
{
NodeType *tempPtr;
While (listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete temPtr;
}
}
7
void UnsortedType::RetrieveItem( ItemType& 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 ( 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 ;
// 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 3

listData ‘X’ ‘C’ ‘L’


currentPos

10
‘B’
location = new NodeType;
item

location

Private data:
length 3

listData ‘X’ ‘C’ ‘L’


currentPos

11
‘B’
location->info = item ;
item

location ‘B’

Private data:
length 3

listData ‘X’ ‘C’ ‘L’


currentPos

12
‘B’
location->next = listData ;
item

location ‘B’

Private data:
length 3

listData ‘X’ ‘C’ ‘L’


currentPos

13
‘B’
listData = location ;
item

location ‘B’

Private data:
length 3

listData ‘X’ ‘C’ ‘L’


currentPos

14
‘B’
length++ ;
item

location ‘B’

Private data:
length 4

listData ‘X’ ‘C’ ‘L’


currentPos

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”

void SortedType:: InsertItem ( ItemType item )


// Pre: List has been initialized. List is not full. item is not in list.
// List is sorted by key member.
// Post: item is in the list. List is still sorted.
{
.
.
.

19
Inserting ‘S’ into a Sorted List
predLoc location

Private data:
length 3

listData ‘C’ ‘L’ ‘X’


currentPos

moreToSearch

20
Finding proper position for ‘S’
predLoc location
NULL

Private data:
length 3

listData ‘C’ ‘L’ ‘X’


currentPos

moreToSearch true

21
Finding proper position for ‘S’
predLoc location

Private data:
length 3

listData ‘C’ ‘L’ ‘X’


currentPos

moreToSearch true

22
Finding proper position for ‘S’
predLoc location

Private data:
length 3

listData ‘C’ ‘L’ ‘X’


currentPos

moreToSearch false

23
Inserting ‘S’ into proper position
predLoc location

Private data:
length 4

listData ‘C’ ‘L’ ‘X’


currentPos

‘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’

‘B’ ‘C’ ‘L’ ‘T’ ‘V’ ‘Y’ listData

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.

listData ‘A’ ‘C’ ‘F’ ‘T’ ‘Z’

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
};

3000 ‘A’ NULL


. back . info . next
31

You might also like