what is a list? a list is a homogeneous collection of elements, with a linear relationship between...

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 unique predecessor, and each element (except the last) has a unique successor.

Upload: susanna-george

Post on 08-Jan-2018

218 views

Category:

Documents


0 download

DESCRIPTION

class UnsortedType clearList ~UnsortedType removeItem. insertItem UnsortedType retrieveNextItem atEnd ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos

TRANSCRIPT

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

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

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

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

#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

// 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

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

// 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

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

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

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

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

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

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

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

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

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

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

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

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

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

‘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

‘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

‘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

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

//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