170 likes | 317 Views
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
E N D
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 • Size: Fixed vs. Variable
Classification of ADTs Collections Ordered Collections Unordered Collections Direct Sequential Heterogeneous Homogeneous Array Record List Stack Table Set Queue Bag
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
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.
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)
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
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
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).
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?
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 • };
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); • }
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); • }
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++; • }
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--; • }
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; • }
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()