Unsorted Lists CS 302 Data Structures Sections 3

  • Slides: 50
Download presentation
Unsorted Lists CS 302 – Data Structures Sections 3. 1, 3. 2, 3. 4

Unsorted Lists CS 302 – Data Structures Sections 3. 1, 3. 2, 3. 4 & 3. 5

Unsorted list • A list in which data items are placed in no particular

Unsorted 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

Unsorted List Implementations Array-based Linked-list-based

Array-based Implementation template<class Item. Type> class Unsorted. Type { public: void Make. Empty(); bool

Array-based Implementation template<class Item. Type> class Unsorted. Type { public: void Make. Empty(); bool Is. Full() const; int Length. Is() const; void Retrieve. Item(Item. Type&, bool&); void Insert. Item(Item. Type); void Delete. Item(Item. Type); void Reset. List(); bool Is. Last. Item() void Get. Next. Item(Item. Type&); private: int length; Item. Type info[MAX_ITEMS]; int current. Pos; }; current. Pos

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

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

Array-based Implementation (cont. ) template<class Item. Type> void Unsorted. Type<Item. Type>: : Make. Empty()

Array-based Implementation (cont. ) template<class Item. Type> void Unsorted. Type<Item. Type>: : Make. Empty() { length = 0; O(1) } template<class Item. Type> bool Unsorted. Type<Item. Type>: : Is. Full() const { return (length == MAX_ITEMS); } O(1)

Array-based Implementation (cont. ) template<class Item. Type> int Unsorted. Type<Item. Type>: : Length. Is()

Array-based Implementation (cont. ) template<class Item. Type> int Unsorted. Type<Item. Type>: : Length. Is() const { return length; } O(1)

Retrieve. Item (Item. Type& item, boolean& found) • Function: Retrieves list element whose key

Retrieve. Item (Item. Type& 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 some. Item whose key matches item's key, then found=true and item is a copy of some. Item; otherwise, found=false and item is unchanged, (2) List is unchanged.

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

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

Array-based Implementation (cont. ) template<class Item. Type> void Unsorted. Type<Item. Type>: : Retrieve. Item

Array-based Implementation (cont. ) template<class Item. Type> void Unsorted. Type<Item. Type>: : Retrieve. Item (Item. Type& 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 “==“ ?

Insert. Item (Item. Type item) • Function: Adds item to list • Preconditions: (1)

Insert. Item (Item. Type 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.

Insert. Item (Item. Type item) (cont’d) Insert George

Insert. Item (Item. Type item) (cont’d) Insert George

Array-based Implementation (cont. ) template<class Item. Type> void Unsorted. Type<Item. Type>: : Insert. Item

Array-based Implementation (cont. ) template<class Item. Type> void Unsorted. Type<Item. Type>: : Insert. Item (Item. Type item) { info[length] = item; length++; } O(1)

Delete. Item (Item. Type item) • Function: Deletes the element whose key matches item's

Delete. Item (Item. Type 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

easy possible better

Array-based Implementation (cont. ) template<class Item. Type> void Unsorted. Type<Item. Type>: : Delete. Item(Item.

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

Traversing the List • Reset. List current. Pos • Get. Next. Item • Is.

Traversing the List • Reset. List current. Pos • Get. Next. Item • Is. Last. Item

Reset. List • Function: Initializes current. Pos for an iteration through the list. •

Reset. List • Function: Initializes current. Pos 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. ) current. Pos template<class Item. Type> void Unsorted. Type<Item. Type>: :

Array-based Implementation (cont. ) current. Pos template<class Item. Type> void Unsorted. Type<Item. Type>: : Reset. List() { current. Pos = -1; O(1) }

Get. Next. Item (Item. Type& item) • Function: Gets the next element in list.

Get. Next. Item (Item. Type& 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 Item. Type> void Unsorted. Type<Item. Type>: : Get. Next.

Array-based Implementation (cont. ) template<class Item. Type> void Unsorted. Type<Item. Type>: : Get. Next. Item (Item. Type& item) { current. Pos++; item = info[current. Pos]; O(1) }

Array-based Implementation (cont. ) template<class Item. Type> bool Unsorted. Type<Item. Type>: : Is. Last.

Array-based Implementation (cont. ) template<class Item. Type> bool Unsorted. Type<Item. Type>: : Is. Last. Item () { return (current. Pos == length-1); O(1) }

Warning • When traversing the list using Get. Next. Item, • the list should

Warning • When traversing the list using Get. Next. Item, • the list should not be modified (i. e. , add or delete elements) This might modify the value of current. Pos.

Linked-list-based Implementation template <class Item. Type> private: int length; struct Node. Type; Node. Type<Item.

Linked-list-based Implementation template <class Item. Type> private: int length; struct Node. Type; Node. Type<Item. Type>* list. Data; Node. Type<Item. Type>* current. Pos; template<class Item. Type> }; class Unsorted. Type { public: Unsorted. Type(); ~Unsorted. Type(); void Make. Empty(); bool Is. Full() const; int Length. Is() const; void Retrieve. Item(Item. Type&, bool&); void Insert. Item(Item. Type); void Delete. Item(Item. Type); void Reset. List(); bool Is. Last. Item() const; void Get. Next. Item(Item. Type&);

Retrieve. Item (Item. Type& item, boolean& found) • Function: Retrieves list element whose key

Retrieve. Item (Item. Type& 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 some. Item whose key matches item's key, then found=true and item is a copy of some. Item; otherwise, found=false and item is unchanged, (2) List is unchanged.

Retrieve. Item

Retrieve. Item

Retrieve. Item (cont. ) template<class Item. Type> void Unsorted. Type<Item. Type>: : Retrieve. Item

Retrieve. Item (cont. ) template<class Item. Type> void Unsorted. Type<Item. Type>: : Retrieve. Item (Item. Type& item, bool& found) { Node. Type<Item. Type>* location; location = list. Data; found = false; while( (location != NULL) && !found) { if(item == location->info) { found = true; item = location->info; } else location = location->next; } } O(N)

Insert. Item (Item. Type item) • Function: Adds item to list • Preconditions: (1)

Insert. Item (Item. Type 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.

Insert. Item • Insert the item at the beginning of the list

Insert. Item • Insert the item at the beginning of the list

Insert. Item (cont. ) template <class Item. Type> void Unsorted. Type<Item. Type>: : Insert.

Insert. Item (cont. ) template <class Item. Type> void Unsorted. Type<Item. Type>: : Insert. Item (Item. Type new. Item) { Node. Type<Item. Type>* location; location = new Node. Type<Item. Type>; location->info = new. Item; location->next = list. Data; list. Data = location; length++; } O(1)

Delete. Item (Item. Type item) • Function: Deletes the element whose key matches item's

Delete. Item (Item. Type 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.

Delete. Item • Find the item to be deleted. • In order to delete

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

Delete. Item (cont. ) • Idea: compare one item ahead: • i. e. ,

Delete. Item (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 Delete. Item (cont. ) template <class Item. Type> void Unsorted. Type<Item. Type>: :

Function Delete. Item (cont. ) template <class Item. Type> void Unsorted. Type<Item. Type>: : Delete. Item(Item. Type item) Delete. Item { Node. Type<Item. Type>* location = list. Data; Node. Type<Item. Type>* temp. Location; if(item == list. Data->info) { temp. Location = list. Data; // special case list. Data = list. Data->next; } else { O(1) while(!(item == (location->next)->info)) location = location->next; O(N) // delete node at location->next temp. Location=location->next; location->next = temp. Location->next; } O(1) delete temp. Location; length--; } O(1) Overall: O(N)

Other Unsorted. List functions template<class Item. Type> Unsorted. Type<Item. Type>: : Unsorted. Type() {

Other Unsorted. List functions template<class Item. Type> Unsorted. Type<Item. Type>: : Unsorted. Type() { length = 0; list. Data = NULL; O(1) } template<class Item. Type> void Unsorted. Type<Item. Type>: : Make. Empty() { Node. Type<Item. Type>* temp. Ptr; while(list. Data != NULL) { temp. Ptr = list. Data; list. Data = list. Data->next; delete temp. Ptr; } length=0; } O(N)

Other Unsorted. List functions (cont. ) template<class Item. Type> Unsorted. Type<Item. Type>: : ~Unsorted.

Other Unsorted. List functions (cont. ) template<class Item. Type> Unsorted. Type<Item. Type>: : ~Unsorted. Type() { Make. Empty(); } O(N) template<class Item. Type> bool Unsorted. Type<Item. Type>: : Is. Full() const { Node. Type<Item. Type>* ptr; ptr = new Node. Type<Item. Type>; if(ptr == NULL) return true; else { delete ptr; return false; } } O(1)

Other Unsorted. List functions (cont. ) template<class Item. Type> int Unsorted. Type<Item. Type>: :

Other Unsorted. List functions (cont. ) template<class Item. Type> int Unsorted. Type<Item. Type>: : Length. Is() const { O(1) return length; } template<class Item. Type> int Unsorted. Type<Item. Type>: : Reset. List() { current. Pos = list. Data; O(1) }

Other Unsorted. List functions (cont. ) template<class Item. Type> void Unsorted. Type<Item. Type>: :

Other Unsorted. List functions (cont. ) template<class Item. Type> void Unsorted. Type<Item. Type>: : Get. Next. Item(Item. Type& item) { item = current. Pos->info; O(1) current. Pos = current. Pos->next; } template<class Item. Type> bool Unsorted. Type<Item. Type>: : Is. Last. Item() const { return(current. Pos == NULL); O(1) }

Comparing unsorted list implementations Big-O Comparison of Unsorted List Operations Operation Array Implementation Linked

Comparing unsorted list implementations Big-O Comparison of Unsorted List Operations Operation Array Implementation Linked Implementation Constructor O(1) Destructor O(1) O(N) Make. Empty O(1) O(N) Is. Full O(1) Length. Is O(1) Reset. List O(1) Get. Next. Item O(1) Retrieve. Item O(N) Insert. Item O(1) Delete. Item O(N)

Exercise: Write a client function that splits an unsorted list into two unsorted lists

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

void Split. Lists(const Unsorted. Type& list, Item. Type item, Unsorted. Type& list 1, Unsorted.

void Split. Lists(const Unsorted. Type& list, Item. Type item, Unsorted. Type& list 1, Unsorted. Type& list 2) { Item. Type list. Item; list 1. Make. Empty(); list 2. Make. Empty(); list. Reset. List(); while (!list. Is. Last. Item()) { list. Get. Next. Item(list. Item); if(list. Item > item) list 2. Insert. Item(list. Item); else list 1. Insert. Item(list. Item); } } What is the time complexity using big-O? O(N)

Exercise: Write a client function that merges two unsorted lists into another unsorted list

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

void Merge. Lists(Unsorted. Type list 1, Unsorted. Type list 2, Unsorted. Type& list) {

void Merge. Lists(Unsorted. Type list 1, Unsorted. Type list 2, Unsorted. Type& list) { Item. Type item; bool found; list. Make. Empty(); list 1. Reset. List(); while(!list 1. Is. Last. Item()) { list 1. Get. Next. Item(item); list. Insert. Item(item); } O(N 1)

 list 2. Reset. List(); while (!list 2. Is. Last. Item() && (!list. Is.

list 2. Reset. List(); while (!list 2. Is. Last. Item() && (!list. Is. Full())) { list 2. Get. Next. Item(item); list. Retrieve. Item(item, found); O(N 2(N 1+N 2)) if(!found) list. Insert. Item(item) } if(list. Is. Full() && (!list 2. Is. Last. Item())) cout << “Not enough space to merge the lists” << endl; } O(N 1+N 2))=O(N 1 N 2+(N 2)2) equivalent O(max(N 1, N 2(N 1+N 2)))

 list 2. Reset. List(); while (!list 2. Is. Last. Item() && (!list. Is.

list 2. Reset. List(); while (!list 2. Is. Last. Item() && (!list. Is. Full())) { list 2. Get. Next. Item(item); list 1. Retrieve. Item(item, found); O(N 2 N 1) if(!found) more efficient! list. Insert. Item(item) } if(list. Is. Full() && (!list 2. Is. Last. Item())) cout << “Not enough space to merge the lists” << endl; } O(N 1+N 2 N 1)=O(N 1 N 2) equivalent to O(max(N 1, N 2 N 1))

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

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

Detailed Analysis (cont’d) Solution 1 O(N 1 N 2+(N 2)2) Solution 2 O(N 1

Detailed Analysis (cont’d) Solution 1 O(N 1 N 2+(N 2)2) Solution 2 O(N 1 N 2) 2. N 1=O(1) N 2=O(N) O(c. N+N 2)=O(N 2) O(c. N)=O(N)

Detailed Analysis (cont’d) Solution 1 Solution 2 O(N 1 N 2+(N 2)2) O(N 1

Detailed Analysis (cont’d) Solution 1 Solution 2 O(N 1 N 2+(N 2)2) O(N 1 N 2) O(c. N+c 2)=O(N) O(c. N)=O(N) 3. N 1=O(N) N 2=O(1)