Collection Classes Eric Roberts CS 106B January 14, 2013 (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

Stack & Queues COP 3502.
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.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 3 Stacks.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
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:
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Building Java Programs
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
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.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Stack and Queue.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Vectors and Grids Eric Roberts CS 106B April 8, 2009.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Stacks And Queues Chapter 18.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Data Structures & Algorithms
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
April 27, 2017 COSC Data Structures I Review & Final Exam
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
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: –
Collection Classes Eric Roberts CS 106B January 12, 2015 (Part 1: Vectors, Grids, Stacks, and Queues)
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.
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Vectors and Grids Chris Piech CS 106B Lecture 2 Jan 9, 2015.
Stack and Queue APURBO DATTA.
Pointers and Linked Lists
Chapter 19: Stacks and Queues.
Stacks and Queues.
Stacks.
Stacks.
Visit for more Learning Resources
Stacks.
Stacks and Queues.
Presentation transcript:

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

Outline for Today 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. The Stanford C++ library implementation of Vector includes a shorthand form for initializing an array given a list of values, as illustrated by the following example: Vector digits; digits += 0, 1, 2, 3, 4, 5, 6, 7, 8, 9;

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.insertAt(index, value) Inserts the value before the specified index position. vec.removeAt(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(); while (true) { string line; getline(is, line); if (is.fail()) break; 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. Download solution: IsBalanced.cpp

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