Download presentation
Presentation is loading. Please wait.
Published byTyrone Taylor Modified over 8 years ago
1
114 3/30/98 CSE 143 Collection ADTs [Chapter 4]
2
115 3/30/98 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
116 3/30/98 Classification of ADTs Collections Ordered CollectionsUnordered Collections Array List Queue Stack Record Set DirectSequential Bag HeterogeneousHomogeneous Table
4
117 3/30/98 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
118 3/30/98 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
119 3/30/98 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
120 3/30/98 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
121 3/30/98 List Terminology Head: First element in list Tail: Last element in list Cursor: “Current” element of list Size: Number of elements in list aList: HeadTail Cursor elem 0 elem 1 elem 2 elem N-1... (Size=N)
9
122 3/30/98 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
123 3/30/98 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
124 3/30/98 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
125 3/30/98 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
126 3/30/98 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
127 3/30/98 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
128 3/30/98 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
129 3/30/98 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
130 3/30/98 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()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.