Unsorted Lists CS 308 – Data Structures. What is a list? A list is a homogeneous collection of elements. Linear relationship between elements: (1) Each.

Slides:



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

Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
ADTs unsorted List and Sorted List
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.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
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.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
1 Chapter 6 Object-Oriented Software Development.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
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.
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.
Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.
Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations.
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 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.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
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.
CS 302 – Data Structures Sections 3.1, 3.2, 3.4 & 3.5
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.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
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.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
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.
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
Chapter 13 Applied Arrays: Lists and Strings
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
C++ Plus Data Structures
Unsorted Lists CS3240, L. Grewe.
Chapter 16 Linked Structures
CSI 1340 Introduction to Computer Science II
Queues.
Chapter 13 Applied Arrays: Lists and Strings
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:

Unsorted Lists CS 308 – Data Structures

What is a list? A list is a homogeneous collection of elements. Linear relationship between elements: (1) Each element except the first one has a unique predecessor. (2) Each element except the last one has a unique successor. Length: the number of items in the list.

What is an unsorted list? A list in which data items are placed in no particular order. What is a 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.

Operations MakeEmpty Boolean IsFull int LengthIs RetrieveItem (ItemType& item, Boolean& found) InsertItem (ItemType item) DeleteItem (ItemType item) ResetList void GetNextItem (ItemType& item)

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.

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. Postconditions: item is in list.

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.

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

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

Unsorted List Implementation template 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; };

Unsorted List Implementation template void UnsortedType ::MakeEmpty() { length = 0; } template bool UnsortedType ::IsFull() const { return (length == MAX_ITEMS); } template int UnsortedType ::LengthIs() const { return length; } (cont.)

Unsorted List Implementation template void UnsortedType ::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++; } (cont.)

Unsorted List Implementation template void UnsortedType ::InsertItem (ItemType item) { info[length] = item; length++; } (cont.)

Unsorted List Implementation (cont.) template void UnsortedType ::DeleteItem(ItemType item) { int location = 0; while(item != info[location]) location++; info[location] = info[length - 1]; length--; }

Unsorted List Implementation (cont.) template void UnsortedType ::ResetList() { currentPos = -1; } template bool UnsortedType ::IsLastItem() { return(currentPos == length - 1); } template void UnsortedType ::GetNextItem (ItemType& item) { currentPos++; item = info[currentPos]; }

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.

ItemType listItem; list.ResetList(); while ( !list.IsLastItem()) { list.GetNextItem(listItem); if(listItem > item) { if (!list2.IsFull()) list2.InsertItem(listItem); } else { if ( !list1.IsFull()) list1.InsertItem(listItem); }

Exercises 1, 9