Collection Classes Eric Roberts CS 106B January 12, 2015 (Part 1: Vectors, Grids, Stacks, and Queues)

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
1 Abstract Data Types. Objectives To appreciate the concept and purpose of abstract data types, or ADTs To understand both the abstract behavior and the.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
TCSS 342, Winter 2005 Lecture Notes
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Vectors and Grids Eric Roberts CS 106B April 8, 2009.
Collection Classes Eric Roberts CS 106B January 14, 2013 (Part 1: Vectors, Grids, Stacks, and Queues)
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Implementing Queues Eric Roberts CS 106B February 13, 2013.
Click to edit Master text styles Stacks Data Structure.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Building Java Programs
Cpt S 122 – Data Structures Abstract Data Types
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
Pointers and Linked Lists
COSC160: Data Structures: Lists and Queues
Stacks and Queues.
Chapter 14 Templates C++ How to Program, 8/e
Queues Queues Queues.
Vectors and Grids Chris Piech CS 106B Lecture 2 Jan 9, 2015.
Cinda Heeren / Geoffrey Tien
CSE 373: Data Structures and Algorithms
Programming Fundamentals Lecture #7 Functions
Stack and Queue APURBO DATTA.
Stacks and Queues.
Building Java Programs
Stack Data Structure, Reverse Polish Notation, Homework 7
Stacks and Queues.
Building Java Programs
structures and their relationships." - Linus Torvalds
Pointers and Linked Lists
Chapter 19: Stacks and Queues.
Stacks and Queues.
Stacks.
Pointers and Linked Lists
Building Java Programs
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
Abstract Data Type Abstract Data Type as a design tool
Stacks.
slides created by Marty Stepp
Visit for more Learning Resources
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Generics, Stack, Queue Based on slides by Alyssa Harding
Stacks.
structures and their relationships." - Linus Torvalds
Stacks and Queues.
A type is a collection of values
Stack Implementations
Introduction to Classes and Objects
Presentation transcript:

Collection Classes Eric Roberts CS 106B January 12, 2015 (Part 1: Vectors, Grids, Stacks, and Queues)

Computer Forum Career Fair Computer Forum Career Fair (For CS and EE Students) Wed, January 14, 11am – 4pm Computer Forum Career Fair Wednesday, January 14 Time: 11am - 4pm Lawn between the Mudd Chemistry and the Gates CS Buildings The Computer Forum Career Fairs enable Stanford Engineering students, specifically CS and EE, to get a head start on careers and internships with Forum member companies. When: What: Date: Time: Location: Description:

Outline Introduce the idea of collection classes1. Use vectors to read an entire file3. Introduce the Grid class4. Introduce the Stack class5. Use stacks to balance parentheses6. Introduce the Queue class7. Introduce the Vector class2.

The Collection Classes For the rest of this week, we will be learning about the classes in Chapter 5. These classes contain other objects and are called container or collection classes. Here are some general guidelines for using these classes: –These classes represent abstract data types whose details are hidden. –Each class (except Lexicon ) requires type parameters. –Declaring variables of these types always invokes a constructor. –Any memory for these objects is freed when its declaration scope ends. –Assigning one value to another copies the entire structure. –To avoid copying, these structures are usually passed by reference. VectorGridStackQueueMapSetLexicon

ADTs as Software Tools Over the relatively short history of software development, one of the clear trends is the increasing power of the tools available to you as a programmer. One of the best explanations of the importance of tools is the book Software Tools by Brian Kernighan and P. J. Plauger. Even though it was published in 1976, its value and relevance have not diminished over time. The primary theme of the book is that the best way to extend your reach in programming is to build on the tools of others.

Template Classes The collection classes are implemented as template classes, which make it possible for an entire family of classes to share the same code. Instead of using the class name alone, the collection classes require a type parameter that specifies the element type. For example, Vector represents a vector of integers. Similarly, Grid represents a two-dimensional array of characters. It is possible to nest classes, so that, for example, you could use the following definition to represent a list of chess positions: Vector > chessPositions;

Constructors for the Vector Class Vector vec; Initializes an empty vector of the specified element type. Vector vec(n); Initializes a vector with n elements all set to the default value of the type. Vector vec(n, value); Initializes a vector with n elements all set to value.

Methods in the Vector Class vec.size() Returns the number of elements in the vector. vec.isEmpty() Returns true if the vector is empty. vec.get(i) or vec[i] Returns the i th element of the vector. vec.insert(index, value) Inserts the value before the specified index position. vec.remove(index) Removes the element at the specified index. vec.clear() Removes all elements from the vector. vec.set(i, value) or vec[i] = value; Sets the i th element of the vector to value. vec.add(value) or vec += value; Adds a new element to the end of the vector.

The readEntireFile Function /* * Function: readEntireFile * Usage: readEntireFile(is, lines); * * Reads the entire contents of the specified input stream * into the string vector lines. The client is responsible * for opening and closing the stream */ void readEntireFile(istream & is, Vector & lines) { lines.clear(); string line; while (getline(is, line)) { lines.add(line); }

Methods in the Grid Class Grid grid(nrows, ncols); Constructs a grid with the specified dimensions. grid.numRows() Returns the number of rows in the grid. grid.numCols() Returns the number of columns in the grid. inBounds(row, col) Returns true if the specified row and column position is within the grid. grid[i][j] Selects the element in the i th row and j th column. resize(nrows, ncols) Changes the dimensions of the grid and clears any previous contents.

The Stack Metaphor A stack is a data structure in which the elements are accessible only in a last-in/first-out order. The fundamental operations on a stack are push, which adds a new value to the top of the stack, and pop, which removes and returns the top value. One of the most common metaphors for the stack concept is a spring- loaded storage tray for dishes. Adding a new dish to the stack pushes any previous dishes downward. Taking the top dish away allows the dishes to pop back up.

Methods in the Stack Class stack.size() Returns the number of values pushed onto the stack. stack.isEmpty() Returns true if the stack is empty. stack.push(value) Pushes a new value onto the stack. stack.pop() Removes and returns the top value from the stack. stack.peek() Returns the top value from the stack without removing it. stack.clear() Removes all values from the stack.

Exercise: Stack Processing IsBalanced Enter string: { s = 2 * (a[2] + 3); x = (1 + (2)); } Brackets are properly nested Enter string: (a[2] + b[3) Brackets are incorrect Enter string: Write a C++ program that checks whether the bracketing operators (parentheses, brackets, and curly braces) in a string are properly matched. As an example of proper matching, consider the string { s = 2 * (a[2] + 3); x = (1 + (2)); } If you go through the string carefully, you discover that all the bracketing operators are correctly nested, with each open parenthesis matched by a close parenthesis, each open bracket matched by a close bracket, and so on.

Methods in the Queue Class queue.size() Returns the number of values in the queue. queue.isEmpty() Returns true if the queue is empty. queue.enqueue(value) Adds a new value to the end of the queue (which is called its tail). queue.dequeue() Removes and returns the value at the front of the queue (which is called its head). queue.peek() Returns the value at the head of the queue without removing it. queue.clear() Removes all values from the queue.

The LaIR Queue

Comparing Stacks and Queues Stack: Queue: push pop enqueue dequeue CBA ABC

The End