1 / 42

Review of Sequential Representations

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.

kylia
Download Presentation

Review of Sequential Representations

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. 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.

  2. Linked List • A better solutions to resolve the aforementioned 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.

  3. Linked List Insertion • To insert an element 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.

  4. Linked List Insertion And Deletion first CAT BAT EAT FAT HAT GAT first CAT BAT EAT FAT GAT HAT

  5. 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.

  6. Program 4.1 Composite Classes class ThreeLetterList; // forward delcarion class ThreeLetterNode { friend class ThreeLetterList; private: char data[3]; ThreeLetterNode * link; }; class ThreeLetterList { public: // List Manipulation operations . . private: ThreeLetterNode *first; };

  7. 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; };

  8. 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

  9. Program 4.11 Attaching A Node To The End Of A List 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; } };

  10. Program 4.13 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; } }

  11. 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.

  12. 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.

  13. Diagram of A Circular List first last

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

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

  16. Program 4.20 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; };

  17. 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

  18. 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

  19. 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

  20. 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.

  21. 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; } }

  22. 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.

  23. Using Circular Lists For Polynomials • By using circular lists for polynomials and free pool mechanism, the deleting of a polynomial can be done in a fixed amount of time independent of the number of terms in the polynomial.

  24. Deleting A Polynomial with a Circular List Structure av 3 a.first 1 0 14 8 3 2 1 second 2 av

  25. Doubly Linked Lists • The problem of a singly linked list is that supposed we want to find the node precedes a node ptr, we have to start from the beginning of the list and search until find the node whose link field contains ptr. • To efficiently delete a node, we need to know its preceding node. Therefore, doubly linked list is useful. • A node in a doubly linked list has at least three fields: left link field (llink), a data field (item), and a right link field (rlink).

  26. Doubly Linked List • A head node is also used in a doubly linked list to allow us to implement our operations more easily. rlink item llink Head Node rlink item llink Empty List

  27. Deletion From A Doubly Linked Circular List rlink item llink Head Node

  28. Insertion Into An Empty Doubly Linked Circular List node node newnode

  29. PolyNode Class in C++ enum Triple{ var, ptr, no }; class PolyNode { PolyNode *link; int exp; Triple trio; union { char vble; PolyNode *dlink; int coef; }; };

  30. PolyNode in C++ (Cont.) • trio == var: the node is a head node. • vble indicates the name of the variable. Or it is an integer point to the variable in a variable table. • exp is set to 0. • trio == ptr: coefficient itself is a list and is pointed by the field dlink. exp is the exponent of the variable on which the list is based on. • trio == no, coefficient is an integer and is stored in coef. exp is the exponent of the variable on which the list is based on.

  31. Representing 3x2y trio vble exp link trio vble exp link var y 0 ptr 1 0 var x 0 no 3 2 0 P

  32. Representation of P(x, y, z) P(x, y, z) v z 0 p 2 p 1 0 v y 0 p 3 p 2 0 v y 0 p 4 p 1 0 v x 0 n 2 0 0 v x 0 n 3 8 0 v x 0 n 1 10 n 2 8 0 v x 0 n 1 4 n 6 3 0

  33. Recursive Algorithms For Lists • A recursive algorithm consists of two components: • The recursive function (the workhorse); declared as a private function • A second function that invokes the recursive function at the top level (the driver); declared as a public function.

  34. Program 4.6 Copying A List // Driver void GenList::Copy(const GenList& l) { first = Copy(l.first); } // Workhorse GenListNode* GenList::Copy(GenListNode *p) // Copy the nonrecursive list with no shared sublists pointed at by p { GenListNode *q = 0; if (p) { q = new GenListNode; q->tag = p->tag; if (!p->tag) q->data = p->data; else q->dlink = Copy(p->dlink); q->link = Copy(p->link); } return q; }

  35. Linked Representation for A r b t t 0 t u v s f a f b 0 t f e 0 w x f c f d 0

  36. Generalized List Representation Example D = 0 Empty list A=(a, (b, c)) A f a t 0 f b t c 0 B t t 0 0 B=(A, A, ()) C f a t 0 C=(a, C)

  37. Recursiveness GenList::Copy

  38. Important List Functions • List Equality (Program 4.37) • List Depth (Program 4.38) • An empty list has depth 0.

  39. Reference Counts, Shared and Recursive Lists • Lists may be shared by other lists for the purpose of space saving. • Lists that are shared by other lists create problems when performing add or delete functions. For example, let’s look at the previous A, B, C, D example. When deleting the front node of list A would requires List B to update its pointers. • The use of the data field of a head node to record the reference count can resolve the aforementioned problem. The list can not be deleted unless the reference count is 0.

  40. Example of Reference Counts, Shared and Recursive Lists X f 1 0 A=(a, (b, c)) Y f 3 f a t 0 f 1 f b f c 0 Z f 1 t t t 0 B=(A, A, ()) W f 2 f a t 0 f 1 0 C=(a, C)

  41. Erasing A List Recursively // Driver GenList::~GenList() // Each head node has a reference count. We assume first ≠ 0. { Delete(first); first = 0; } // Workhorse void GenList::Delete(GenListNode* x) { x->ref--; // decrement reference coutn of head node. if (!x->ref) { GenListNode *y = x; // y traverses top-level of x. while (y->link) { y= y->link; if (y->tag == 1) Delete (y->dlink);} y->link = av; // Attach top-level nodes to av list av = x; } }

  42. Issue In Erasing Recursive Lists • When erasing a recursive list (either direct recursive or indirect recursive), the reference count does not become 0. Then the nodes are not returned to available list. This will cause memory leak issue.

More Related