1 / 17

CSE 143

CSE 143. Collection ADTs [Chapter 4]. Collection ADTs. Many standard ADTs are for collections Data structures that manage groups of data Some terms: Order of data: Linear vs. Nonlinear (list vs. set) Method of access: Direct vs. Sequential Type of data: Homogenous vs. Heterogeneous

kemp
Download Presentation

CSE 143

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 143 Collection ADTs [Chapter 4]

  2. Collection ADTs • Many standard ADTs are for collections • Data structures that manage groups of data • Some terms: • Order of data: Linear vs. Nonlinear (list vs. set) • Method of access: Direct vs. Sequential • Type of data: Homogenous vs. Heterogeneous • Size: Fixed vs. Variable

  3. Classification of ADTs Collections Ordered Collections Unordered Collections Direct Sequential Heterogeneous Homogeneous Array Record List Stack Table Set Queue Bag

  4. Collections in C++ • Direct support for records (struct/class) and arrays • Other ADTs must be implemented • No absolute standards • Many conventional operations and concepts • Standard (Template) Library (new) • contains many ADTs • Our Plan: • Look at each ADT • Learn the conventional operations • Consider possible implementations

  5. Arrays as ADTs [Section 4.3] • Attributes of array type: • Linear sequence of homogeneous elements • Fixed length • Direct access • Operations on arrays • Direct indexing using [] operator • Built in arrays provide no bounds checking • No searching, sorting, insertion, etc.

  6. Record ADT [Section 4.4] • Attributes of record type: • Nonlinear collection of heterogeneous elements (fields) • Fixed number of elements • Direct access • Operations on records • Accessing elements directly through . operator • We’ll typically use classes to implement records, providing encapsulation via access functions. • In C++, structs are classes where members are public (by default)

  7. List ADT [Section 4.5] • Attributes of list type: • Linear sequence of homogeneous elements • Varying number of elements • Sequential access • Operations on lists • Sequential access through a “cursor” • Test if full, empty, cursor at end • Insert, delete elements

  8. List Terminology • Head: First element in list • Tail: Last element in list • Cursor: “Current” element of list • Size: Number of elements in list Cursor aList: ... elem0 elem1 elem2 elemN-1 (Size=N) Head Tail

  9. Lists in C++ • No predefined list type, so write our own • Use the class construct • Member functions for list operations • Private data for list representation • We’ll look at a list of integers, but later we’ll see how to write more general kinds of lists (or other ADTs).

  10. List Interface // In IntList.h const int MAX_SIZE = 20; class IntList { public: IntList (); // Create an empty list int data (); // get data item // (the one pointed to by cursor) bool isEmpty (); // Is the list empty? bool isFull (); // can add more elements? int sizeOf(); void reset (); // move cursor to head void advance(); // advance cursor bool endOfList(); // cursor past the end?

  11. List Interface (2) • // insert the item before the cursor. • void insertBefore(int item); • // insert the item after cursor. • void insertAfter(int item); • void deleteItem(); // delete the item@cursor • private: • int items[MAX_SIZE]; • int last; // position of last element • int cursor; // position of current element • };

  12. List Implementation (1) • #include “IntList.h” • IntList::IntList() { • cursor = -1; • last = cursor; • } • bool IntList::isEmpty() { • return (sizeOf() == 0); • } • bool IntList::isFull() { • return (sizeOf() == MAX_SIZE); • }

  13. List Implementation (2) • int IntList::sizeOf() { • return last+1; } • void IntList::reset() { • cursor = 0; • } • void IntList::advance() { • assert(!endOfList()); • cursor++; • } • bool IntList::endOfList() { • return (cursor > last); • }

  14. List Implementation (3) • int IntList::data() { • assert (!endOfList()); • return items[cursor]; • } • void IntList::insertBefore(int item) { • assert (!isFull()); • if (last == -1) • cursor = 0; • else • for (int i=last; i>=cursor; i--) • items[i+1] = items[i]; • items[cursor] = item; • last++; • }

  15. List Implementation (4) • void IntList::insertAfter(int item) { • assert(!isFull() && !endOfList()); • for (int i=last; i>= cursor+1; i--) • items[i+1] = items[i]; • items[cursor+1] = item; • last++; • cursor++; • } • void IntList::deleteItem() { • for (int i=cursor; i < last; i++) • items[i] = items[i+1]; • last--; • }

  16. Client of List • #include “IntList.h” • void main() { • IntList grades; • int aGrade; • while (cin >> aGrade) • grades.insertAfter(aGrade); • for (grades.reset(); • !grades.endOfList(); • grades.advance()) • cout << grades.data() << endl; • }

  17. Summary • Collection ADTs • Distinguished by order, access, types of data • Arrays: • ordered, direct access by index, homogeneous • Records: • unordered,direct access by name, heterogeneous • Lists: • ordered, sequential access, homogeneous • Head, Tail, Cursor • size(), isEmpty(), isFull() • reset(), endOfList(), advance(), data() • insertBefore(), insertAfter(), deleteItem()

More Related