Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.

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.
Beginning C++ Through Game Programming, Second Edition
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 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.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of.
C++ for Engineers and Scientists Third Edition
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.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
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
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
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.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Array Size Macro approach 19 Macros Macros do not perform error checking. // macro approach #define macro_array_size(array) (sizeof(array)/sizeof(array[0]))
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.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
Data Abstraction: The Walls
CS212: Object Oriented Analysis and Design
Bill Tucker Austin Community College COSC 1315
Vectors Holds a set of elements, like an array
CSCE 210 Data Structures and Algorithms
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
Associative Structures
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
priority_queue<T>
Chapter 9 One-Dimensional Arrays
Recursive Linked List Operations
Engineering Problem Solving with C++, Etter
Lists - I The List ADT.
Lists - I The List ADT.
CS150 Introduction to Computer Science 1
Jordi Cortadella Department of Computer Science
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 3 Lists, Stacks, and Queues
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
The List Container and Iterators
Presentation transcript:

Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer Science California State University, Fresno Fall 2006

Introduction to Data Structure, Fall 2006 Slide- 2 California State University, Fresno Vector STL - Constructors vector(); Create an empty vector. This is the default constructor. vector(int n, const T& value = T()); Create a vector 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(). vector(T *first, T *last); Initialize the vector using the address range [first, last).

Introduction to Data Structure, Fall 2006 Slide- 3 California State University, Fresno Vector STL - Access T& back(); Return the value of the item at the rear of the vector. Precondition: The vector must contain at least one element. const T& back() const; Constant version of back(). bool empty() const; Return true if the vector is empty and false otherwise.

Introduction to Data Structure, Fall 2006 Slide- 4 California State University, Fresno Vector STL - Access T& operator[] (int i); Allow the vector element at index i to be retrieved or modified. Precondition: The index, i, must be in the range 0  i < n, where n is the number of elements in the vector. Postcondition: If the operator appears on the left side of an assignment statement, the expression on the right side modifies the element referenced by the index. const T& operator[] (int i) const; Constant version of the index operator.

Introduction to Data Structure, Fall 2006 Slide- 5 California State University, Fresno Vector STL – Insertion/Removal void push_back(const T& value); Add a value at the rear of the vector. Postcondition: The vector has a new element at the rear and its size increases by 1. void pop_back(); Remove the item at the rear of the vector. Precondition: The vector is not empty. Postcondition: The vector has a new element at the rear or is empty. How to insert/remove at the middle?

Introduction to Data Structure, Fall 2006 Slide- 6 California State University, Fresno Vector STL – Growing/Shrinking void resize((int n, const T& fill = T()); Modify the size of the vector. If the size is increased, the value fill is added to the elements on the tail of the vector. If the size is decreased, the original values at the front are retained. Postcondition: The vector has size n. int size() const; Return the number of elements in the vector

Introduction to Data Structure, Fall 2006 Slide- 7 California State University, Fresno Example – Printing a vector template void writeVector(const vector & v) { // capture the size of the vector in n int i, n = v.size(); for(i = 0; i < n; i++) cout << v[i] << " "; cout << endl; }

Introduction to Data Structure, Fall 2006 Slide- 8 California State University, Fresno Using Vector - Declaration #include using namespace std; // vector of size 5 containing the integer value 0 vector intVector(5); // vector of size 10; each element contains the empty string vector strVector(10);

Introduction to Data Structure, Fall 2006 Slide- 9 California State University, Fresno Using Vector – Insertion/Removal Write code to remove {8} from a vector {12,-5,8,14,10}

Introduction to Data Structure, Fall 2006 Slide- 10 California State University, Fresno Using Vector – Resizing 7493 vector v(5); v.resize(10); v.resize(4); 3

Introduction to Data Structure, Fall 2006 Slide- 11 California State University, Fresno Application – Insertion Sort Insert Dare at location 1; the tail of the list shifts to the right Start withMonroe Chin Insert Chin in location 0; Monroe moves tolocation 1 FloresChin InsertFlores in location 1; Monroe moves to location 2 ChinMonroeSteinFlores Element Stein is OK ChinDareMonroe Stein Flores ProcessingFlores Processing Chin Processing Dare Processing Stein Monroe

Introduction to Data Structure, Fall 2006 Slide- 12 California State University, Fresno Insertion Sort - Algorithm for each element i in vector v shifting all larger elements before i one position right; inserting element i to the right position;

Introduction to Data Structure, Fall 2006 Slide- 13 California State University, Fresno Insertion Sort - Algorithm for each element i in vector v store v[i] to temp; for all previous element j from 1 to i if v[j] > temp shift element j to element j+1 record the last shifting element to k inserting element i at position k;

Introduction to Data Structure, Fall 2006 Slide- 14 California State University, Fresno Insertion Sort - Code insertionSort(): template void insertionSort(vector & v) { int i, j, n = v.size(); T temp; for (i = 1; i < n; i++) { j = i; temp = v[i]; while (j > 0 && temp < v[j-1]) { v[j] = v[j-1]; j--; } v[j] = temp; }

Introduction to Data Structure, Fall 2006 Slide- 15 California State University, Fresno Example – Tracing the program a e d c b j=1, temp=e skip j=2, temp=d since d < v[1], v[1] = d, j=1 (a,e,e,c,b) j=1, temp=d skip (a,d,e,c,b) j=3, temp=c since c<v[2], v[2]=c, j=2 (a,d,e,e,b) j=2, temp=c since c<v[1], v[1]=c,j=1 (a,d,d,e,b) j=1, temp=c skip (a,c,d,e,b) Next?