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 17 Linked Lists.
Advertisements

Linear Lists – Linked List Representation
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.
©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Data Structure Data structure uses collection of related variables that can be accessed.
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.
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)
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
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.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
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,
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract 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.
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.
Chapter 16: Linked Lists.
Reminder: Course Policy
C++ Programming:. Program Design Including
Lectures linked lists Chapter 6 of textbook
Data structures and algorithms
Review Deleting an Element from a Linked List Deletion involves:
Data Structure and Algorithms
Data Structure Dr. Mohamed Khafagy.
List ADT & Linked Lists.
UNIT-3 LINKED LIST.
Linked lists.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
CSCI 3333 Data Structures 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.
Chapter 20: Binary Trees.
The Class chain.
Chapter 21: Binary Trees.
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.
بردارها و ماتريس ها ساختمان داده ها
Simulated Pointers.
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.
Linked Representation
A List Implementation That Links Data
Linked List.
Linked List and Selection Sort
Chapter 17: Linked Lists.
The Class chain.
The Class chain.
CSC 143 Java Linked Lists.
Data Structures & Algorithms
Data Structures & Algorithms
Data Structures Chapter 4: Linked Lists.
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.

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