CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Similar documents
CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

CMSC202 Computer Science II for Majors

Chapter 18: Stacks And Queues

Chapter 18: Stacks And Queues

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

! A data type for which: ! An ADT may be implemented using various. ! Examples:

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

ADTs: Stacks and Queues

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

STL Standard Template Library

COEN244: Class & function templates

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Chapter 12 - Templates

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CE221 Programming in C++ Part 1 Introduction

Lecture Data Structure Stack

CMSC 341 Lecture 10 Binary Search Trees

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

An Introduction to Queues With Examples in C++

CS24 Week 4 Lecture 2

Name CPTR246 Spring '17 (100 total points) Exam 3

CSE 303: Concepts and Tools for Software Development

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Object Oriented Programming COP3330 / CGS5409

Introduction to the C programming language

Introduction to the C programming language

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials)

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

Computational Physics

Motivation for Templates

UNIT-2 Stack & Queue

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

STACKS AND QUEUES. Problem Solving with Computers-II

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

// The next 4 functions return true on success, false on failure

1 Short Answer (7 Points Each)

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Introduction to Programming using C++

AN OVERVIEW OF C++ 1

Function Overloading

CMSC202 Computer Science II for Majors

UNIVERSITY REGULATIONS

G Programming Languages - Fall 2012

Class and Function Templates

Introduction to Data Structures and Algorithms

Motivation for Templates. Class and Function Templates. One Way to Look at Templates...

PIC 10A Objects/Classes

CS201 - Introduction to Programming Glossary By

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

CMSC 341 Lecture 14: Priority Queues, Heaps

Datastructuren André Deutz

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Dynamic Data Structures

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

.:: UNIT 4 ::. STACK AND QUEUE

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Short Notes of CS201

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Lesson 13 - Vectors Dynamic Data Storage

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

CS 376b Computer Vision

CS 206 Introduction to Computer Science II

Exercise 1.1 Hello world

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

CMSC 341 Lecture 7 Lists

Lecture-5. STL Containers & Iterators

COMP322 - Introduction to C++ Lecture 07 - Introduction to C++ classes

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

1. The term STL stands for?

G52CPP C++ Programming Lecture 18

pointers & references

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

III. Classes (Chap. 3)

Data Structure. Recitation VII

CS197c: Programming in C++

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

Fast Introduction to Object Oriented Programming and C++

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes


C++ Templates. David Camp

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Unit 4 Basic Collections

Data Abstraction and Specification of ADTs

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

Transcription:

CMSC 341 Lecture 6 Templates, Stacks & Queues Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Today s Topics Data types in C++ Overloading functions Templates How to implement them Possible problems (and solutions) Compiling with templates Stacks Queues UMBC CMSC 341 Templates 2

Data Types 3

Data Types (Review) Values of variables are stored somewhere in an unspecified location in the computer memory as zeros and ones Our program does not need to know the exact location where a variable is stored It can simply refer to it by its name What the program needs to be aware of is the kind of data stored in the variable UMBC CMSC 341 Templates 4

Fundamental Data Types in C++ Source: http://www.cplusplus.com/doc/tutorial/variables/ UMBC CMSC 341 Templates 5

Overloading Functions 6

What is Overloading? Used to create multiple definitions for functions in various settings: Class constructors Class operators Functions Let s look at a simple swap function UMBC CMSC 341 Templates 7

Example: Swap Function Here is a function to swap two integers: void SwapVals(int &v1, int &v2) { int temp; } temp = v1; v1 = v2; v2 = temp; what if we want to swap two floats? what do we need to change? UMBC CMSC 341 Templates 8

Example: Swap Function Here is a function to swap two floats: void SwapVals(float &v1, float &v2) { float temp; } temp = v1; v1 = v2; v2 = temp; what if we want to swap two chars? what do we need to change? UMBC CMSC 341 Templates 9

Example: Swap Function Here is a function to swap two chars: void SwapVals(char &v1, char &v2) { char temp; } temp = v1; v1 = v2; v2 = temp; what if we want to swap two strings? what do we need to change? UMBC CMSC 341 Templates 10

Example: Swap Function This is getting ridiculous! We should be able to write just one function that can handle all of these things The only difference is the data type, after all This is possible by using templates UMBC CMSC 341 Templates 11

Templates 12

Common Uses for Templates Some common algorithms that easily lend themselves to templates: Swap Sort Search FindMax FindMin UMBC CMSC 341 Templates 13

