Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Generic List Class and Linked Lists Andy Wang Data Structures, Algorithms, and Generic Programming.

Similar presentations


Presentation on theme: "A Generic List Class and Linked Lists Andy Wang Data Structures, Algorithms, and Generic Programming."— Presentation transcript:

1 A Generic List Class and Linked Lists Andy Wang Data Structures, Algorithms, and Generic Programming

2 Lists in Everyday Life Shopping list To-do list Dave Letterman’s top 10 list My cat’s revenge list Shopping vector? What if you want to insert an item? What if you want to insert an item?

3 List Wish List Insert an element Remove an element Remove all items Assignment operator Comparison operators Constructors/destructors Generic class Convenient way to iterate through the list

4 Iterator Motivating example char A[10] = { ‘a’, ‘b’, ‘c’, …’j’ }; char *j; for (j = A; j != A + 10; j++) { cout << *j << endl; }

5 Iterator A generalized model A way to initialize at the front and back of a list A way to initialize at the front and back of a list A way to move to the next or previous position A way to move to the next or previous position A way to detect the end of an iteration A way to detect the end of an iteration A way to retrieve the current value A way to retrieve the current value A way to compare two iterators at the same location A way to compare two iterators at the same location

6 List Iterator Wish List Return the first item Return the last item Return the current item Increment to the next item Decrement to previous item Detect the end of an iteration Compare iterators for equality Generic class

7 List Public Interface Read-only accessor functions unsigned int Size() const; int Empty() const;

8 List Public Interface (2) Locating places on the list Iterator Begin() const; Iterator End() const; Accessing values on the list T& Front() const; T& Back() const;

9 List Public Interface (3) Write functions int PushFront(const T& t); int PushBack(const T& t); int PopFront(); int PopBack(); int Insert(Iterator& I, const T& t); int Remove(Iterator& I); unsigned int Remove(const T& t); void Clear();

10 List Public Interface (4) Constructors and destructor TList();~TList(); TList & operator=(const TList & L); TList(const TList & L); Terminology support typedef T value_type; typedef TListIterator Iterator;

11 List Public Interface (5) Non-member functions int operator==(const TList & L1, const TList & L2); int operator!=(const TList & L1, const TList & L2); ostream operator & L);

12 List Complexity Requirements O(1) Runtime complexity Default Constructor Default Constructor PushFront(t), PushBack(t), Insert(I, t) PushFront(t), PushBack(t), Insert(I, t) PopFront(), PopBack(), Remove(I, t) PopFront(), PopBack(), Remove(I, t) Begin(), End() ; Begin(), End() ; Front(), Back() ; Front(), Back() ; Empty() ; Empty() ; Not available for vectors PushFront(t) for a vector Increment size Copy v[j] to v[j + 1] Assign v[0] = t  (size) (at least size)

13 List Complexity Requirements (2) O(Size()) Runtime complexity Copy constructor Copy constructor Assignment operator Assignment operator Destructor Destructor Size() Size() Clear() Clear() Remove(t) Remove(t) The maintenance cost of the size operation is  (size) (at least size) Incremental computation On-demand computation

14 List Iterator Public Interface Read-only operators int operator==(const iterator& I2) const; int operator!=(const iterator& I2) const; T& operator*() const; // return a reference to current value int Valid() const; // Iterator is pointing to a valid element Write operators iterator& operator=(const iterator& I); iterator& operator++(); // prefix iterator operator++(int); // postfix iterator& operator--(); // prefix iterator operator--(int); // postfix O(1) requirement for space and time

