1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.

Slides:



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

Linear Lists – Array Representation
DATA STRUCTURES USING C++ Chapter 5
Lists: An internal look
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
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 4 ADT Sorted List.
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.
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 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
Data Structures Using C++ 2E
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.
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 Chapter 17-1 Templates and Exceptions Dale/Weems.
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 first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
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 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.
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.
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 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
Chapter 14 Dynamic Data and Linked Lists
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
Data Abstraction: The Walls
C++ Plus Data Structures
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Unsorted Lists CS3240, L. Grewe.
Data Abstraction: The Walls
Chapter 16 Linked Structures
Chapter 9 Priority Queues and Sets
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
Data Abstraction: The Walls
Presentation transcript:

1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems

2 Chapter 13 Topics l Meaning of a List l Insertion and Deletion of List Elements l Selection Sort of List Elements l Insertion and Deletion using a Sorted List l Binary Search in a Sorted List l Order of Magnitude of a Function l Declaring and Using C Strings Using typedef with Arrays

3 What is a List? l A list is a variable-length, linear collection of homogeneous elements l Linear means that each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor

4 Basic Kinds of ADT Operations l Constructors -- create a new instance (object) of an ADT l Transformers -- change the state of one or more of the data values of an instance l Observers -- allow client to observe the state of one or more of the data values of an instance without changing them l Iterators -- allow client to access the data values in sequence 4

ADT List Operations Transformers n Insert n Delete n Sort Observers n IsEmpty n IsFull n Length n IsPresent change state observe state 5

6 ADT List Operations Iterator n Reset n GetNextItem l Reset prepares for the iteration l GetNextItem returns the next item in sequence l No transformer can be called between calls to GetNextItem (Why?) Access in Sequence

ADT Unsorted List Data Components length data[0.. MAX_LENGTH -1 ] currentPos number of elements in list array of list elements used in iteration 7

8 Array-based class List Reset IsFull Length IsPresent Delete IsEmpty Insert GetNexItem Private data: length data [ 0] [1] [2] [MAX_LENGTH-1] currentPos SelSort

9 // Specification file array-based list (“list.h”) const int MAX_LENGTH = 50; typedef int ItemType; class List // Declares a class data type { public: // Public member functions List(); // constructor bool IsEmpty () const; bool IsFull () const; int Length () const; // Returns length of list void Insert (ItemType item); void Delete (ItemType item); bool IsPresent(ItemType item) const; void SelSort (); void Reset (); ItemType GetNextItem (); private: // Private data members int length; // Number of values currently stored ItemType data[MAX_LENGTH]; int CurrentPos; // Used in iteration }; 9

10 Sorted and Unsorted Lists UNSORTED LIST Elements are placed into the list in no particular order SORTED LIST List elements are in sorted in some way -- either numerically or alphabetically

11 // Implementation file array-based list (“list.cpp”) #include “list.h” #include using namespace std; int List::Length () const // Post: Return value is length { return length; } bool List::IsFull () const // Post: Return value is true if length is equal // to MAX_LENGTH and false otherwise { return (length == MAX_LENGTH); } 11

12 List::List () // Constructor // Post: length == 0 { length = 0; } void List::Insert (/* in */ ItemType item) // Pre: length < MAX_LENGTH && item is assigned // Post: == item && // length == + 1 { data[length] = item; length++; } 12

13 Assertions l Abstract assertions (located in the specification file): written in terms that are meaningful to the user of the ADT l Implementation assertions (located in the implementation file): more precise by referring directly to data structures and algorithms

Before Inserting 64 into an Unsorted List length 3 data [0] 15 [1] 39 [ 2] -90 [ 3]. [MAX_LENGTH-1] The item will be placed into the length location, and length will be incremented 14 item 64

After Inserting 64 into an Unsorted List length 4 data [0] 15 [1] 39 [ 2] -90 [ 3] 64. [MAX_LENGTH-1] The item will be placed into the length location, and length will be incremented 15 item 64

16 bool List::IsEmpty () const // Post: Return value is true if length is equal // to zero and false otherwise { return (length == 0); } bool List::IsPresent( /* in */ ItemType item) const // Searches the list for item, reporting whether found // Post: Function value is true, if item is in // data[0.. length-1] and is false otherwise { int index = 0; while (index < length && item != data[index]) Index++; return (index < length); } 16

17 void List::Delete ( /* in */ ItemType item) // Pre: length > 0 && item is assigned // Post: IF item is in data array at entry //First occurrence of item is no longer in array // && length == - 1 //ELSE // length and data array are unchanged { int index = 0; while (index < length && item != data[index]) index++; // IF item found, move last element into // item’s place if (index < length) { data[index] = data[length - 1]; length--; } 17

Deleting 39 from an Unsorted List index: 0 39 has not been matched 18 item 39 length 4 data [0] 15 [1] 39 [ 2] -90 [ 3] 64. [MAX_LENGTH-1]

Deleting 39 from an Unsorted List index: 1 19 item 39 length 4 data [0] 15 [1] 39 [ 2] -90 [ 3] 64. [MAX_LENGTH-1] 39 has been matched

Deleting 39 from an Unsorted List index: 1 20 item 39 length 4 data [0] 15 [1] 64 [ 2] -90 [ 3] 64. [MAX_LENGTH-1] Placed copy of last list element into the position where 39 was before

Deleting 39 from an Unsorted List index: 1 21 item 39 length 3 data [0] 15 [1] 64 [ 2] -90 [ 3] 64. [MAX_LENGTH-1] Decremented length

22 What should currentPos be initialized to in order to access the first item? void List::Reset() // Post: currentPos has been initialized. { currentPos = 0; } 22 Preparing for Iteration

23 Iteration Operator ItemType GetNextItem () // Pre: No transformer has been executed since last call // Post:Return value is // Current position has been updated // If last item returned, next call returns first item { ItemType item; item = data[currentPos]; if (currentPos == length - 1) currentPos = 0; else currentPos++; return item; }

24 Reset currentPos: 0 24 item ? length 4 data [0] 15 [1] 64 [ 2] -90 [ 3] 64. [MAX_LENGTH-1]

25 GetNextItem currentPos: 1 25 item 15 length 4 data [0] 15 [1] 64 [ 2] -90 [ 3] 64. [MAX_LENGTH-1] currentPos is incremented item is returned

26 The End of Chapter 13 Part 1