Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Implementing a Sorted List as a Linked Structure CS 308 – Data Structures."— Presentation transcript:

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

2 Implementing a Sorted List as a Linked Structure Allocate memory for each new element dynamically Link the list elements together Use two pointers, currentPos and listData, to mark the current position in the list and the beginning of the list Use an integer variable, length, to store the current length of the list

3 SortedList Class Specification template struct NodeType; template 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 * listData; NodeType * currentPos; };

4 Function RetrieveItem

5 Function RetrieveItem (cont.) template void SortedType ::RetrieveItem(ItemType& item, bool& found) { NodeType * 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 }

6 Can we use Binary Search with linked lists?

7 Function InsertItem

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

9 Function InsertItem (cont.) prevLoc = location location= location->next

10 Inserting an element at the beginning of the list newNode->next= location; listData=newNode; Case 1

11 Inserting an element in the middle of the list newNode->next=location; prevLoc->next = newNode; Case 2

12 Inserting an element at the end of the list prevLoc->next = newNode; newNode->next=location; Case 3

13 Inserting into an empty list listData=newNode; newNode->next=location; Case 4

14 Function InsertItem (cont.) template void SortedType ::InsertItem(ItemType newItem) { NodeType * newNode; NodeType * predLoc; NodeType * 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; }

15 Function InsertItem (cont.) newNode = new NodeType ; 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++; }

16 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

17 Deleting the only element in the list

18 Delete the first element in the list

19 Delete an element in the middle of the list

20 Delete the last element in the list

21 Same as in the case of UnsortedList class Other SortedList functions

22 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).

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

24 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).

25 NodeType * UnsortedType ::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).

26 Comparing sorted list implementations Big-O Comparison of Sorted List Operations OperationArray Implementation Linked Implementation Class constructorO(1) DestructorO(1)O(N) MakeEmptyO(1)O(N) IsFullO(1) LengthIsO(1) ResetListO(1) GetNextItemO(1) RetrieveNextItemO(N) or O(logN)O(N) InsertItemO(N) DeleteItemO(N)

27 Exercises 9, 15 - 18


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

Similar presentations


Ads by Google