Top Banner
Implementing a Sorted List as a Linked Structure CS 308 – Data Structures
26

Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Implementing a Sorted List as a Linked

Structure CS 308 – Data Structures

Page 2: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

SortedList Class Specification template <class ItemType>struct NodeType; template<class ItemType>class SortedType { public: SortedType(); ~SortedType(); void MakeEmpty(); bool IsFull() const; int LengthIs() const; void RetrieveItem(ItemType&, bool&); void InsertItem(ItemType); void DeleteItem(ItemType); void ResetList(); bool IsLastItem() const; void GetNextItem(ItemType&);

private: int length; NodeType<ItemType>* listData; NodeType<ItemType>* currentPos;};

Page 3: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Function RetrieveItem

Page 4: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Function RetrieveItem (cont.)template<class ItemType>void SortedType<ItemType>::RetrieveItem(ItemType& item,

bool& found){

NodeType<ItemType>* location; 

location = listData; found = false;

while( (location != NULL) && !found) { 

if (location->info < item) location = location->next; else if (location->info == item) { found = true; item = location->info; }

else location = NULL; // no reason to continue }}

Page 5: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Can we use Binary Search with linked lists?

Page 6: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Function InsertItem

Page 7: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Function InsertItem (cont.)

• Can we compare one item ahead?? (like in the unsorted list case?)

• In general, we must keep track of the previous pointer, as well as the current pointer.

Page 8: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Function InsertItem (cont.)

prevLoc = location

location=location->next

Page 9: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Inserting an element at the beginning of the list

newNode->next=location;

listData=newNode;

Case 1

Page 10: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Inserting an element in the middle of the list

newNode->next=location;prevLoc->next = newNode;Case 2

Page 11: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Inserting an element atthe end of the list

prevLoc->next = newNode;newNode->next=location;Case 3

Page 12: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Inserting into an empty list

listData=newNode;newNode->next=location;

Case 4

Page 13: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Function InsertItem (cont.)template <class ItemType>void SortedType<ItemType>::InsertItem(ItemType newItem){ NodeType<ItemType>* newNode; NodeType<ItemType>* predLoc; NodeType<ItemType>* location; bool stop; 

stop = false; location = listData; predLoc = NULL; 

while( location != NULL && !stop) { 

if (location->info < newItem) { predLoc = location; location = location->next; } else stop = true; }

Page 14: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Function InsertItem (cont.) newNode = new NodeType<ItemType>; newNode->info = newItem;  if (predLoc == NULL) { newNode->next = listData; cases (1) and (4) listData = newNode; } else { newNode->next = location; predLoc->next = newNode; cases (2) and (3) } length++;}

Page 15: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Function DeleteItem

• The DeleteItem we wrote for unsorted lists works for sorted lists as well

• Another possibility is to write a new DeleteItem based on the following cases

Page 16: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Deleting the only element in the list

Page 17: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Delete the first element in the list

Page 18: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Delete an element in the middle of the list

Page 19: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Delete the last element in the list

Page 20: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

• Same as in the case of UnsortedList class

Other SortedList functions

Page 21: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Write a client function that takes two lists (unsorted or sorted) and returns a Boolean indicating whether the second list is a sublist of the first.

(i.e., the first list contains all the elements of the second list but may also contain other elements).

Page 22: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

bool IsSubList (SortedType list1, SortedType list2){

ItemType item;bool subList, found;

sublist = true;

list2.ResetList();while ( !list2.IsLastItem() && sublist ) {

list2.GetNextItem (item);list1.RetrieveItem (item, found);if (!found) sublist = false;

}return sublist;

}

Page 23: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Write a member function that returns a pointer to the minimum node (i.e. the node storing the smallest value) of an unsorted list.

Precondition: list is not empty.

How would you implement the same function if the list

was sorted? (assume that the elements in a sorted list are sorted in increasing order).

Page 24: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

NodeType<ItemType>* UnsortedType<ItemType>::MinNode(){

NodeType<ItemType * location, *tempLocation;ItemType minItem;

minItem = listData->info; tempLocation = listData;

location = listData; while (location->next != NULL) { location = location->next;

if (location->info < minItem) { minItem=location->info; tempLocation=location;}

}return tempLocation;

}

If the list is sorted, then you just need to return “listData” (pointer to the first element).

If the list is sorted, then you just need to return “listData” (pointer to the first element).

Page 25: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Comparing sorted list implementations

Big-O Comparison of Sorted List Operations

Operation Array Implementation

Linked Implementation

Class constructor O(1) O(1)

Destructor O(1) O(N)

MakeEmpty O(1) O(N)

IsFull O(1) O(1)

LengthIs O(1) O(1)

ResetList O(1) O(1)

GetNextItem O(1) O(1)

RetrieveNextItem O(N) or O(logN) O(N)

InsertItem O(N) O(N)

DeleteItem O(N) O(N)

Page 26: Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.

Exercises

• 9, 15 - 18