CS 302 – Data Structures Sections 3.1, 3.2, 3.4 & 3.5

Slides:



Advertisements
Similar presentations
Sorted Lists CS Data Structures Sections 4.1, 4.2 & 4.3.
Advertisements

Stacks, Queues, and Linked Lists
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.
Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the.
C++ Plus Data Structures ADTs Unsorted List and Sorted List
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
What is a Queue? A queue is a FIFO “first in, first out” structure.
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Unsorted Lists CS 308 – Data Structures. What is a list? A list is a homogeneous collection of elements. Linear relationship between elements: (1) Each.
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.
Chapter 4 ADT Sorted List.
Sorted Lists CS Data Structures. Sorted List Specification (partial) InsertItem (ItemType item) Function: Adds item to list Preconditions: (1) List.
Implementing an Unsorted List as a Linked Structure CS 308 – Data Structures.
Recursion CS 308 – Data Structures. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the.
Heaps CS 308 – Data Structures.
1 expanded by J. Goetz Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Nell Dale Chapter 3 ADTs Unsorted List and Sorted List Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
1 Chapter 6 Object-Oriented Software Development.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Doubly Linked Lists CS 308 – Data Structures. Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info.
Dynamic Structures & Arrays.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
Chapter 4 ADT Sorted List.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Heaps and Priority Queues CS 3358 – Data Structures.
Binary Search Trees. What is a binary tree? Property 1: each node can have up to two successor nodes.
Chapter 3 ADT Unsorted List. Lecture 7 List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element.
Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations.
C++ Classes and Data Structures Jeffrey S. Childs
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Recursion CS 3358 – Data Structures. Big Picture Our objective is to write a recursive program and convince ourselves it is correct with the minimum amount.
Recursion CS 302 – Data Structures Chapter 7. What is recursion? smaller A technique that solves problem by solving smaller versions of the same problem!
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 4 ADT Sorted List. Sorted Type Class Interface Diagram SortedType class IsFull GetLength ResetList DeleteItem PutItem MakeEmpty GetItem Private.
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.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
1 Nell Dale Lecture 3 ADTs Unsorted List and Sorted List Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by Reneta.
CSI 1340 Introduction to Computer Science II Chapter 3 ADTs Unsorted List and Sorted List.
1 Chapter 4 ADT Sorted List. 2 Sorted Type Class Interface Diagram SortedType class IsFull LengthIs ResetList DeleteItem InsertItem MakeEmpty RetrieveItem.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
C++ Plus Data Structures ADTs Unsorted List and Sorted List
TA: Nouf Al-Harbi Data Structures LAB 3 TA: Nouf Al-Harbi
C++ Plus Data Structures
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Unsorted Lists CS3240, L. Grewe.
Chapter 16 Linked Structures
CSI 1340 Introduction to Computer Science II
Yan Shi CS/SE 2630 Lecture Notes
Data Structures and Algorithms Memory allocation and Dynamic Array
TA: Nouf Al-Harbi Data Structures LAB 2 TA: Nouf Al-Harbi
Presentation transcript:

CS 302 – Data Structures Sections 3.1, 3.2, 3.4 & 3.5 Unsorted Lists CS 302 – Data Structures Sections 3.1, 3.2, 3.4 & 3.5

Unsorted list Sorted list A list in which data items are placed in no particular order. Sorted list A list in which data items are placed in a particular order. Key: a member of the class whose value is used to determine the order of the items in the list.

Unsorted List Implementations Array-based Linked-list-based

Array-based Implementation template<class ItemType> class UnsortedType { public: void MakeEmpty(); bool IsFull() const; int LengthIs() const; void RetrieveItem(ItemType&, bool&); void InsertItem(ItemType); void DeleteItem(ItemType); void ResetList(); bool IsLastItem() void GetNextItem(ItemType&); private: int length; ItemType info[MAX_ITEMS]; int currentPos; }; currentPos

Length: length of the list (different from array size!)

Array-based Implementation (cont.) template<class ItemType> void UnsortedType<ItemType>::MakeEmpty() { length = 0; }   bool UnsortedType<ItemType>::IsFull() const return (length == MAX_ITEMS); O(1) O(1)

Array-based Implementation (cont.) template<class ItemType> int UnsortedType<ItemType>::LengthIs() const { return length; } O(1)

RetrieveItem (ItemType& item, boolean& found) Function: Retrieves list element whose key matches item's key (if present). Preconditions: (1) List has been initialized, (2) Key member of item has been initialized. Postconditions: (1) If there is an element someItem whose key matches item's key, then found=true and item is a copy of someItem; otherwise, found=false and item is unchanged, (2) List is unchanged.

Item is in the list Item is NOT in the list Retrieve Judy