15 Using List TList KittyVengenceList; KittyVengenceList.PushFront(“toe biting”); “toe biting” “toe biting” KittyVengenceList.PushBack(“carpet littering”); “toe biting”, “carpet littering” “toe biting”, “carpet littering” KittyVengenceList.PushFront(“midnight howling”); “midnight howling”, “toe biting”, “carpet littering” “midnight howling”, “toe biting”, “carpet littering” KittyVengenceList.PushBack(“toilet drinking”); “midnight howling”, “toe biting”, “carpet littering”, “toilet drinking” “midnight howling”, “toe biting”, “carpet littering”, “toilet drinking” TList ::Iterator I; for (I = KittyVengenceList.Begin(); I != KittyVengenceList.End(); ++I) { // print list with << }

16 List Insertion Insert “furniture scratching” after “toe biting” // sequential search for (I = KittyVengenceList.Begin(); I.Valid(); ++I) { if (“toe biting” == *I) { break;}} KittyVengenceList.Insert(I, “furniture scratching”); “midnight howling”, “toe biting”, “furniture scratching”, “carpet littering”, “toilet drinking” “midnight howling”, “toe biting”, “furniture scratching”, “carpet littering”, “toilet drinking” // what happens if “toe biting” is not on the list?

17 Remove all copies of t from List TList ::Iterator I; for (I = KittyVengenceList.Begin(); I.Valid();) { if (t == *I) { KittyVengenceList.Remove(I); } else { ++I;}}

18 List and List Iterator Conceptual relationship Iterator L1 begincurrentend List: A, B, C, D, E, F begincurrentend Iterator L2

19 List Implementation Plan ListElement Pointers to the previous and next element Pointers to the previous and next element Value Value Defined within the List class, with limited scope class ListElement { // data ListElement *prev; ListElement *next; T value; // constructor ListElement(const T& t) : value(t); ListElement(const T& t) : value(t);}; value prev next value prev next value prev next No need for contiguous memory allocation

20 List Implementation Plan (2) Protected data Pointers to the first and the last element of the list Pointers to the first and the last element of the list Number of list elements Number of list elements template template class List { protected: ListElement* first; ListElement* last; unsigned int size; public:…};

21 Three Perspectives of List Client view List interface List interface ListIterator interface ListIterator interface Implementation view Sequentially ordered list elements Sequentially ordered list elements System view List elements connected by pointers List elements connected by pointers

22 List Iterator Implementation Plan Protected data Pointer to the current list element Pointer to the current list element template template class ListIterator { protected: ListElement* curr; public: ListIterator& operator++(); …};

23 Iterator operator++() Set curr to the next list element template template ListIterator<T>& ListIterator ::operator++() { curr = (*curr).next; // curr->next return *this; } value prev next value prev next value prev next curr ListIterator ListElement (*curr).next

24 Common Implementation Pitfalls Forget to update first, last, and size Forget to handle special cases Empty list Empty list List with one element List with one element List with more than one elements List with more than one elements

25 Debugging Tips Best debugging tools YOUR BRAIN YOUR BRAIN Careful analysis and design Careful analysis and design Writing code for readability and self- documentation Writing code for readability and self- documentation Best times to debug DESIGN TIME DESIGN TIME Coding time Coding time

26 Defining class TList Defining class TList template template class TList { friend class TListIterator ; public: typedef t value_type; typedef TListIterator Iterator; // constructors and destructor TList();~TList(); TList(const TList & L); TList & operator=(const TList & L); // Read-only accessor functions unsigned int Size() const; int Empty() const; T& Front() const; T& Back() const; Iterator Begin() const; Iterator End() const; void Display(ostream os, char ofc = ‘\0’) const;

27 Defining class TList (2) // Write routines int PushFront(const T& t); int PushBack(const T& t); int PopFront(); int PopBack(); int Insert(Iterator& I, const T& t); int Remove(Iterator& I); unsigned int Remove(const T& t); void Clear();

28 Defining class TList (3) protected: class TListElement { friend class TList ; friend class TListIterator ; // data T value; TListElement *prev; TListElement *next; TListElement(const T& Tval); } ListElement *first; ListElement *last; unsigned int size; void Clone(const TList & L); };

29 Defining class TList (4) // global scope operators and functions template template int operator==(const TList & L1, const TList & L2); template template int operator!=(const TList & L1, const TList & L2); tempalte tempalte ostream& operator & L);

30 Defining class TListIterator Defining class TListIterator Template Template Class TListIterator { friend class TList ; public: typedef T value_type; // constructors TListIterator(); TListIterator(const TList & L); tListIterator(const TListIterator& I); void Initialize(const TList & L); void rInitialize(const TList & L); // read-only routines T& Retrieve() const; // return reference to curr int Valid() const;

31 Defining class TListIterator (2) // read-only operators int operator==(const TListIterator& I2) const; int operator!=(const TListIterator& I2) const; T& operator*() const; // same as retrieve // write operators TListIterator & operator=(const TListIterator & I); TListIterator & operator++(); // prefix TListIterator operator++(int); // postfix TListIterator & operator--(); // prefix TListIterator operator--(int); // postfix protected: TList ::TListElement *curr; };

32 Implementing class TList Implementing class TList Helper functions template template void TList ::Clear() { // deletes all list elements in the TList // and set data members to 0 } template template void TList ::Clone(const TList & L) { // makes *this a clone of L // first copy the static data and initialize the pointers // then the dynamic data in the non-empty case }

33 Implementing class TList (2) Constructors template template TList ::TListElement::TListElement(const T& t) : value(t), prev(0), next(0) { } template template TList ::TList() : first(0), last(0), size(0) { } template template TList ::TList(const TList & L) { Clone(L);}

34 Implementing class TList (3) Destructor template template TList ::~TList() { Clear();}

35 Implementing class TList (4) Read-only functions template TListIterator TList ::Begin() const { Iterator I; I.curr = first; return I; } template void TList ::Display(ostream &os, char ofc) const { TList ::Iterator I; if (ofc == ‘\0’) { for (I = Begin(); I.Valid(); ++I) { os << *I; } } else { for (I = Begin(); I.Valid(); ++I) { os << *I << ofc; }}}

36 Implementing class TList (5) Read-only functions template template ostream& operator & L2) { L2.Display(os); return os; }

37 Implementing class TList (6) Read-only functions template template int operator==(const TList & L1, const TList & L2) { if (L1.Size() != L2.Size()) { return 0; } for (TList ::Iterator I1(L1), I2(L2); I1.Valid() && I2.Valid; I1++, I2++) { if (*I1 != *I2) { return 0; }} return 1; }

38 Implementing class TList (7) Write functions template template int operator=(const TList & L) { if (this != &L) { Clear();Clone(L);} return *this; }

39 Implementing class TList (8) Insert template template int TList ::Insert(TListIterator & I, const T& t) { // call the Push methods when they apply // Insert at back if iterator is not valid // insert at front if iterator is at front // iterator is valid and not at front // 1. create a new element // 2. link new element into the list // 3. adjust size (if present) // 4. leave I at new entry and return } template template TListIterator TList ::Insert(const T& t) { TListIterator I = End(); Insert(I, t); return I; }

40 Implementing class TList (9) Remove template int TList ::Remove(TListIterator & I) { // call the Pop methods when they apply // make sure of consistent interaction with ends of list // first deal with the invalid iterator case // for a valid iterator and non-void list // deal with cases where I.curr == first or last // at this point, we are removing a link that’s neither first nor last // 1. remember list element to be removed, and advance iterator // 2. unlink old list element from the list // 3. delete old list element, adjust size and return } template int TList ::Remove(const T& t) { // use iterator I and call Remove(I) // be sure to take into account the similarity of Remove(I) and ++I }

41 Implementing class TListIterator Implementing class TListIterator Helper function template template TListIterator ::Initialize(const TList & L) { curr = L.first; } template template TListIterator ::rInitialize(const TList & L) { curr = L.last; }

42 Implementing class TListIterator (2) Constructors template template TListIterator ::TListIterator() : curr(0) { } template template TListIterator ::TListIterator(const TList & L) { Initialize(L);} template template TListIterator ::TListIterator(const TListIterator & I) : curr(I.curr) { }

43 Implementing class TListIterator (3) Read-only functions template template int TListIterator ::Valid() const { return curr != 0; } template template T& TListIterator ::Retrieve() const { if (curr == 0) { std:cerr << “TListIterator: invalid dereference” << endl; exit(EXIT_FAILURE);} return curr->value; }

44 Implementing class TListIterator (4) Operator functions template template T& TListIterator ::operator*() const { return curr->value; } template template int TListIterator ::operator==(const TListIterator & I2) const { if (curr == I2.curr) { return 1; } return 0; }


Download ppt "A Generic List Class and Linked Lists Andy Wang Data Structures, Algorithms, and Generic Programming."

Similar presentations


Ads by Google