SlideServe
Menu
Préférences
© SlideServe · v2026

The List ADT Sections 3.2, 3.3, 3.5

bwagner avatar
27 diapositives · 310 Views · Jan 04, 2020

Discover how to manage shopping lists, to-do tasks, and more using generic list classes with insert, remove, and clear functions.

The List ADT Sections 3.2, 3.3, 3.5 - slide 1
01 / 27The List ADT Sections 3.2, 3.3, 3.5
1 / 17

Télécharger la présentation

The List ADT Sections 3.2, 3.3, 3.5
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. The List ADTSections 3.2, 3.3, 3.5

  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?

  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. Abstract view of List and Iterator ListElement next next next next null null prev prev prev prev front back I1 Iterator

  5. List Public Interface (contd) • Locating places on the list iterator begin(); const iterator begin() const; iterator end(); const iterator end() const; • Accessing values on the list Object & front(); Object & back(); (and their const versions)

  6. List Public Interface (contd.) • Write functions int push_front(const Object & x); int push_back(const Object & x); int pop_front(); int pop_back(); int insert(iterator & itr, const Object & x); iterator erase( iterator itr); iterator erase( iterator start, iterator end ); void clear();

  7. List Public Interface (contd.) • Constructors and destructor List(); List(const List & rhs); ~List();

  8. List Complexity Requirements • O(1) Runtime complexity • Default Constructor • push_front(t), push_back(t), insert(I, t) • pop_front(), pop_back(), erase(I) • begin(), end(); • front(), back(); • empty();

  9. List Complexity Requirements (2) • O(N) Runtime complexity • Copy Constructor • Destructor • clear() • erase(SI,EI)

  10. List Iterator Public Interface • Read-only operators int operator== (const iterator & rhs) const; int operator!= (const iterator & rhs) const; Object& operator* ( ) const; // return a reference to // current value • Write operators iterator& operator++ ( ); // prefix iterator operator++ ( int ); // postfix iterator& operator-- ( ); // prefix iterator operator-- ( int ); // postfix • O(1) requirement for space and time

  11. Using List List<String> KittyVengeance; KittyVengeance.push_front(“toe biting”); • “toe biting” KittyVengeance.push_back(“carpet littering”); • “toe biting”, “carpet littering” KittyVengeance.push_front(“midnight howling”); • “midnight howling”, “toe biting”, “carpet littering” KittyVengeance.push_back(“couch tearing”); • “midnight howling”, “toe biting”, “carpet littering”, “couch tearing” List<String>::iterator I; for (I = KittyVengeance.begin(); I != KittyVengence.end(); I++) { // print list with << }

  12. List Insertion • Insert “furniture scratching” before “toe biting” // sequential search for (I = KittyVengeance.begin(); I != KittyVengence.end(); I++) { if (“toe biting” == *I) { break; } } // insert the new string KittyVengeance.insert(I, “furniture scratching”); • “midnight howling”, “furniture scratching”, “toe biting”, “carpet littering”, “couch tearing” // what happens if “toe biting” is not on the list?

  13. Remove all copies of item from List List<String>::iterator I = KittyVengeance.begin(); while( I != KittyVengeance.end()) { if (“couch tearing” == *I) { KittyVengeance.erase(I); } else { I++; } }

  14. List and List Iterator • Conceptual relationship Iterator I1 begin current end List: A, B, C, D, E, F begin current end Iterator I2

  15. data prev next data prev next data prev next No need for contiguous memory allocation Nodes in a list • Node • Pointers to the previous and next element • Data Value • Defined within the List class, with limited scope

  16. List Implementation A Doubly Linked List With Header and Tail Nodes as Markers An Empty List

  17. Outline of List Class (Part 1)

  18. Outline of List Class (Part 2)

  19. Outline of List Class (Part 3)

  20. Outline of List Class (Part 4)

  21. const_iterator for List Prefix Postfix

  22. const_iterator class for List (contd.)

  23. iterator class for List Why do we override the parent’s ++ implementation?

  24. iterator class for List (contd.)

  25. List Initialization Routines

  26. List Insert Routine

  27. List Erase Routine