Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Forward iterator.

Slides:



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

Linked Lists Geletaw S..
Linked Lists.
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
CSE Lecture 12 – Linked Lists …
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
List Representation - Chain Fall 2010 CSE, POSTECH.
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.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Input iterator.
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Linked Lists Dr. Youssef Harrath
Data Structures Using C++ 2E
Linear Lists – Linked List Representation CSE, POSTECH.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
L.Chen1 Chapter 3 DATA REPRESENTATION(2) L.Chen A Chain Iterator Class A Chain Iterator Class ( 遍历器类 )  An iterator permits.
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)
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Stacks Chapter 8. Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array.
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.
ITERATORS. Iterator An iterator in C++ is a concept that refines the iterator design pattern into a specific set of behaviors that work well with the.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
The List ADT Reading: Sections 3.2, 3.3, 3.5.
1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
SNU IDB Lab. Ch6. Linear Lists – Linked Representation © copyright 2006 SNU IDB Lab.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
slides adapted from Marty Stepp and Hélène Martin
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Chapter 16: Linked Lists.
Reminder: Course Policy
C++ Programming:. Program Design Including
Design of a HashTable and its Iterators
Design of a HashTable and its Iterators
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
LinkedList Class.
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.
Linked List Yumei Huo Department of Computer Science
The Class chain.
Queue, Deque, and Priority Queue Implementations
Chapter 18: Linked Lists.
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.
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.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Linked Representation
Doubly Linked List Implementation
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.
Templates and Iterators
The Class chain.
The Class chain.
Doubly Linked List Implementation
Linked Lists Templates and Iterators
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
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.
Presentation transcript:

Iterators & Chain Variants

Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Forward iterator Bidirectional iterator Reverse iterator

Forward Iterator Allows only forward movement through the elements of a data structure. abcde NULL first

Iterator Class  Assume that a forward iterator class ChainIterator is defined within the class Chain.  Assume that methods Begin() and End() are defined for Chain. Begin() returns an iterator positioned at element 0 (i.e., leftmost node) of list. End() returns an iterator positioned one past last element of list (i.e., NULL or 0).

Using An Iterator Chain ::iterator xHere = x.Begin(); Chain ::iterator xEnd = x.End(); for (; xHere != xEnd; xHere++) examine( *xHere); vs for (int i = 0; i < x.Size(); i++) examine(x.Get(i)); See next slide

Review Get(5) desiredNode = first  link  link  link  link  link; // desiredNode = NULL return desiredNode  data; // NULL.element abcde NULL First

In Class Exercise Analyze the time complexity for traversal a list with size n for (; xHere != xEnd; xHere++) examine( *xHere); for (int i = 0; i < x.Size(); i++) examine(x.Get(i));

Required Methods for Forward Iterator  iterator(T* thePosition) Constructs an iterator positioned at specified element (Like Begin())  dereferencing operators * and ->  Post and pre increment operators ++  Equality testing operators == and !=

A Forward Iterator For Chain See Program 191 class ChainIterator { public: // some typedefs omitted // constructor comes here // dereferencing operators * & ->, pre and post // increment, and equality testing operators private: ChainNode *current; };

Constructor ChainIterator(ChainNode * startNode = 0) {current = startNode;}

Dereferencing Operators T& operator*() const {return current->data;} T* operator->() const {return &current->data;}

Increment ChainIterator& operator++() // preincrement {current = current->link; return *this;} ChainIterator& operator++(int) // postincrement { ChainIterator old = *this; current = current->link; return old; }

Equality Testing bool operator!=(const ChainIterator right) const {return current != right.current;} bool operator==(const ChainIterator right) const {return current == right.current;}

Chain With Header Node and Insertion abcde NULL headerNode f newNode

Empty Chain With Header Node headerNode NULL

Compare with the Linked list without Header Node (Two different Cases) abcde NULL first f newNode abcde NULL first f newNode beforeNode c

Insertion (See Lec. 11) template void Chain ::Insert(int theIndex, const T& theElement) { if (theIndex < 0) throw “Bad insert index”; if (theIndex == 0) // insert at front first = new chainNode (theElement, first); else { // find predecessor of new element ChainNode * p = first; for (int i = 0; i < theIndex - 1; i++) {if (p == 0) throw “Bad insert index”; p = p->next;} // insert after p p->link = new ChainNode (theElement, p->link); } Can be removed.

Circular List abcde firstNode

Doubly Linked List abcde NULL firstNode NULL lastNode

Doubly Linked Circular List abcde firstNode

Doubly Linked Circular List With Header Node abce headerNode d

Empty Doubly Linked Circular List With Header Node headerNode

Bidirectional Iterator Allows both forward and backward movement through the elements of a data structure.

Bidirectional Iterator Methods  iterator(T* thePosition) Constructs an iterator positioned at specified element  dereferencing operators * and ->  Post and pre increment and decrement operators ++ and ––  Equality testing operators == and !=

Homework Sec Exercise