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.

Slides:



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

Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
DATA STRUCTURES USING C++ Chapter 5
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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.
Data Structures Using C++ 2E
Linear Lists – Linked List Representation CSE, POSTECH.
Data Structures Using C++ 2E
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Forward 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.
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 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
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.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
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.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
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.
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.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
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.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Chapter 16: Linked Lists.
Reminder: Course Policy
C++ Programming:. Program Design Including
Review Deleting an Element from a Linked List Deletion involves:
Data Structure Dr. Mohamed Khafagy.
UNIT-3 LINKED LIST.
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.
The Class chain.
CS313D: Advanced Programming Language
Simulated Pointers.
بردارها و ماتريس ها ساختمان داده ها
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.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Linked Representation
A List Implementation That Links Data
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.
Chapter 17: Linked Lists.
Computer Science and Engineering
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
The Class chain.
The Class chain.
CSC 143 Java Linked Lists.
Data Structures & Algorithms
Stacks template<class T> class stack { public:
ITI Introduction to Computing II Lab-12
8 List ADTs List concepts. List applications.
Linked lists.
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.
A List Implementation That Links Data
Presentation transcript:

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 Bidirectional iterator Reverse iterator Quite often, an application needs to examine all elements of an instance. The elements of a linear list may be examined by doing examine(get(i)), 0 <= i < size(). This technique has much unnecessary overhead associated with it. For example, the index i is checked each time get() is invoked. What if the data structure we are dealing with doesn’t have a get(index) method? Iterators provide a uniform way to examine all elements of a data structure instance, and often with much smaller overhead than using other techniques.

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 != hasNext, next, and remove are the methods of Java’s interface Iterator.

Iterator Class Assume that a bidirectional iterator class iterator is defined within the class arrayList. Assume that methods begin() and end() are defined for arrayList. begin() returns an iterator positioned at element 0 of list. end() returns an iterator positioned one past last element of list.

Using An Iterator arrayList<int>::iterator xHere = x.begin(); arrayList<int>::iterator xEnd = x.end(); for (; xHere != xEnd; xHere++) examine( *xHere); vs for (int i = 0; i < x.size(); i++) examine(x.get(i));

Merits Of An Iterator it is often possible to implement the ++ and -- operators so that their complexity is less than that of get many data structures do not have a get by index method iterators provide a uniform way to sequence through the elements of a data structure

Linked Representation 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

Layout of L = (a,b,c,d,e) using an array representation. Memory Layout Layout of L = (a,b,c,d,e) using an array representation. a b c d e The figures show computer memory. An array uses a contiguous chunk of memory. A linked representation uses an arbitrary layout. c a e d b

Linked Representation c a e d b firstNode pointer (or link) in e is NULL use a variable firstNode to get to the first element a

Normal Way To Draw A Linked List b c d e NULL firstNode link or pointer field of node data field of node

Chain firstNode NULL a b c d e A chain is a linked list in which each node represents one element. There is a link or pointer from one element to the next. The last node has a NULL pointer.

Node Representation template <class T> struct chainNode { // data members T element; chainNode<T> *next;   // constructors come here }; next element

Constructors Of chainNode ? chainNode() {} ? element chainNode(const T& element) {this->element = element;} chainNode(const T& element, chainNode<T>* next) {this->element = element; this->next = next;} next element

get(0) a b c d e firstNode checkIndex(0); NULL firstNode checkIndex(0); desiredNode = firstNode; // gets you to first node return desiredNode->element; Do a checkIndex first. Then move to desired node. Once you are at the desired node, you can return the element in that node as in return desiredNode.element; When firstNode = null, there is no element whose index is 0.

get(1) a b c d e firstNode checkIndex(1); NULL firstNode checkIndex(1); desiredNode = firstNode->next; // gets you to second node return desiredNode->element;

get(2) a b c d e firstNode checkIndex(2); NULL firstNode checkIndex(2); desiredNode = firstNode->next->next; // gets you to third node return desiredNode->element;

get(5) a b c d e firstNode checkIndex(5); // throws exception NULL firstNode checkIndex(5); // throws exception desiredNode = firstNode->next->next->next->next->next; // desiredNode = NULL return desiredNode->element; // NULL.element Note that checkIndex would throw an exception. Other two statements executed only when checkIndex not done.

Erase An Element erase(0) deleteNode = firstNode; b c d e NULL firstNode erase(0) First do a checkIndex. deleteNode = firstNode; firstNode = firstNode->next; delete deleteNode;

first get to node just before node to be removed erase(2) a b d e NULL firstNode c b beforeNode c first get to node just before node to be removed beforeNode = firstNode->next;

save pointer to node that will be deleted erase(2) firstNode null a b c d e beforeNode save pointer to node that will be deleted deleteNode = beforeNode->next;

now change pointer in beforeNode erase(2) firstNode null a b c d e beforeNode now change pointer in beforeNode beforeNode->next = beforeNode->next->next; delete deleteNode;

insert(0,’f’) firstNode newNode a b c d e NULL firstNode f newNode Step 1: get a node, set its data and link fields First, do a checkIndex. newNode = new chainNode<char>(theElement, firstNode);

insert(0,’f’) firstNode newNode Step 2: update firstNode b c d e NULL firstNode f newNode Step 2: update firstNode firstNode = newNode;

firstNode = new chainNode<char>(‘f’, firstNode); One-Step insert(0,’f’) a b c d e NULL firstNode f newNode firstNode = new chainNode<char>(‘f’, firstNode);

first find node whose index is 2 insert(3,’f’) a b c d e NULL firstNode f newNode beforeNode c first find node whose index is 2 next create a node and set its data and link fields chainNode<char>* newNode = new chainNode<char>( ‘f’, beforeNode->next); finally link beforeNode to newNode beforeNode->next = newNode;

Two-Step insert(3,’f’) beforeNode = firstNode->next->next; a b c d e NULL firstNode f newNode beforeNode beforeNode = firstNode->next->next; beforeNode->next = new chainNode<char> (‘f’, beforeNode->next);

To-do Practice: Read more on iterators: Read more on linked lists: https://www.hackerrank.com/domains/cpp/introduction Read more on iterators: http://www.cplusplus.com/reference/iterator/ http://www.cplusplus.com/reference/iterator/iterator/ Read more on linked lists: https://en.wikipedia.org/wiki/Linked_list