Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the.

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.
C++ Plus Data Structures ADTs Unsorted List and Sorted List
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.
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.
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
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.
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.
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.
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.
Chapter 4 ADT Sorted List.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Applied Arrays: Lists and Strings. 2 Chapter 13 Topics  Meaning of a List  Insertion and Deletion of List Elements  Selection Sort of List Elements.
Chapter 3 ADT Unsorted List. Lecture 7 List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element.
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
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.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
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.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
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 Chapter 13 Applied Arrays: Lists and Strings Dale/Weems/Headington.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
1 C++ Plus Data Structures Abstract Data Types Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by.
CSCE 210 Data Structures and Algorithms
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
Chapter 5 ADTs Stack and Queue.
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Unsorted Lists CS3240, L. Grewe.
Chapter 16 Linked Structures
Chapter 9 Priority Queues and Sets
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
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:

Chapter 3 ADT Unsorted List

List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the last has a unique successor. Length The number of items in a list; The length can vary over time. 2

List Definitions Unsorted list A list in which data items are placed in no particular order; the only relationship between data elements is the list predecessor and successor relationships. Sorted list A list that is sorted by the value in the key; There is a semantic relationship among the keys of the items in the list. Key The attributes that are used to determine the logical order of the list. 3

ADT Operations Constructor Transformer Observer Iterator 4

Sorted and Unsorted Lists UNSORTED LIST Elements are placed into the list in no particular order. SORTED LIST List elements are in an order that is sorted in some way -either numerically, -alphabetically by the elements themselves, or by a component of the element -called a KEY member 5

ADT Unsorted List Operations Transformers MakeEmpty PutItem DeleteItem Observers IsFull GetLength GetItem Iterators ResetList GetNextItem change state observe state process all 6

Generic Data Type A generic data type is a type for which the operations are defined but the types of the items being manipulated are not defined. One way to simulate such a type for our UnsortedList ADT is via a user-defined class ItemType with member function ComparedTo returning an enumerated type value LESS, GREATER, or EQUAL. 7

// SPECIFICATION FILE ( unsorted.h ) #include “ItemType.h” class UnsortedType// declares a class data type { public : // 8 public member functions void UnsortedType ( ); boolIsFull ( ) const; intGetLength ( ) const ; // returns length of list ItemType GetItem ( ItemType item, bool& found); void PutItem ( ItemType item ); void DeleteItem ( ItemType item ); void ResetList ( ); ItemType GetNextItem (); private : // 3 private data members int length; ItemType info[MAX_ITEMS]; intcurrentPos; }; 8

Class Interface Diagram UnsortedType class IsFull GetLength ResetList DeleteItem PutItem UnsortedType GetItem GetNextItem Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentPos 9

10 // IMPLEMENTATION FILE ARRAY-BASED LIST ( unsorted.cpp ) #include “itemtype.h” void UnsortedType::UnsortedType ( ) // Pre: None. // Post: List is empty. { length = 0; } void UnsortedType::InsertItem ( ItemType item ) // Pre: List has been initialized. List is not full. // item is not in list. // Post: item is in the list. { info[length] = item; length++; } 10

Before Inserting Henry into an Unsorted List 11 length 3 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ]. [MAX_ITEMS-1] The item will be placed into the length location, and length will be incremented. 11

After Inserting Henry into an Unsorted List length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Henry... [MAX_ITEMS-1] 12

13 int UnsortedType::GetLength( ) const // Pre: List has been inititalized. // Post: Function value == ( number of elements in // list ). { return length; } bool UnsortedType::IsFull ( ) const // Pre: List has been initialized. // Post: Function value == ( list is full ). { return ( length == MAX_ITEMS ); } 13

14 ItemType UnsortedType::GetItem ( ItemType item, bool& found ) // Pre: Key member of item is initialized. // Post: If found, item’s key matches an element’s key in the list // and a copy of that element is returned; // otherwise, input item is returned. { bool moreToSearch; int location = 0; found = false; moreToSearch = ( location < length ); while ( moreToSearch && !found ) { switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++; moreToSearch = ( location < length ); break; case EQUAL : found = true; item = info[ location ]; break; } } return item; } 14

Getting Ivan from an Unsorted List moreToSearch: true found: false location: 0 length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Henry. [MAX_ITEMS-1] 15

Getting Ivan from an Unsorted List length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Henry. [MAX_ITEMS-1] moreToSearch: true found: false location: 1 16

Getting Ivan from an Unsorted List length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Henry. [MAX_ITEMS-1] moreToSearch: true found: false location: 2 17

Getting Ivan from an Unsorted List length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Henry.. [MAX_ITEMS-1] moreToSearch: true found: false location: 3 18

Getting Ivan from an Unsorted List length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Henry... [MAX_ITEMS-1] moreToSearch: false found: false location: 4 19

void UnsortedType::DeleteItem ( ItemType item ) // Pre: item’s key has been inititalized. // An element in the list has a key that matches item’s. // Post: No element in the list has a key that matches item’s. { int location = 0 ; while (item.ComparedTo (info [location] ) != EQUAL ) location++; // move last element into position where item was located info [location] = info [length - 1 ] ; length-- ; } 20

Deleting Bradley from an Unsorted List location: 0 length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Henry. [MAX_ITEMS-1] Key Bradley has not been matched. 21

Deleting Bradley from an Unsorted List location: 1 Key Bradley has been matched. length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Henry. [MAX_ITEMS-1] 22

Deleting Bradley from an Unsorted List location: 1 length 4 info [ 0 ] Maxwell [ 1 ] Henry [ 2 ] Asad [ 3 ] Henry. [MAX_ITEMS-1] Placed copy of last list element into the position where the key Bradley was before. 23

Deleting Bradley from an Unsorted List location: 1 length 3 info [ 0 ] Maxwell [ 1 ] Henry [ 2 ] Asad [ 3 ] Henry. [MAX_ITEMS-1] Decremented length. 24

void UnsortedType::ResetList ( ) // Pre: List has been inititalized. // Post: Current position is prior to first element in list. { currentPos = -1; } ItemType UnsortedType::GetNextItem () // Pre: List has been initialized. Current position is defined. // Element at current position is not last in list. // Post: Current position is updated to next position. // item is a copy of element at current position. { currentPos++; return info [currentPos]; } 25

Private data value ComparedTo Print Initialize class ItemType ItemType Class Interface Diagram 26

// SPECIFICATION FILE( itemtype.h ) const int MAX_ITEM = 5 ; enum RelationType { LESS, EQUAL, GREATER }; class ItemType// declares class data type { public : // 3 public member functions RelationType ComparedTo ( ItemType ) const; void Print ( ) const; void Initialize ( int number ) ; private : // 1 private data member intvalue ; // could be any different // type, including a class } ; Specifying class ItemType 27

// IMPLEMENTATION FILE( itemtype.cpp ) // Implementation depends on the data type of value. #include “itemtype.h” #include RelationType Itemtype::ComparedTo(ItemType otherItem) const { if ( value < otherItem.value ) return LESS; else if ( value > otherItem.value ) return GREATER; else return EQUAL; } void ItemType::Print ( ) const { using namespace std; cout << value << endl; } void ItemType::Initialize ( int number ) { value = number; } 28

UML diagrams 29