2004-03L.Chen1 Chapter 3 DATA REPRESENTATION(2) 2004-03L.Chen2 3.4.4 A Chain Iterator Class 3.4.4 A Chain Iterator Class ( 遍历器类 )  An iterator permits.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linear Lists – Array Representation
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
M180: Data Structures & Algorithms in Java
List Representation - Chain Fall 2010 CSE, POSTECH.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Input iterator.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Chapter 4 1. Why “linked lists” 2 IndexWord A[0]BAT A[1]CAT A[2]EAT … A[n]WAT Insert “FAT” ? Or delete something.
Data Structures Using C++ 2E
Linear Lists – Linked List Representation CSE, POSTECH.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Forward iterator.
1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
L.Chen Chapter 3 DATA REPRESENTATION(3) L.Chen 3.8 APPLICATIONS Bin Sort Bin Sort (箱子排序)  It is a faster way to accomplish.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists.
SNU IDB Lab. Ch6. Linear Lists – Linked Representation © copyright 2006 SNU IDB Lab.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
第二章 线性表 第二节 线性链表 5. 线性表的链式表示 顺序表的优点是可以随机选取表中元素 缺点是插入删除操作复杂。 用指针将互不相连的内存结点串成的 线性表叫线性链表。 结点 node 由一个数据元素域,一个或几个 指针域组成。单链表的结点只有一个指针域。
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Session 03 - Templates Outline 03.1Introduction 03.2Function Templates 03.3Overloading Template Functions 03.4Class Templates 03.5Class Templates and Non-type.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
The Class arrayList General purpose implementation of linear lists. Unknown number of lists.
CSCE 210 Data Structures and Algorithms
Reminder: Course Policy
C++ Programming:. Program Design Including
CS505 Data Structures and Algorithms
UNIT-3 LINKED LIST.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
Linked List Yumei Huo Department of Computer Science
The Class chain.
Chapter 16-2 Linked Structures
Simulated Pointers.
Linked List (Part I) Data structure.
Simulated Pointers.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
The Class chain.
The Class chain.
Presentation transcript:

L.Chen1 Chapter 3 DATA REPRESENTATION(2)

L.Chen A Chain Iterator Class 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.Chen3 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 Θ ( n 2 ),  the Output() is Θ( n)  An iterator records the current position and advance one position right each time.

L.Chen4 Program 3.18 Chain iterator class template class ChainIterator{ public: T* Initialize (const Chain & 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 *location; };

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

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

L.Chen7 Comparing Difference: head pointer ; head node

L.Chen8 Circular List abcde firstNode

Program 3.20 Searching a circular linked list with head node template int CircularList : : Search(const T& x) const { // Locate x in a circular list with head node. ChainNode *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); }

L.Chen Comparison with Formula-Based Representation  formula-based representation: Continuous space; It is easy to search an element.  The procedures for insertion and deletion are more complex.  Time Complexity to access kth element is Θ(1)  Chain and circular list representation: The run time of the Insert and delete from a linear list will be smaller than the formula-based representation. Time Complexity to access kth element is O( k ).

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

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

L.Chen13 Doubly Linked List abcde null firstNode null lastNode

L.Chen14 Doubly Linked Circular List abcde firstNode

L.Chen15 Doubly Linked Circular List With Header Node abce headerNode d

L.Chen16 Empty Doubly Linked Circular List With Header Node headerNode

L.Chen17 Circular Doubly Linked List ( 双向循环链表 ) a) Nodeb) Null List c) Circular Doubly Linked List L D R

template class DoubleNode{ friend Double ; private: T data; DoubleNode *left, *right; }; template 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 & Delete (int k, T& x); Double & Insert (int k, const T& x); void Output (ostream& out) const ; private : DoubleNode *LeftEnd, *RightEnd; }; Program 3.21 Class definition for doubly linked lists

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

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

L.Chen Summary  Chain  Singly linked circular list  Head node  Doubly linked list  Circular doubly linked list

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

L.Chen23 template 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 & Delete(int k, T& x); IndirectList & 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 Operations template IndirectList :: indirectList (int MaxListSize) { MaxSize = MaxListSize; table = new T *[MaxSize]; length = 0; } template IndirectList :: ~indirectList( ) { for (int i = 0; i < length; i++) delete table[i]; delete [ ] table; } Program 3.23 Constructor and destructor for indirect addressing

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

L.Chen26 template IndirectList & IndirectList ::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.Chen27

L.Chen28 template IndirectList & IndirectList ::Insert(int k, const T& x) { if (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 Simulating Pointers

L.Chen30 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). caedb

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

How It All Looks  14 caedb caedb firstNode = 4 next element

L.Chen33 Still Drawn The Same abcde firstNode

L.Chen34 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. caedb firstNode

L.Chen35 Marking caedb firstNode caede Repeat for all reference variables. Start at firstNode and mark all nodes reachable from firstNode.

L.Chen36 Compaction  Move all marked nodes (i.e., nodes in use) to one end of memory, updating all pointers as necessary. cbedb firstNode aed Free Memory

L.Chen37 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 A Comparison

L.Chen39 Exercises Chapter3 Ex27; Ex31; Ex34; Ex37; Ex53