Lists - I The List ADT.

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
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: An internal look
The List ADT Textbook Sections
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
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.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
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
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
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.
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. Basics A vector is like an array, but more flexible A vector provides (constant time) random access to its elements A vector may be dynamically.
Chapter 2 Objects and Classes
Motivation for Generic Programming in C++
CS212: Object Oriented Analysis and Design
Pointers and Dynamic Arrays
Cpt S 122 – Data Structures Abstract Data Types
CS505 Data Structures and Algorithms
The List ADT Reading: Textbook Sections 3.1 – 3.5
The List ADT Sections 3.2, 3.3, 3.5 Introduction
Vectors Holds a set of elements, like an array
CSCE 210 Data Structures and Algorithms
CS Computer Science IB: Object Oriented Programming
Lists The List ADT.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Starting Out with C++ Early Objects Eighth Edition
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Friday, February 16, 2018 Announcements… For Today… 4.5-, pgs. 252
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
ADT Implementations: Templates and Standard Containers
Object Oriented Programming COP3330 / CGS5409
Associative Structures
The List ADT Reading: Textbook Sections 3.1 – 3.5
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 9 One-Dimensional Arrays
CMSC 341 Binary Search Trees.
Doubly Linked List Implementation
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Standard Template Library (STL)
CMSC 341 Binary Search Trees.
Lab4 problems More about templates Some STL
Lists - I The List ADT.
CMSC 341 List 2.
STL List.
Lesson 25 Miscellaneous Topics
Lists - I The List ADT.
Lists CMSC 202, Version 4/02.
Collections Intro What is the STL? Templates, collections, & iterators
Lists CMSC 202, Version 4/02.
Doubly Linked List Implementation
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
STL List.
A dictionary lookup mechanism
Presentation transcript:

Lists - I The List ADT

List ADT A list is a dynamic ordered tuple of homogeneous elements Ao, A1, A2, …, AN-1 where Ai is the i-th element of the list Definition: The position of element Ai is i; positions range from 0 to N-1 inclusive Definition: The size of a list is N ( a list with no elements is called an “empty list”) dynamic means the list can change over time homogeneous means the elements are all of the same type ordered means that A1+1 follows (or succeeds) Ai and Ai-1 precedes Ai 2/16/2006

Operations on a List create an empty list destroy a list construct a (deep) copy of a list find(x) – returns the position of the first occurrence of x remove(x) – removes x from the list if present insert(x, position) – inserts x into the list at the specified position isEmpty( ) – returns true if the list has no elements makeEmpty( ) – removes all elements from the list findKth(int k) – returns the element in the specified position The implementations of these operations in a class may have different names than the generic names above 9/13/2006

STL vector The STL vector class template provides an array implementation of the List ADT The vector also support operations that are not specific to List 2/16/2006

List operations using a vector vector supports constant time insertion at the “end” of the list using void push_back( const Object& x); vector supports constant time deletion from the “end” of the list using void pop_back( ); vector supports constant time access to the element at the “end” of the list using const Object& back( ) const; vector supports constant time access to the element at the “front” of the list usin const Object& front( ) const; vector also supports these List operations bool empty( ) const; -- returns true if the vector is empty void clear( ) – removes all elements of the vector Default constructor Copy constructor Assignment operator 2/16/2006

iterators Some List operations (notably those that remove and insert from the middle of the list) require the notion of position. All STL containers use iterators and const_iterators to represent position within the container. Doing so provides a uniform interface for all STL container templates. For example, a position within a vector<int> is represented by the type vector<int>::iterator Questions How does an application get an iterator? What can iterators do? What methods of vector require an iterator? 9/13/2006

Creating an iterator The STL vector (and all STL containers) define the following methods for creating iterators iterator begin( ) – returns an iterator representing the first element of the vector iterator end( ) – returns an iterator representing the end marker of the vector (i.e. the position after the last element of the vector). 2/16/2006

Using iterators We can now loop using iterators to access and print all elements of the vector of ints, v vector<int>::iterator itr; for (itr = v.begin( ); itr != v.end( ); itr.xxx()) { cout << itr.zzz() << endl; // element at itr’s position } itr.xxx() must move the iterator to the next element itr.zzz() must retrieve the element at itr’s position 9/13/2006

Iterator Operations Iterators are often considered an abstraction of a pointer and so support pointer semantics ++itr and itr++ advance the iterator to the next element in the container *itr returns the element stored at itr’s position itr1 == itr2 returns true if both iterators refer to the same position itr1 != itr2 returns true if the iterators refer to different positions So, how do we complete the loop on the previous slide? 9/13/2006

List remove and insert Now armed with iterators, we can insert and remove elements from the middle of a vector iterator insert(iterator pos, const Object & x) inserts x into the vector prior to the position specified by the iterator iterator erase (iterator position) removes the object at the position specified by the iterator iterator erase (iterator start, iterator end) removes all objects beginning at position “start”, up to (but NOT including) position “end” 2/16/2006

const_iterator Since *itr represents the element in a container, the statement *itr = x; can be used to change the contents of the container. This should not be allowed for const containers (typically passed as parameters), therefore containers also support const_iterators. A const_iterator behaves the same as an iterator except the * operator returns a const reference to the element in the container and hence cannot be used to change the element. Think of *iterator as a mutator and *const_iterator as an accessor. Also note that the iterator class is derived from const_iterator via inheritance. Therefore an iterator can be used anywhere a const_iterator can be used but not vice-versa. However, good programming practice dictates that const_iterators be used with const containers. 2/16/2006

const_iterator (cont’d) The compiler forces you to use const_iterator for const containers by supplying two versions of begin( ) and end( ) iterator begin( ); const_iterator begin( ) const; iterator end( ); const_iterator end( ) const; The “constness” of the methods is part of their signature. 2/16/2006

Scanning a container template <typename Container> void PrintCollection( const Container & c, ostream & out ) { if( c.empty( ) ) out << "(empty)"; else // note the required keyword “typename” typename Container::const_iterator itr = c.begin( ); out << "[ " << *itr++; // Print first item while( itr != c.end( ) ) out << ", " << *itr++; out << " ]" << endl; } 2/16/2006