Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 ADT Unsorted List Operations Transformers clearList insertItem removeItem resetList insertFirst Observers isFull isEmpty isLength retrieveNextItem searchItem atEnd change state observe state 2

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

4 #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

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

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

8 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

9 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

10 Inserting ‘B’ into an Unsorted List ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos

11 location = new NodeType; ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos item location ‘B’

12 location->info = item ; ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos item location ‘B’

13 location->next = listData ; ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos item location ‘B’

14 listData = location ; ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos item location ‘B’

15 length++ ; ‘X’ ‘C’ ‘L’ Private data: length 4 listData currentPos item location ‘B’

16 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

17 class SortedType clearList ~SortedType removeItem. insertItem SortedType searchItem retrieveNextItem ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos

18 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.

19 Inserting ‘S’ into a Sorted List ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos predLoc location moreToSearch

20 Finding proper position for ‘S’ ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos predLoc location NULL moreToSearch true

21 ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos predLoc location Finding proper position for ‘S’ moreToSearch true

22 ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos predLoc location Finding proper position for ‘S’ moreToSearch false

23 ‘C’ ‘L’ ‘X’ Private data: length 4 listData currentPos predLoc location Inserting ‘S’ into proper position moreToSearch false ‘S’

24 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

25 //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 ppt "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."

Similar presentations


Ads by Google