1 / 81

Chapter 4

Chapter 4. Review of Sequential Representations. Previously introduced data structures, including array, queue, and stack, they all have the property that successive nodes of data object were stored a fixed distance apart.

vemerick
Télécharger la présentation

Chapter 4

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4

  2. Review of Sequential Representations • Previously introduced data structures, including array, queue, and stack, they all have the property that successive nodes of data object were stored a fixed distance apart. • The drawback of sequential mapping for ordered lists is that operations such as insertion and deletion become expensive. • Also sequential representation tends to have less space efficiency when handling multiple various sizes of ordered lists.

  3. Linked List • A better solutions to resolve the issues of sequential representations is linked lists. • Elements in a linked list are not stored in sequential in memory. Instead, they are stored all over the memory. They form a list by recording the address of next element for each element in the list. Therefore, the list is linked together. • A linked list has a head pointer that points to the first element of the list. • By following the links, you can traverse the linked list and visit each element in the list one by one.

  4. Ordered List for Three-letter words

  5. Linked List Insertion • To insert an element (GAT) into the three letter linked list: • Get a node that is currently unused; let its address be x. • Set the data field of this node to GAT. • Set the link field of x to point to the node after FAT, which contains HAT. • Set the link field of the node cotaining FAT to x.

  6. Designing a List in C++ • Design Attempt 1: Use a global variable first which is a pointer of ThreeLetterNode. • Unable to access to private data members: data and link. • Design Attempt 2: Make member functions public. • Defeat the purpose of data encapsulation. • Design Attempt 3: Use of two classes. Create a class that represents the linked list. The class contains the items of another objects of another class.

  7. Class ThreeLetterNode { Private: char data[3]; ThreeLetterNode *link; }; ThreeLetterNode *first; Design 1

  8. Design 3

  9. Program 4.1 Composite Classes

  10. Nested Classes • The Three Letter List problem can also use nested classes to represent its structure. class ThreeLetterList { public: // List Manipulation operations . . private: class ThreeLetterNode { // nested class public: char data[3]; ThreeLetterNode *link; }; ThreeLetterNode *first; };

  11. Pointer Manipulation in C++ x a y b • Addition of integers to pointer variable is permitted in C++ but sometimes it has no logical meaning. • Two pointer variables of the same type can be compared. • x == y, x != y, x == 0 x a b x y b b y x = y *x = * y

  12. Composite Classes for List and ListNode Class List; // forward declaration Class ListNode { Friend class List; Private: int data ; ListNode *link; }; Class List { Public: // List manipulation operations Private: ListNode *first: };

  13. Define A Linked List Template • A linked list is a container class, so its implementation is a good template candidate. • Member functions of a linked list should be general that can be applied to all types of objects. • When some operations are missing in the original linked list definition, users should not be forced to add these into the original class design. • Users should be shielded from the detailed implementation of a linked list while be able to traverse the linked list. Solution => Use of ListIterator

  14. List Iterator • A list iterator is an object that is used to traverse all the elements of a container class. • ListIterator<Type> is delcared as a friendof both List<Type> and ListNode<Type>. • A ListIterator<Type> object is initialized with the name of a List<Type> object l with which it will be associated. • The ListIterator<Type> object contains a private data member current of type ListNode<Type> *. At all times, current points to a node of list l. • The ListIterator<Type> object defines public member functions NotNull(), NextNotNull(), First(), and Next() to perform various tests on and to retrieve elements of l.

  15. Template of Linked Lists Enum Boolean { FALSE, TRUE}; template <class Type> class List; template <class Type> class ListIterator; template <class Type> class ListNode { friend class List<Type>; friend class ListIterator <Type>; private: Type data; ListNode *link; }; Template <class Type> class List { friend class ListIterator <Type>; public: List() {first = 0;}; // List manipulation operations . . private: ListNode <Type> *first; };

  16. Template of Linked Lists (Cont.) template <class Type> class ListIterator { public: ListIterator(const List<Type> &l): list(l), current(l.first) {}; Boolean NotNull(); Boolean NextNotNull(); Type * First(); Type * Next(); Private: const List<Type>& list; // refers to an existing list ListNode<Type>* current; // points to a node in list };

  17. Attaching A Node To The End Of A List Assume there is a private ListNode<Type>* last in class List<Type> Template <class Type> Void List<Type>::Attach(Type k) { ListNode<Type>*newnode = new ListNode<Type>(k); if (first == 0) first = last =newnode; else { last->link = newnode; last = newnode; } };

  18. Inverting a List

  19. Concatenating Two Chains Template <class Type> void List<Type>:: Concatenate(List<Type> b) // this = (a1, …, am) and b = (b1, …, bn) m, n ≥ , // produces the new chain z = (a1, …, am, b1, bn) in this. { if (!first) { first = b.first; return;} if (b.first) { for (ListNode<Type> *p = first; p->link; p = p->link); // no body p->link = b.first; } }

  20. When Not To Reuse A Class • If efficiency becomes a problem when reuse one class to implement another class. • If the operations required by the application are complex and specialized, and therefore not offered by the class.

  21. Circular Lists • By having the link of the last node points to the first node, we have a circular list. • Need to make sure when current is pointing to the last node by checking for current->link == first. • Insertion and deletion must make sure that the circular structure is not broken, especially the link between last node and first node.

  22. Examples of Circular Lists

  23. Diagram of A Circular List first last

  24. Linked Stacks and Queues top front rear 0 Linked Queue 0 Linked Stack

  25. Revisit Polynomials 1 a.first 14 0 8 3 0 2 6 10 b.first 14 0 8 10 -3

  26. Polynomial Class Definition struct Term // all members of Terms are public by default { int coef; // coefficient int exp; // exponent void Init(int c, int e) {coef = c; exp = e;}; }; class Polynomial { friend Polynomial operator+(const Polynomial&, const Polynomial&); private: List<Term> poly; };

  27. Operating On Polynomials • With linked lists, it is much easier to perform operations on polynomials such as adding and deleting. • E.g., adding two polynomials a and b a.first 1 0 14 0 8 3 2 p 6 10 14 0 8 b.first 10 -3 q (i) p->exp == q->exp c.first 0 14 11

  28. Operating On Polynomials a.first 1 0 14 0 8 3 2 p 6 10 14 0 8 b.first 10 -3 q c.first 0 14 0 11 10 -3 (ii) p->exp < q->exp

  29. Operating On Polynomials 0 14 11 a.first 1 0 14 0 8 3 2 p 6 10 14 0 8 b.first 10 -3 q c.first 10 -3 0 8 2 (iii) p->exp > q->exp

  30. Memory Leak • When polynomials are created for computation and then later on out of the program scope, all the memory occupied by these polynomials is supposed to return to system. But that is not the case. Since ListNode<Term> objects are not physically contained in List<Term> objects, the memory they occupy is lost to the program and is not returned to the system. This is called memory leak. • Memory leak will eventually occupy all system memory and causes system to crash. • To handle the memory leak problem, a destructor is needed to properly recycle the memory and return it back to the system.

  31. List Destructor Template <class Type> List<Type>::~List() // Free all nodes in the chain { ListNode<Type>* next; for (; first; first = next) { next = first->link; delete first; } }

  32. Free Pool • When items are created and deleted constantly, it is more efficient to have a circular list to contain all available items. • When an item is needed, the free pool is checked to see if there is any item available. If yes, then an item is retrieved and assigned for use. • If the list is empty, then either we stop allocating new items or use new to create more items for use.

More Related