Array-based Implementation (cont.) template<class ItemType> void UnsortedType<ItemType>::RetrieveItem (ItemType& item, bool& found) { int location = 0; found = false;   while( (location < length) && !found) if (item == info[location]) { found = true; item = info[location]; } else location++; O(N) Who should overload “==“ ?

InsertItem (ItemType item) Function: Adds item to list Preconditions: (1) List has been initialized, (2) List is not full, (3) item is not in list (i.e., no duplicates) Postconditions: item is in list.

InsertItem (ItemType item) (cont’d) Insert George

Array-based Implementation (cont.) template<class ItemType> void UnsortedType<ItemType>::InsertItem (ItemType item) { info[length] = item; length++; } O(1)

DeleteItem (ItemType item) Function: Deletes the element whose key matches item's key Preconditions: (1) List has been initialized, (2) Key member of item has been initialized, (3) There is only one element in list which has a key matching item's key. Postconditions: No element in list has a key matching item's key.

easy possible better

Array-based Implementation (cont.) template<class ItemType> void UnsortedType<ItemType>::DeleteItem(ItemType item) { int location = 0; while(item != info[location]) location++;   info[location] = info[length - 1]; length--; } O(N) O(N) O(1)

Traversing the List currentPos ResetList GetNextItem IsLastItem

ResetList Function: Initializes currentPos for an iteration through the list. Preconditions: List has been initialized Postconditions: Current position is prior to first element in list.

Array-based Implementation (cont.) currentPos template<class ItemType> void UnsortedType<ItemType>::ResetList() { currentPos = -1; }   O(1)

GetNextItem (ItemType& item) Function: Gets the next element in list. Preconditions: (1) List has been initialized, (2) Current position is not the last item. Postconditions: (1) Current position is updated to next position, (2) item is a copy of element at current position.

Array-based Implementation (cont.) template<class ItemType> void UnsortedType<ItemType>::GetNextItem (ItemType& item) { currentPos++; item = info[currentPos]; } O(1)

Array-based Implementation (cont.) template<class ItemType> bool UnsortedType<ItemType>::IsLastItem () { return (currentPos == length-1); } O(1)

Warning When traversing the list using GetNextItem, the list should not be modified (i.e., add or delete elements) This might modify the value of currentPos.

Linked-list-based Implementation template <class ItemType> struct NodeType;   template<class ItemType> class UnsortedType { public: UnsortedType(); ~UnsortedType(); 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; };

RetrieveItem (ItemType& item, boolean& found) Function: Retrieves list element whose key matches item's key (if present). Preconditions: (1) List has been initialized, (2) Key member of item has been initialized. Postconditions: (1) If there is an element someItem whose key matches item's key, then found=true and item is a copy of someItem; otherwise, found=false and item is unchanged, (2) List is unchanged.

RetrieveItem

RetrieveItem (cont.) O(N) template<class ItemType> void UnsortedType<ItemType>::RetrieveItem (ItemType& item, bool& found) { NodeType<ItemType>* location;   location = listData; found = false; while( (location != NULL) && !found) { if(item == location->info) { found = true; item = location->info; } else location = location->next; O(N)

InsertItem (ItemType item) Function: Adds item to list Preconditions: (1) List has been initialized, (2) List is not full, (3) item is not in list (i.e., no duplicates) Postconditions: item is in list.

InsertItem Insert the item at the beginning of the list

InsertItem (cont.) O(1) template <class ItemType> void UnsortedType<ItemType>::InsertItem (ItemType newItem) { NodeType<ItemType>* location;   location = new NodeType<ItemType>; location->info = newItem; location->next = listData; listData = location; length++; } O(1)

DeleteItem (ItemType item) Function: Deletes the element whose key matches item's key Preconditions: (1) List has been initialized, (2) Key member of item has been initialized, (3) There is only one element in list which has a key matching item's key. Postconditions: No element in list has a key matching item's key.

DeleteItem Find the item to be deleted. In order to delete it, we must change the “next” pointer in the previous node!

DeleteItem (cont.) Idea: compare one item ahead: i.e., (location->next)->info) Works efficiently since the item to be deleted is on the list (i.e., precondition). Special case: deleting the first node!

Function DeleteItem (cont.) template <class ItemType> void UnsortedType<ItemType>::DeleteItem(ItemType item) { NodeType<ItemType>* location = listData; NodeType<ItemType>* tempLocation;   if(item == listData->info) { tempLocation = listData; // special case listData = listData->next; } else { while(!(item == (location->next)->info)) location = location->next; // delete node at location->next tempLocation=location->next; location->next = tempLocation->next; O(1) delete tempLocation; length--; } O(N) O(1) Overall: O(N) O(1)

Other UnsortedList functions template<class ItemType> UnsortedType<ItemType>::UnsortedType() { length = 0; listData = NULL; }   void UnsortedType<ItemType>::MakeEmpty() NodeType<ItemType>* tempPtr; while(listData != NULL) { tempPtr = listData; listData = listData->next; delete tempPtr; length=0; O(1) O(N)

Other UnsortedList functions (cont.) template<class ItemType> UnsortedType<ItemType>::~UnsortedType() { MakeEmpty(); }   bool UnsortedType<ItemType>::IsFull() const NodeType<ItemType>* ptr; ptr = new NodeType<ItemType>; if(ptr == NULL) return true; else { delete ptr; return false; O(N) O(1)

Other UnsortedList functions (cont.) template<class ItemType> int UnsortedType<ItemType>::LengthIs() const { return length; }   int UnsortedType<ItemType>::ResetList() currentPos = listData; O(1) O(1)

Other UnsortedList functions (cont.)   template<class ItemType> void UnsortedType<ItemType>::GetNextItem(ItemType& item) { item = currentPos->info; currentPos = currentPos->next; } bool UnsortedType<ItemType>::IsLastItem() const return(currentPos == NULL); O(1) O(1)

Comparing unsorted list implementations Big-O Comparison of Unsorted List Operations Operation Array Implementation Linked Implementation Constructor O(1) Destructor O(N) MakeEmpty IsFull LengthIs ResetList GetNextItem RetrieveItem InsertItem DeleteItem

Exercise: Write a client function that splits an unsorted list into two unsorted lists using the following specification. SplitLists (UnsortedType list, ItemType item, UnsortedType& list1, UnsortedType& list 2) Function: Divides list into two lists according to the key of item. Preconditions: list has been initialized and is not empty. Postconditions: list1 contains all the items of list whose keys are less than or equal to item’s key; list2 contains all the items of list whose keys are greater than item’s key; list remains unchanged.

O(N) What is the time complexity using big-O? void SplitLists(const UnsortedType& list, ItemType item, UnsortedType& list1, UnsortedType& list2) { ItemType listItem; list1.MakeEmpty(); list2.MakeEmpty(); list.ResetList(); while (!list.IsLastItem()) { list.GetNextItem(listItem); if(listItem > item) list2.InsertItem(listItem); else list1.InsertItem(listItem); } What is the time complexity using big-O? O(N)

Exercise: Write a client function that merges two unsorted lists into another unsorted list using the following specification. MergeLists (UnsortedType list1, UnsortedType list2, UnsortedType& list) Function: Merge list1 and list2 into another list. Preconditions: list1 and list2 have been initialized. Postconditions: list contains all the items of list1 and list2; list does not contain duplicates.

void MergeLists(UnsortedType list1, UnsortedType list2, void MergeLists(UnsortedType list1, UnsortedType list2, UnsortedType& list) { ItemType item; bool found; list.MakeEmpty(); list1.ResetList(); while(!list1.IsLastItem()) { list1.GetNextItem(item); list.InsertItem(item); } O(N1)

O(N1+N2(N1+N2))=O(N1N2+(N2)2) list2.ResetList(); while (!list2.IsLastItem() && (!list.IsFull())) { list2.GetNextItem(item); list.RetrieveItem(item, found); if(!found) list.InsertItem(item) } if(list.IsFull() && (!list2.IsLastItem())) cout << “Not enough space to merge the lists” << endl; O(N2(N1+N2)) O(N1+N2(N1+N2))=O(N1N2+(N2)2) equivalent O(max(N1, N2(N1+N2)))

O(N2N1) O(N1+N2N1)=O(N1N2) more efficient! list2.ResetList(); while (!list2.IsLastItem() && (!list.IsFull())) { list2.GetNextItem(item); list1.RetrieveItem(item, found); if(!found) list.InsertItem(item) } if(list.IsFull() && (!list2.IsLastItem())) cout << “Not enough space to merge the lists” << endl; O(N2N1) more efficient! O(N1+N2N1)=O(N1N2) equivalent to O(max(N1, N2N1))

Detailed Analysis O(N1N2+(N2)2) O(N1N2) Solution 1 Solution 2 O(N1N2+(N2)2) O(N1N2) 1. N1=N2=O(N) O(2N2)=O(N2) O(N2)

Detailed Analysis (cont’d) Solution 1 Solution 2 O(N1N2+(N2)2) O(N1N2) 2. N1=O(1) N2=O(N) O(cN+N2)=O(N2) O(cN)=O(N)

Detailed Analysis (cont’d) Solution 1 Solution 2 O(N1N2+(N2)2) O(N1N2) 3. N1=O(N) N2=O(1) O(cN+c2)=O(N) O(cN)=O(N)