maxx() Overloaded Example float maxx ( const float a, const float b ); int maxx ( const int a, const int b ); Rational maxx ( const Rational& a, const Rational& b); mytype maxx ( const mytype& a, const mytype& b); Code for each looks the same if ( a < b ) return b; else return a; we want to reuse this code for all types UMBC CMSC 341 Templates 14

What are Templates? Templates let us create functions and classes that can use generic input and types This means that functions like SwapVals() only need to be written once And can then be used for almost anything UMBC CMSC 341 Templates 15

Indicating Templates To let the compiler know you are going to apply a template, use the following: template <class T> this keyword tells the compiler that what follows this will be a template UMBC CMSC 341 Templates 16

Indicating Templates To let the compiler know you are going to apply a template, use the following: template <class T> this does not mean class in the same sense as C++ classes with members! in fact, another keyword we can use is actually typename, because we are defining a new type but class is more common by far, and so we will use class to avoid confusion UMBC CMSC 341 Templates 17

Indicating Templates To let the compiler know you are going to apply a template, use the following: template <class T> T is the name of our new type we can call it anything we want, but using T is the style convention (of course, we can t use int or for or any other types or keywords as a name for our type) UMBC CMSC 341 Templates 18

Indicating Templates To let the compiler know you are going to apply a template, use the following: template <class T> What this line means overall is that we plan to use T in place of a data type e.g., int, char, myclass, etc. This template prefix needs to be used before function declarations and function definitions UMBC CMSC 341 Templates 19

Template Example Function Template template <class T> T maxx ( const T& a, const T& b) { if ( a < b ) return b; else return a; } Notice how T is mapped to int everywhere in the function Compiler generates code based on the argument type cout << maxx(4, 7) << endl; Generates the following: int maxx ( const int& a, const int& b) { if ( a < b ) return b; else return a; } UMBC CMSC 341 Templates 20

Using Templates When we call these templated functions, nothing looks different: SwapVals(intOne, inttwo); SwapVals(charOne, chartwo); SwapVals(strOne, strtwo); SwapVals(myClassA, myclassb); UMBC CMSC 341 Templates 21

(In)valid Use of Templates Which of the following will work? SwapVals(int, int); SwapVals(char, string); SwapVals("hello", "world"); SwapVals(double, float); SwapVals(Shape, Shape); These use two different types, and the SwapVals() function doesn t allow this. These are two string literals we can t swap those! UMBC CMSC 341 Templates 22

Template Requirements Templated functions can handle any input types that makes sense i.e., any data type where the behavior that occurs in the function is defined Even user-defined types! As long as the behavior is defined What happens if the behavior isn t defined? Compiler will give you an error UMBC CMSC 341 Templates 23

Overloading Templates 24

Why Overload Templates? Sometimes, even though the behavior is defined, the function performs incorrectly Assume the code: char* s1 = "hello"; char* s2 = "goodbye"; cout << maxx( s1, s2 ); What is the call to maxx() actually going to do? UMBC CMSC 341 Templates 25

Incorrect Template Performance The compiler generates: char* maxx (const char*& a, const char*& b) { if ( a < b ) return b; else return a; } Is this what we want? UMBC CMSC 341 Templates 26

Overloading a Template Fix this by creating a version of maxx() specifically to handle char* variables Compiler will use this instead of the template char* maxx(char *a, char *b) { if (strcmp(a,b) < 0) return b; else return a; } UMBC CMSC 341 Templates 27

Compiling Templates 28

Compiler Handling of Templates Exactly what versions of SwapVals() are created is determined at compile what? time If we call SwapVals() with integers and strings, the compiler will create versions of the function that take in integers and strings UMBC CMSC 341 Templates 29

Separate Compilation Which versions of templated function to create are determined at compile time How does this affect our use of separate compilation? Function declaration in.h file Function definition in.cpp file Function call in separate.cpp file UMBC CMSC 341 Templates 30

Separate Compilation: Example Code Here s an illustrative example: #include "swap.h" int main() { int a = 3, b = 8; SwapVals(a, b); } main.cpp template <class T> void SwapVals(T &v1, T &v2); swap.h #include swap.h template <class T> void SwapVals(T &v1, T &v2) { T temp; temp = v1; v1 = v2; v2 = temp; } swap.cpp UMBC CMSC 341 Templates 31

Separate Compilation Most compilers (including GL s) cannot handle separate compilation with templates When swap.cpp is compiled There are no calls to SwapVals() swap.o has no SwapVals() definitions UMBC CMSC 341 Templates 32

Separate Compilation When main.cpp is compiled It assumes everything is fine Since swap.h has the appropriate declaration When main.o and swap.o are linked Everything goes wrong error: undefined reference to void SwapVals<int>(int&, int&) UMBC CMSC 341 Templates 33

