1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Queue Overview Queue ADT Basic operations of queue
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
More on the STL vector list stack queue priority_queue.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Object Oriented Data Structures
Chapter 05 (Part V) Control Statements: Part II. Nested For-Structures Consider the following codes: for (int i=0; i
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
STL !!!generic programming!!! Anar Manafov
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
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.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
CS342 Data Structures End-of-semester Review S2002.
A Generic List Class and Linked Lists Andy Wang Data Structures, Algorithms, and Generic Programming.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Queue Queue – First In / First Out (FIFO) data structure that supports two operations: push for adding new element at the end of the queue pop for removing.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
1 Chapter 1 C++ Templates Sections 1.6 and Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
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.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
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.
Object-Oriented Programming (OOP) Lecture No. 42.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
CS505 Data Structures and Algorithms
Pointers and Linked Lists
Pointers and Linked Lists
Data Structure #1: The Array
Lecture 7-2 : STL Iterators
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 16-2 Linked Structures
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Lecture 7-2 : STL Iterators
ADT Implementations: Templates and Standard Containers
Pointers and Linked Lists
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Level 1 STL – Linear DS fb.com/acmicpcfcicu. Static Arrays Creating 1D, 2D, ND Static Arrays. Accessing Time. Traversing a static array with N dimensions.
Lists - I The List ADT.
Lists - I The List ADT.
Standard Template Library (STL)
Lecture 8-2 : STL Iterators and Algorithms
Collections Intro What is the STL? Templates, collections, & iterators
Chapter 3 Lists, Stacks, and Queues
Data Structures & Programming
Presentation transcript:

1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms

2 Iterators: Motivation Need a way to navigate through the items in a container An example: navigating over vector v. for (int i = 0; i != v.size(); i++ ) cout << v[i] << endl; However, doubly-linked list would need a different form We want a general approach to navigate elements for different implementations of an ADT

3 Iterators A generalized type that helps in navigating a container –A way to initialize at the front and back of a list –A way to move to the next or previous position –A way to detect the end of an iteration –A way to retrieve the current value Implemented as nested types of containers in STL Examples: –Iterator type for vector defined as vector ::iterator itr; –Iterator type for list defined as list ::iterator itr;

4 Getting an Iterator Two methods in all STL containers –iterator begin ( ) Returns an iterator to the first item in the container –iterator end ( ) Returns an iterator representing end marker in the container (that is, the position after the last item) Example for (int i = 0; i != v.size(); i++ ) cout << v[i] << endl; can be written using iterators as for(vector ::iterator itr=v.begin(); itr!=v.end(); itr++) cout << itr++ << endl;

5 Iterator Methods Iterators have methods Many methods use operator overloading –itr++ and ++itr advance the iterator to next location Consider int A[8], int *i=A+2 –cout << *++i prints value of A[3] –cout << *i++ prints A[2], but i points to A[3] after this operation is complete –*itr returns reference to object stored at iterator itr’s location – itr1 == itr2 is true if itr1 and itr2 refer to the same location, else false – itr1 != itr2 is true if itr1 and itr2 refer to different locations, else it is false

6 Implementing Iterators Example: Lec11/DLL.h –An iterator class is associated with each container type –Its main data is a pointer –The container typedefs the iterator class and also the type of value that it stores The latter is helpful in implementing generic algorithms –Note the difference between pre-increment and post-increment operators DLLIterator &operator++()DLLIterator operator++(int) NodePtr = old value NodePtr = new value Returned NodePtr = old value NodePtr = new value Returned NodePtr = old Temporary

7 Generic Algorithms Give you flexibility in the container type and data type used –Enabled by common naming schemes for operations and typedefs of iterator and value_type –Example: Lec11/FindMax.cpp