1 / 39

Chapter 3

Chapter 3. DATA REPRESENTATION(2). 3.4.4 A Chain Iterator Class ( 遍历器类). An iterator permits you to examine the elements of a data structure one at a time. Suppose for a moment that Output( ) is not a member function of Chain and that << is not overloaded for this class.

millerscott
Download Presentation

Chapter 3

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 3 DATA REPRESENTATION(2) L.Chen

  2. 3.4.4 A Chain Iterator Class(遍历器类) • An iterator permits you to examine the elements of a data structure one at a time. • Suppose for a moment that Output( ) is not a member function of Chain and that << is not overloaded for this class. L.Chen

  3. A Chain Iterator Class • int len = x.length( ); for ( int i=1; i<=len; i++) { x.find(i,x); Cout<< x<< ‘‘ ;} • the complexity of this code is Θ ( n2), • the Output() is Θ( n) • An iterator records the current position and advance one position right each time. L.Chen

  4. Program 3.18 Chain iterator class template<class T> class ChainIterator{ public: T* Initialize (const Chain<T>& c){ location = c.first; if (location) return &location->data; return 0; } T* Next ( ) { if (!location) return 0; location = location->link; if (location) return &location->data; return 0; } private: ChainNode<T> *location; }; L.Chen

  5. Program 3.19 Outputting the integer chain X using a chain iterator int *x; ChainIterator <int > c; x = c.Initialize(X); while (x) { cout << *x << ' '; x = c.Next();} cout << endl; L.Chen

  6. 3.4.5 Circular List Representation a chain is simplified and made to run faster by doing one or both of the following: • represent the linear list as a singly linked circular list (circular list) • add an additional node, called the head node at the front. L.Chen

  7. Comparing Difference: head pointer ; head node L.Chen

  8. firstNode a b c d e Circular List L.Chen

  9. Program 3.20 Searching a circular linked list with head node template <class T> int CircularList <T> : : Search(const T& x) const { // Locate x in a circular list with head node. ChainNode<T> *current = first->link; int index = 1; // index of current first->data = x; // put x in head node // search for x while (current->data != x) { current = current->link; index++; } // are we at head? return ((current == first) ? 0 : index); }

  10. 3.4.6 Comparison with Formula-Based Representation • formula-based representation:Continuous space; It iseasy to searchan element. • The procedures forinsertion and deletion are more complex. • Time Complexity to access kth element is Θ(1) • Chain and circular list representation:Therun timeof the Insert and delete from a linear list willbe smallerthan the formula-basedrepresentation. • Time Complexity to access kth element isO( k ). L.Chen

  11. 3.4.7 Doubly Linked List Representation • 为什么要引入双向链表? • 在单链表中,寻找下一个元素时间复杂度为O(1),寻找上一个元素时间复杂度为O(n)。为了克服单链表的缺点,引入双链表。 L.Chen

  12. 链表的具体结构 A doubly linked list is an ordered sequence of nodes in which each node has two pointers: left pointer (前趋) and right pointer(后继). • 优点:可以向前、向后访问,效率高。 • 缺点:需要更多的空间,结构更复杂。 L.Chen

  13. firstNode lastNode null null a b c d e Doubly Linked List L.Chen

  14. firstNode a b c d e Doubly Linked Circular List L.Chen

  15. headerNode a b c d e Doubly Linked Circular List With Header Node L.Chen

  16. Empty Doubly Linked Circular List With Header Node headerNode L.Chen

  17. Circular Doubly Linked List (双向循环链表) L D R a) Node b) Null List c) Circular Doubly Linked List L.Chen

  18. template<class T> class DoubleNode{ friend Double<T>; private: T data; DoubleNode<T> *left, *right; }; template<class T> class Double{ public: Double() { LeftEnd=RightEnd=0; }; ~Double(); int Length()const; bool Find (int k, T& x) const ; int Search (const T& x) const ; Double<T>& Delete (int k, T& x); Double<T>& Insert (int k, const T& x); void Output (ostream& out) const ; private : DoubleNode<T> *LeftEnd, *RightEnd; }; Program 3.21 Class definition for doubly linked lists

  19. Delete (k) 删除第k 个元素 = k (1) (2) e = p->data; p->prior->next = p->next; p->next->prior = p->prior; L.Chen

  20. insert (k, x) 在第k个元素之前插入x = k (2) (4) (3) (1) S s->data =x; s->prior =p->prior; p->prior->next = s; s->prior =p; p->prior = s; L.Chen

  21. 3.4.8 Summary • Chain • Singly linked circular list • Head node • Doubly linked list • Circular doubly linked list L.Chen

  22. 3.5 Indirect Addressing 3.5.1 Representation • 间接寻址的定义、引入的目的。 • 如何实现间接寻址? L.Chen

  23. template<class T> class IndirectList{ public: IndirectList(int MaxListSize=10); ~IndirectList( ); bool IsEmpty( )const {return length==0;} int Length( )const {return length;} bool Find(int k,T& x) const ; int Search(const T& x) const ; IndirectList<T>& Delete(int k, T& x); IndirectList<T>& Insert(int k, const T& x); void Output(ostream& out) const ; private:T **table; // 1D array of T pointers int length, MaxSize; }; Program 3.22 Class definition for an indirectly addressed list L.Chen

  24. 3.5.2 Operations template <class T> IndirectList<T>:: indirectList (int MaxListSize) { MaxSize = MaxListSize; table = new T *[MaxSize]; length = 0; } template <class T> IndirectList<T>:: ~indirectList( ) { for (int i = 0; i < length; i++) delete table[i]; delete [ ] table; } Program 3.23 Constructor and destructor for indirect addressing L.Chen

  25. template <class T> bool IndirectList<T>::Find(int k, T& x) const { if (k < 1 || k > length) return false; x = *table[k - 1]; return true; } Program 3.24 Find operation for indirect lists L.Chen

  26. template <class T> IndirectList<T>& IndirectList<T>::Delete(int k,T& x) { if (Find(k, x)) { for (int i = k; i < length; i++) table[i-1] = table[i]; length--; return *this ; } else throw OutOfBounds( ); } Program 3.25 Deletion from an indirect list L.Chen

  27. L.Chen

  28. template <class T> IndirectList<T>& IndirectList<T>::Insert(int k, const T& x) { if (k < 0 || k > length) throw OutOfBounds(); if (length == MaxSize) throw NoMem( ); for (int i = length-1; i >= k; i--) table[i+1] = table[i]; table[k] = new T; *table[k] = x; length++; return *this ; } Program 3.26 Insertion into an indirectly addressed list L.Chen

  29. 3.6 Simulating Pointers L.Chen

  30. c a e d b Simulated-Pointer Memory Layout • Data structure memory is an array, and each array position has an element field (type Object) and a next field (type int). L.Chen

  31. Node Representation package dataStructures; class SimulatedNode { // package visible data members Object element; int next; // constructors not shown } L.Chen

  32. How It All Looks c a e d b next • 14 11 14 -1 8 0 c a e d b element 0 1 2 3 4 5 8 11 14 firstNode = 4

  33. Still Drawn The Same firstNode -1 a b c d e L.Chen

  34. c a e d b firstNode Marking • Unmark all nodes (set all mark bits to false). • Start at each program variable that contains a reference, follow all pointers, mark nodes that are reached. L.Chen

  35. Start at firstNode and mark all nodes reachable from firstNode. Marking c c a a e e d d b e firstNode Repeat for all reference variables. L.Chen

  36. Compaction Free Memory c a e d b e d b firstNode • Move all marked nodes (i.e., nodes in use) to one end of memory, updating all pointers as necessary. L.Chen

  37. Simulated Pointers • Can allocate a chain of nodes without having to relink. • Can free a chain of nodes in O(1) time when first and last nodes of chain are known. L.Chen

  38. 3.7 A Comparison L.Chen

  39. Exercises Chapter3 Ex27; Ex31; Ex34; Ex37; Ex53 L.Chen

More Related