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.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Advertisements

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.
Linked Lists CENG 213 Data Structures.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
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.
More on the STL vector list stack queue priority_queue.
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 Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
Templates and the STL.
Data Structures Using C++ 2E
Containers Overview and Class Vector
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Generic Positional Containers and Double-Ended Queues.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 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.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Data Structures Using C++
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.
Lecture 7 : Intro. to STL (Standard Template Library)
A gentle introduction to the standard template library (STL) Some references:
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 5 – September 4 th, 2001.
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.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
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.
1 Generic Positional Containers and Double-Ended Queues.
Data Structure and Algorithm Analysis 03: Lists, Stacks and Queues Hongfei Yan School of EECS, Peking University.
Object-Oriented Programming (OOP) Lecture No. 41
Cpt S 122 – Data Structures Abstract Data Types
CS505 Data Structures and Algorithms
Vectors Holds a set of elements, like an array
CSCE 210 Data Structures and Algorithms
Chapter 16-2 Linked Structures
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
priority_queue<T>
Chapter 9 One-Dimensional Arrays
Doubly Linked List Implementation
Lists - I The List ADT.
Lists - I The List ADT.
Doubly Linked List Implementation
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
Presentation transcript:

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

2 Abstract Data Type (ADT) High-level definition of data types An ADT specifies –A collection of data –A set of operations on the data or subsets of the data ADT does not specify how the operations should be implemented Examples –list, stack, queue, deque, priority queue, table (map), associative array, set, graph, digraph How are they different? –Class –Class template –ADT

3 List ADT Objects/data –A 0, A 1, A 2, … A N-1 –Size of the List is N Operations –Up to the designer of a List, for example, –printList() –makeEmpty() –Find() –Insert() –Remove() –findKth() –etc

4 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

5 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;

6 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;

7 Iterator Methods Iterators have methods Many methods use operator overloading –itr++ and ++itr  advance the iterator to next location –*itr  return reference to object stored at iterator itr’s location – itr1 == itr2  true if itr1 and itr2 refer to the same location, else false – itr1 != itr2  true if itr1 and itr2 refer to different locations, else false

8 Container Operations Requiring Iterators Adding element –iterator insert(iterator pos, const object &x) –Add x in list before interator pos –Returns iterator representing position of inserted item Removing element –iterator erase(iterator pos) –Remove element at position pos –Returns iterator representing position of item following pos Removing elements in a range –iterator erase(iterator start, iterator end) –Remove elements from start to end (not including end )

9 Iterator example Removing every other elements in a list

10 The Vector Implementation of List ADT Extends the notion of array by storing a sequence of arbitrary objects –Informally, we call it Vector ADT Elements of vector ADT can be accessed by specifying their index

11 vector in C++ STL Collection  Elements of some proper type T Operations –int size ( )  returns the number of elements in the vector –void clear ( )  removes all elements from the vector –bool empty ( )  returns true if the vector has no elements –void push_back ( const Object &x ) adds x to the end of the vector –void pop_back ( ) Removes the object at the end of the vector –Object & back ( ) Returns the object at the end of the vector –Object & front ( ) Returns the object at the front of the vector

12 vector in C++ STL (contd.) More Operations –Object & operator[] ( int index ) Returns the object at location index (without bounds checking) Both accessor and mutator versions –int capacity ( ) Returns the internal capacity of the vector Number of elements the container can hold without further memory allocation –void resize ( int newSize, const Object& val = Object() ) Change the size of the vector Newly created elements will be initialized to val

13 Implementing the vector Class Implementing Vector as first-class type –Can be copied –Memory it uses is automatically reclaimed Vector maintains –A primitive C++ array –The array capacity –The current number of items stored in the vector Operations –Copy constructor –operator= –Destructor to reclaim primitive array –All the other operators we saw earlier

14 vector Implementation template class vector { public: // lots of member functions typedef T * iterator; private: int theSize; int theCapacity T *Array };

15 vector Member Functions - 1 vector(int initialSize=1) : theSize(initialSize),theCapacity(initialSize+1) {Array = new T[theCapacity];} ~vector() {delete [] Array;} void pop_back() {theSize--;} O(1) iterator begin() {return Array;} iterator end() {return Array + theSize;} T &operator[](int index) {return Array[index];} O(1)

16 vector Member Functions - 2 vector &operator=(vector &rhs) { if(this != &rhs) // Prevents self-copy { delete Array; theSize = rhs.size(); theCapacity = rhs.theCapacity; Array = new T[ capacity() ]; for(int k=0; k<size(); k++) Array[k] = rhs.Array[k]; }

17 vector Member Functions - 3 void reserve(int N) { if(N < theSize) return; T *old = Array; Array = new T[N]; for(int k=0;k<theSize;k++) Array[k] = old[k]; theCapacity = N; delete [] old; } O(N) void push_back(T &x) { if(theSize==theCapacity) reserve(2*theSize); // Assumes theSize > 0 Array[theSize++] = x; } Array old

18 Amortized Analysis of push_back Consider the time for N push_backs –Time for copying the data = N*O(1) = O(N) –Let 2 k <= N < 2 k+1 –Time for call to reserve = O( k+1 ) = O(2 k+2 -1) = O(2N) = O(N) –So, the total amortized time per push_back is O(1)