10-list - Lecture 10 Meng Su 1 Today’s Topics 1 List as...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 10 Meng Su 2/16/2011 1 Today’s Topics 1. List as ADT (6.1) 2. Static Array-Based Implementation of Lists (6.2) Array- 3. 3. Dynamic Array for List Class List 2/16/2011 2 Let’s Recall ! What’s What’s Data Structure? It It is about how the data is represented and organized. Example: Example: arrays, structs, pointers …, more more sophisticated ones, which are built upon these and themselves, are: Lists Lists Stacks Stacks Queues Queues Trees Trees …… …… Data Data structures as ADTs, not only are used to store ADTs, data, but also provide operations on the data members. 2/16/2011 3 1 Consider Every Day Lists Groceries Groceries to be purchased Job toJob to-do list List List of assignments Class Class name list 2/16/2011 4 1. List as ADT What What are the data role and operation role of list ADT? 1. Data (what’s the feature of data ?) Homogeneous Homogeneous lists lists of same (but any) type of data elements Sequential Sequential elements Sounds Sounds like array, right? 2/16/2011 5 List as ADT 2. 2/16/2011 Operations (what do we want a list can do?) Be able to : 1. construct an (empty) list object 2. check if the list is empty 3. insert an element into the list 4. delete an element from the list 5. traverse (iterate through) the list to do something on each item. we we will define display() as an example of traversing. 6 2 Designing a List Class ! List Function Function members should have at least the following constructors constructors empty() empty() insert() insert() delete() delete() display() display() How How about the data members? There are several different choices! 2/16/2011 7 2. Array-Based Implementation of Lists ArrayAn An array is a viable choice for building list class Elements Elements are sequential It It is a commonly available data type Algorithm Algorithm development is easy Sequential Sequential orderings of list elements match with array array elements 2/16/2011 8 List Class (Using Static Array) declaration, Fig. 6.1A #include <iostream> <iostream> const int CAPACITY = 1024; typedef int ElementType; ElementType; class List { public: List(); //constructor bool empty() const; void insert(ElementType item, int pos); insert(ElementType void erase(int pos); erase(int //deletion void display(ostream & out) const; display(ostream private: int mySize; mySize; // current size of list ElementType myArray[CAPACITY]; myArray[CAPACITY]; }; //--- end of List class text page 262-269 //--262- 2/16/2011 9 3 Implementing Operations Constructor Constructor Set Set an empty list: mySize = 0 Empty Empty Check Check if mySize == 0 Insert Insert Shift Shift elements to right right of insertion point Delete Delete Shift Shift elements back Also adjust size up or down Traverse Traverse Use Use a loop from 0th element to mySize – 1 mySize 2/16/2011 10 Implementation on Static Array: Fig. 6.1B //--//--- Definition of insert() void List::insert(ElementType item, int pos) { if (mySize == CAPACITY) exit(1); if (pos < 0 || pos > mySize) return; // shift array elements right to make room for item for(int i = mySize; i > pos; i--) i--) myArray[i] = myArray[i - 1]; // insert item at pos and increase list size myArray[pos] = item; mySize++; // don’t forget this! } //--see //--see text page 262-269 for more. 262……. 2/16/2011 11 List Class (Using Static Array) : Driver, Fig 6.1C (Using Driver, #include "List.h" int main() { List intList; intList; for (int i = 0; i < 9; i++) // Test insert() { cout << "Inserting " << i << " at position " << i/2 << endl; intList.insert(i, i/2); //Insert i at position i/2 //Test output cout << intList << endl; } .... } 2/16/2011 12 4 The Execution Results… 2/16/2011 13 Problems of List Class with Static Array Stuck Stuck with "one size fits all" Could Could be wasting space Could Could run out of space Better to be able to change the capacity th Thus Thus we consider creating a List class with List dynamicallydynamically-allocated array. 2/16/2011 14 3. Dynamic Array for List Class List Changes Changes required in data members Eliminate Eliminate const size: CAPACITY for array Change Change to a pointer Add Add variable data member to store capacity Little Little or no changes required for interfaces empty(), empty(), display(), erase(), insert()… 2/16/2011 15 5 DynamicDynamic-Array for List Class List Note Note data changes in Fig. 6.2A, List.h List.h Note Note implementation file Fig. 6.2B, List.cpp List.cpp Changes Changes to constructor Add Addition of other functions to deal with dynamically allocated allocated memory Note Note testing of various features in Fig. 6.2C, the demo the program 2/16/2011 16 List Class (Using Dynamic Array) Fig. 6.2 (Using #include <iostream> typedef int ElementType; class List { public: List( int maxSize = 1024 ); //constructors (include default one). // The following are big three ~List(); //1. destructor List(const List & origList); //2. copy constructor List & operator=(const List & origList); //3. overload assignment operator overload assignment operator bool empty() const; void insert(ElementType item, int pos); void erase(int pos); void display(ostream & out) const; private: int mySize; // current size of list stored in array int myCapacity; // capacity of array ElementType * myArrayPtr; // pointer to dynamically array }; 2/16/2011 17 DynamicDynamic-Allocation for List Class List Now Now possible to specify different sized lists List aList1;// the default myCapacity is 1024 myCapacity cin cin >> maxListSize; // suppose input 500 // List aList2(maxListSize); List aList2(maxListSize); 2/16/2011 18 6 Definition of class constructor List::List(int maxSize) { mySize = 0; myCapacity myCapacity = maxSize; maxSize; myArrayPtr myArrayPtr = new ElementType[maxSize]; } maxSize -1 maxSize 2/16/2011 19 Big three – 1. Destructor: ~C(); ~C(); C is the class name is The The destructor delete allocated memory in constructor by new automatically without explicitly calling it when the object of this class is out of the scope. List::~List(){ delete myArrayPtr;} 2/16/2011 20 Big Big three -2. Copy Constructor: C(const C & orgl) C(const "deep "deep copy" of an object in program: List stCopy(st); stCopy(st); same as List List stCopy = st; st; means we want a exact copy of st of st, a existed List object If If copy constructor is NOT defined, when List aListCopy(aList); List aListCopy(aList); or List List stCopy = st; st; the result is called "shallow" copy 2/16/2011 to be continued,…. 21 7 Reference Some Some slides are adapted from slides of text: ADTs, Data Structures and Problem Solving with C++, Second Edition by Nyhoff Text website: http://cs.calvin.edu/books/c++/ds/2e/ Clip Clip images are selected from WebPages of Microsoft Clip Art and Media 2/16/2011 22 8 ...
View Full Document

This note was uploaded on 10/21/2011 for the course CMPSC 122 taught by Professor Formitchev during the Spring '08 term at Penn State.

Ask a homework question - tutors are online