cse 143 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CSE 143 PowerPoint Presentation
Download Presentation
CSE 143

Loading in 2 Seconds...

play fullscreen
1 / 17

CSE 143 - PowerPoint PPT Presentation


  • 112 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CSE 143' - kemp


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cse 143
CSE 143

Collection ADTs

[Chapter 4]

collection adts
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
Classification of ADTs

Collections

Ordered Collections

Unordered Collections

Direct

Sequential

Heterogeneous

Homogeneous

Array

Record

List

Stack

Table

Set

Queue

Bag

collections in c
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
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
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
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
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
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
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
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
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
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
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
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
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
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()