The List Container and Iterators

Slides:



Advertisements
Similar presentations
Lists Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
Advertisements

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.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Lists: An internal look
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
More on the STL vector list stack queue priority_queue.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents NCurses (curses.h) Background Curses “Hello World” Initializing & Shutting down Moving cursor I/O (printw, scanw.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
Data Structures Using C++ 2E
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Containers Overview and Class Vector
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
A Generic List Class and Linked Lists Andy Wang Data Structures, Algorithms, and Generic Programming.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
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.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
CS212: Object Oriented Analysis and Design
Chapter 16: Linked Lists.
Cpt S 122 – Data Structures Abstract Data Types
Standard Template Library
The List ADT Sections 3.2, 3.3, 3.5 Introduction
Data Abstraction: The Walls
Standard Template Library (STL)
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Array Lists Chapter 6 Section 6.1 to 6.3
Object Oriented Programming COP3330 / CGS5409
Associative Structures
Chapter 18: Linked Lists.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
priority_queue<T>
Doubly Linked List Implementation
Recursive Linked List Operations
Data Abstraction: The Walls
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
CSCE 121 – Spring 2016 Professor J. Michael Moore
STL List.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Doubly Linked List Implementation
Chapter 3 Lists, Stacks, and Queues
Chapter 9 Linked Lists.
STL List.
Presentation transcript:

The List Container and Iterators Chapter 6 The List Container and Iterators

Outline Sample list The list ADT CLASS list Constructors CLASS list Operations CLASS list::iterator Operations Inserting an element into a list Removing an element from a list Ordered lists Splicing two lists

§- list - A Sequence of elements stored by position. - Index access is not available… §- to access the value of an element, must pass through its preceding elements. 3

Shifting blocks of elements to insert or delete a vector item

Model of a list object with links to next and previous element

The List ADT The list API documents the member function prototype as well as pre- and postconditions. provides three constructors to declare a list object.

Create an empty list. This is the default constructor. CLASS list Constructors <list> list(); Create an empty list. This is the default constructor. list(int n, const T&value = T()); Create a list with n elements, each having a specified value. If the value argument is omitted, the elements are filled with the default value for type T. Type T must have a default constructor, and the default value of type T is specified by the notation T(). list(T *first, T *last); Initialize the list, using the address range [first, last).

Return the value of the item at the rear of the list. CLASS list Operations <list> T& back(); Return the value of the item at the rear of the list. Precondition: The list must contain at least one element. bool empty() const; Return true if the list is empty, false otherwise. T& front(); Return the value of the item at the front of the list.

void push_back(const T& value); Add a value at the rear of the list. CLASS list Operations <list> void push_back(const T& value); Add a value at the rear of the list. Postcondition: The list has a new element at the rear, and its size increases by 1. void pop_back(); Remove the item at the rear of the list. Precondition: The list is not empty. Postcondition: The list has a new element at the rear or is empty.

void push_front(const T& value); Add a value at the front of the list. CLASS list Operations <list> void push_front(const T& value); Add a value at the front of the list. Postcondition: The list has a new element at the front, and its size increases by 1. void pop_front(); Remove the item at the front of the list. Precondition: The list is not empty. Postcondition: The list has a new element at the front or is empty. int size() const; Return the number of elements in the list.

const_iterator begin(); CLASS list Operations <list> iterator begin(); Returns an iterator that references the first position (front) of the list. If the list is empty, the iterator value end() is returned. const_iterator begin(); Returns a const_iterator that points to the first position (front) of a constant list. If the list is empty, the const_iterator value end() is returned. iterator end(); Returns an iterator that signifies a location immediately out of the range of actual elements. A program must not dereference the value of end() with the * operator.

const_iterator end(); CLASS list Operations <list> iterator end(); Returns an iterator that signifies a location immediately out of the range of actual elements. A program must not dereference the value of end() with the * operator. const_iterator end(); Returns a const_iterator that signifies a location immediately out of the range of actual elements in a constant list. A program must not dereference the value of end() with the * operator.

void erase(iterator pos); Erase the element pointed to by pos. CLASS list Operations <list> void erase(iterator pos); Erase the element pointed to by pos. Precondition: The list is not empty. Postcondition: The list has one fewer element. void erase(iterator first, iterator last); Erase all list elements within the iterator range [first, last]. Postcondition: The size of the list decreases by the number of elements in the range.

iterator insert(iterator pos, const T& value); CLASS list Operations <list> iterator insert(iterator pos, const T& value); Insert value before pos, and return an iterator pointing to the position of the new value in the list. The operation does not affect any existing iterators. Postcondition: The list has a new element.

§- iterator - A generalized pointer that moves through a container(eg. vector, list) element by element… forward or backward - At any point, the * operator accesses the value of a container item. Example: PP1010 15

--: Moves the iterator to the previous item in the list. CLASS list::iterator Operations <list> *: Accesses the value of the item currently pointed to by the iterator. *iter; ++: Moves the iterator to the next item in the list. iter++; --: Moves the iterator to the previous item in the list. iter--; ==: Takes two iterators as operands and returns true when they both point at the same item in the list. iter1 == iter2 !=: Returns true when the two iterators do not point at the same item in the list. iter1 != iter2

Inserting an element into a list Insertion function returns an iterator pointing at the new element The original iterator continues to point at the same element

Removing an element from a list Erase function make the iterator invalid and the iterator must be reinitialized.

§- The list class has two iterator types: A generalized list traversal pointer. 2) const _ iterator: must be used with a constant list object. Each type is a nested class of list and must be accessed by using the scope operator :: For example: list<int>::iterator iter; 19

§- the list member function begin() §- the list member function end() - Gives an iterator an initial value that points to the first element. §- the list member function end() - Returns an iterator pointing just past the last element of the list. 20

§- list class member functions insert() and erase() - Both use an iterator argument to modify a list. insert(): places value in the list before the data referenced by the iterator pos. erase(): removes the data item referenced by pos from the list. 21

§- The sequential search of a list object - implemented by using an iterator range [first, last). - It returns an iterator that points at the target value or has value last if the target is not in the list. Example: PP 1057: program 17-6 22