11-list - Lecture 11 Meng Su 2/23/2011 1 Exam 2 Reminder...

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 11 Meng Su 2/23/2011 1 Exam 2 Reminder Exam Exam 2 will be on next Wednesday (3/2) (3/2) Cover Cover material taught till Monday (3/1). Review Review Lecture Slides, Textbook (Ch 4-7.1), Lab & Assignments. Ask for help if you need (visit my office) if 2/23/2011 2 Today’s Topics 1. 2. 2/23/2011 The “big three”: destructor, copy constructor, overload assignment operator “=“ Linked list - A new structure to implement list ADT 3 1 1.List Class (Using Dynamic Array) Fig. 6.2 (Using #include <iostream> <iostream> typedef int ElementType; ElementType; class List { public: List( int maxSize = 1024 ); //constructors (include default one). ~List(); //1. destructor List(const List & origList); //2. copy constructor origList); List & operator=(const List & origList); origList); //3. overload assignment operator overload assignment operator bool empty() const; void insert(ElementType item, int pos); insert(ElementType void erase(int pos); erase(int void display(ostream & out) const; display(ostream private: int mySize; // current size of list stored in array int mySize; myCapacity; myCapacity; // capacity of array ElementType * myArrayPtr; // pointer to dynamically array myArrayPtr; }; 2/23/2011 4 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/23/2011 5 Definition of class constructor List::List(int maxSize) { mySize = 0; myCapacity myCapacity = maxSize; maxSize; myArrayPtr myArrayPtr = new ElementType[maxSize]; } maxSize 2/23/2011 maxSize -1 6 2 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/23/2011 7 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 an 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/23/2011 to be continued,…. 8 Codes of Copy Constructor In In the class declaration: List(const List & origList); origList); The The implementation: List::List(const List::List(const List & origList) { origList) mySize = origList.mySize; origList.mySize; myCapacity myCapacity = origList.myCapacity; origList myArrayPtr myArrayPtr = new ElementType[myCapacity]; ElementType[myCapacity]; for(int for(int i = 0; i < mySize; i++) mySize; myArrayPtr[ myArrayPtr[i] = origList.myArrayPtr[i]; origList.myArrayPtr[ } 2/23/2011 9 3 Big three 3. Overloading Assignment Operator: C & operator=(const C & orgl) operator=(const orgl) Default Default assignment operator makes shallow copy aListCopy = aList; Shallow copy! Wasted! Memory leaks! Can Can cause memory leak, dynamically-allocated memory has nothing dynamicallypointing to it Need Need to overload = with deletion of old one, allocate new one and do the deep copy! 2/23/2011 10 Definition of the assignment operator List & List::operator=(const List & origList) origList) { if (this != & origList) origList) // check for list = list { mySize = origList.mySize; origList.mySize; myCapacity = origList.myCapacity; origList.myCapacity; delete myArrayPtr delete myArrayPtr; myArrayPtr = new ElementType[myCapacity]; ElementType[myCapacity]; //--//--- Copy origList's array into this new array for(int for(int i = 0; i < myCapacity; i++) myCapacity; myArrayPtr[ myArrayPtr[i] = origList.myArrayPtr[i]; origList.myArrayPtr[ } return *this; } 2/23/2011 11 Summary: Big Three If a class allocates memory at run time using the new, then it should provide the big three … 1. A destructor // avoid memory leakage 2. A copy constructor // deep copy for initialization 3. Overloading assignment operator // deep copy assignment // and avoid memory leakage Otherwise, it will cause big two trouble: trouble: 1. shallow copy // two heads monster! 2. memory leakage // memory may be used up! Think about them using the diagrams! 2/23/2011 12 4 DynamicDynamic-Array for List Class – Whole Picture 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/23/2011 13 Problem of Array-Implemented List Arrayinsert() insert() and erase() functions inefficient for lists erase() Those Those that change frequently Those Those with many insertions and deletions So … We look for an alternative implementation. 2/23/2011 14 Compare vehicles and data structure single data object such as: double, Time,… Array, array based list array based list Linked list (see next slide) 2/23/2011 15 5 2/23/2011 16 2. Linked List Linked Linked list nodes contain Data Data part – stores an element of the list Next Next part – stores link/pointer to next element (when no next element, null value) 2/23/2011 17 Node Definition class Node { public: ElementType data; Node * next; }; data next 2/23/2011 18 6 Working with Nodes Declaring Declaring pointers data ptr next Node * ptr; Allocate Allocate ptr ptr = new Node; Access the data and next part of node (*ptr).data and (*ptr).next (*ptr).next or ptrptr->data and ptrptr->next Deallocate Deallocate delete ptr; Draw Draw diagrams for better understanding!!! 2/23/2011 19 Class List List Same as array implemented list typedef int ElementType; class List { public: public: List(); // constructor ~List(); // destructor List(const List & original); //copy constructor List & operator=(const List & original); // assignment overloading bool empty() const; void insert(ElementType item, unsigned pos); insert( unsigned pos); void remove(ElementType item); remove(ElementType void traverse(); private: class Node { public: ElementType data; Node * next; }; Node * first; int mySize; } 2/23/2011 20 LinkedLinked-List Needs “Big Three”, for example the copy constructor If If not, by default, it only gets the head pointer Shallow Copy Copy Copy Copy constructor will make a new linked list of nodes to which copy will point for deep copy 2/23/2011 21 7 Some operations: (1). The default constructor List::List() { mySize = 0; first = NULL; first } 0 2/23/2011 22 (2). Traverse the list to process each data // Traverse the list and process data in each node void List::traverse() { Node* ptr = first; while (ptr != NULL) { Process(ptr->data); r->d //Process is a function or a segment of codes. ptr = ptr->next; ptr} return; } 2/23/2011 23 (3). Insertion predptr item newptr // Insert a node at position pos (this is for pos !=0) void List::insert(ElementType item, unsigned pos ) List::insert(ElementT { if (pos > mySize ) return; mySize++; Node* newptr = new Node; newptrnewptr->data = item; Node* predptr = first; for(int i = 1; i < pos; i++) predptr = predptr ->next; // move predptr to the position newptrnewptr->next = predptr->next; predptrpredptrpredptr->next = newptr; } 2/23/2011 24 8 (4). Destructor List::~List() // delete all the nodes. { Node * tempP = first; Node * disposed; while(tempP != NULL) { disposed = tempP; tempP = tempP->next; tempPdelete disposed; } } 2/23/2011 25 (5). Deletion prev ptr Delete Delete node containing 22 from list. Suppose ptr points to the node to be deleted ptr prev prev points to its predecessor (the 17) To free space Do Do a bypass operation: Set Set the next pointer in the predecessor to point to the successor of the node to be deleted Deallocate Deallocate the node being deleted. 2/23/2011 26 Linked Linked Lists - Advantages Can Can access any item as long as link to the first item maintained Insert Insert new item without shifting Delete Delete existing item without shifting Can expand/contract as necessary It’s It’s also a fundamental data structure used to build other ADTs such as Stack, Queue,…… 2/23/2011 27 9 Linked Lists - Disadvantages Overhead Overhead of links: Need Need pointer to next node No No longer have direct access to each element of the list Many sorting algorithms need direct access Man Binary Binary search needs direct access Access Access of nth item now less efficient must must go through first element, and then second, and then third, etc. 2/23/2011 28 Linked List Variant: Doubly-Linked Lists (Ch11) Doublydoubly-linked list with dummy head node => bidirectional movement!! Each node has two pointers — one to its successor (null if there is none) and one to its precedessor (null if there is L prev last da t a first mySize 5 9 17 22 26 34 next Ease of use is a trade-off for the significant overhead of doublylinked lists 2/23/2011 29 Reference 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/23/2011 30 10 ...
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