Separate Compilation Solutions The template function definition code must be in the same file as the function call code Two ways to do this: place function definition in main.c place function definition in swap.h, which is #included in main.c UMBC CMSC 341 Templates 34

Template Compilation Solution Second option keeps some sense of separate compilation, and better allows code reuse #include "swap.h" int main() { int a = 3, b = 8; SwapVals(a, b); } main.cpp // declaration template <class T> void SwapVals(T &v1, T &v2); // definition template <class T> void SwapVals(T &v1, T &v2) { T temp; temp = v1; v1 = v2; v2 = temp; } swap.h UMBC CMSC 341 Templates 35

CMSC 341 Stacks and Queues Prof. Jeremy Dixon

Topics for Today Introduction to Standard Template Library (STL) Stacks Types of Stacks Examples Queues Types of Queues Examples

Standard Template Library (STL)

Standard Template Library (STL) The Standard Template Library (STL) is a C++ library of container classes, algorithms, and iterators Provides many of the basic algorithms and data structures of computer science From: https://www.sgi.com/tech/stl/stl_introduction.html

Considerations of the STL Containers replicate structures very commonly used in programming. Many containers have several member functions in common, and share functionalities. From: http://www.cplusplus.com/reference/stl/

Considerations of the STL The decision of which type of container to use for a specific need depends on: the functionality offered by the container the efficiency of some of its members (complexity) From: http://www.cplusplus.com/reference/stl/

Types of Containers Sequence containers Array, vector, deque, forward_list, list Container adapters Stacks, queues, priority_queues Associative containers (and the unordered) Set, multiset, map, multimap Focus of Today

Standard Containers Sequences: vector: Dynamic array of variables, struct or objects. Insert data at the end. list: Linked list of variables, struct or objects. Insert/remove anywhere. Sequence means order does matter

Container Adapters Container adapters: stack LIFO queue FIFO adapter means VERY LIMITED functionality

Will we use STL? Today we are going to talk about the ways that we can implement stacks and queues 3 Ways to Create a Stack or Queue Create a static stack or queue using an array Create a dynamic stack or queue using a linked list Create a stack or queue using the STL

Stacks

Stacks

Introduction to Stacks A stack is a data structure that stores and retrieves items in a last in first out (LIFO) manner.

Applications of Stacks Computer systems use stacks during a program s execution to store function return addresses, local variables, etc. Some calculators use stacks for performing mathematical operations.

Implementations of Stacks Static Stacks Fixed size Can be implemented with an array Dynamic Stacks Grow in size as needed Can be implemented with a linked list Using STL (dynamic)

Stack Operations Push causes a value to be stored in (pushed onto) the stack Pop retrieves and removes a value from the stack

The Push Operation Suppose we have an empty integer stack that is capable of holding a maximum of three values. With that stack we execute the following push operations. push(5); push(10); push(15);

The Push Operation The state of the stack after each of the push operations:

The Pop Operation Now, suppose we execute three consecutive pop operations on the same stack:

Other Stack Operations isfull(): A Boolean operation needed for static stacks. Returns true if the stack is full. Otherwise, returns false. isempty(): A Boolean operation needed for all stacks. Returns true if the stack is empty. Otherwise, returns false.

Static Stacks

Static Stacks A static stack is built on an array As we are using an array, we must specify the starting size of the stack The stack may become full if the array becomes full

Member Variables for Stacks Three major variables: Pointer Creates a pointer to stack size Tracks elements in stack top Tracks top element in stack

Member Functions for Stacks CONSTRUCTOR Creates a stack DESTRUCTOR Deletes a stack push() Pushes element to stack pop() Pops element from stack isempty() Is the stack empty? isfull() Is the stack full?

#ifndef INTSTACK_H #define INTSTACK_H Static Stack Definition class IntStack { private: int *stackarray; int stacksize; int top; public: }; IntStack(int); ~IntStack() {delete[] stackarray;} void push(int); void pop(int &); bool isfull(); bool isempty(); pointer size() top() Constructor Destructor push() pop() isfull() isempty() Member Variables Member Functions #endif

Dynamic Stacks

Dynamic Stacks A dynamic stack is built on a linked list instead of an array. A linked list based stack offers two advantages over an array based stack. No need to specify the starting size of the stack. A dynamic stack simply starts as an empty linked list, and then expands by one node each time a value is pushed. A dynamic stack will never be full, as long as the system has enough free memory.

Member Variables for Dynamic Stacks Parts: Linked list Linked list for stack (nodes) size Tracks elements in stack

Member Functions for Dynamic Stacks CONSTRUCTOR Creates a stack DESTRUCTOR Deletes a stack push() Pushes element to stack pop() Pops element from stack isempty() Is the stack empty? top() What is the top element? What happened to isfull()?

Dynamic Stack class DynIntStack { private: struct StackNode { int value; StackNode *next; }; Linked list of elements value pointer top Member Variables StackNode *top; public: DynIntStack(void) { top = NULL; } void push(int); void pop(int &); const Elem& top() const throw(stackempty); bool isempty(void); }; Constructor push() pop() top() isempty() Member Functions

Common Problems with Stacks Stack underflow no elements in the stack, and you tried to pop Stack overflow maximum elements in stack, and tried to add another not an issue using STL or a dynamic implementation Practice question: Stack Min - How would you design a stack which, in addition to push and pop, has a function min which returns the minimum element? Push, pop and min should all operate in 0(1) time.

Queues

Introduction to the Queue Like a stack, a queue is a data structure that holds a sequence of elements. A queue, however, provides access to its elements in first in, first out (FIFO) order. The elements in a queue are processed like customers standing in a line: the first customer to get in line is the first one to be served (and leave the line).

Example Applications of Queues In a multi user system, a queue is used to hold print jobs submitted by users, while the printer services those jobs one at a time. Communications software also uses queues to hold information received over networks. Sometimes information is transmitted to a system faster than it can be processed, so it is placed in a queue when it is received.

Implementations of Queues Static Queues Fixed size Can be implemented with an array Dynamic Queues Grow in size as needed Can be implemented with a linked list Using STL (dynamic) Just like stacks!

Queue Operations Think of queues as having a front and a rear. rear: position where elements are added front: position from which elements are removed

Queue Operations The two primary queue operations are enqueuing and dequeuing. To enqueue means to insert an element at the rear of a queue. To dequeue means to remove an element from the front of a queue.

Queue Operations Suppose we have an empty static integer queue that is capable of holding a maximum of three values. With that queue we execute the following enqueue operations. Enqueue(3); Enqueue(6); Enqueue(9);

Queue Operations Enqueue The state of the queue after each of the enqueue operations.

Queue Operations Dequeue Now let's see how dequeue operations are performed. The figure on the right shows the state of the queue after each of three consecutive dequeue operations An important remark After each dequeue, remaining items shift toward the front of the queue. 3 removed 6 removed 9 removed

Efficiency Problem of Dequeue & Solution Shifting after each dequeue operation causes inefficiency. Solution Let front index move as elements are removed let rear index "wrap around" to the beginning of array, treating array as circular Similarly, the front index as well Yields more complex enqueue, dequeue code, but more efficient Let's see the trace of this method on the board for the enqueue and dequeue operations given on the right (queue size is 3) Enqueue(3); Enqueue(6); Enqueue(9); Dequeue(); Dequeue(); Enqueue(12); Dequeue();

Implementation of a Static Queue The previous discussion was about static arrays Container is an array Class Implementation for a static integer queue Member functions enqueue() dequeue() isempty() isfull() clear()

Member Variables for Static Queues Five major variables: queuearray Creates a pointer to queue queuesize Tracks capacity of queue numitems Tracks elements in queue front rear The variables front and rear are used when our queue rotates, as discussed earlier

Member Functions for Queues CONSTRUCTOR Creates a queue DESTRUCTOR Deletes a queue enqueue() Adds element to queue dequeue() Removes element from queue isempty() Is the queue empty? isfull() Is the queue full? clear() Empties queue

Static Queue Example #ifndef INTQUEUE_H #define INTQUEUE_H class IntQueue { private: int *queuearray; int queuesize; int front; int rear; int numitems; public: IntQueue(int); void enqueue(int); void dequeue(int &); bool isempty() const; bool isfull() const; void clear(); }; #endif pointer queuesize() front rear numitems Constructor enqueue() dequeue() isempty() isfull() clear() Member Variables Member Functions

STL Queues

STL Queues Another way to implement a queue is by using the standard library An STL queue leverages the pre existing library to access the data structure Much easier to use

#include <iostream> // std::cin, std::cout #include <queue> // std::queue using namespace std; int main () { std::queue<int> myqueue; int myint; std::cout << "Please enter some integers (enter 0 to end):\n"; STL Queue Example } do { std::cin >> myint; myqueue.push (myint); } while (myint); std::cout << "myqueue contains: "; while (!myqueue.empty()) { std::cout << ' ' << myqueue.front(); myqueue.pop(); } std::cout << '\n'; return 0;