STACKS AND QUEUES UNIT 2 DS THROUGH C++.

Slides:



Advertisements
Similar presentations
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Advertisements

Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Lecture 9 Concepts of Programming Languages
Object Oriented Data Structures
Data Structure Dr. Mohamed Khafagy. Stacks Stack: what is it? ADT Applications Implementation(s)
Data Structures Using C++ 2E
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 22 - C++ Templates Outline 22.1Introduction.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Chapter 22 - C++ Templates Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 22 - C++ Templates
Stacks.
Introduction to Computers Computer Generations
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Abstract Data Types and Encapsulation Concepts
C++ Templates.
Data Structure Interview Question and Answers
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
Homework 4 questions???.
Standard Template Library (STL)
C++ Plus Data Structures
Data Structures Interview / VIVA Questions and Answers
CS212: Object Oriented Analysis and Design
CSCE 3110 Data Structures & Algorithm Analysis
Stack Data Structure, Reverse Polish Notation, Homework 7
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Abstract Data Types (ADTs)
Lecture 9 Concepts of Programming Languages
CSCE 3110 Data Structures & Algorithm Analysis
Stacks.
ADT Implementations: Templates and Standard Containers
structures and their relationships." - Linus Torvalds
Abstract Data Types and Encapsulation Concepts
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Classes and Objects.
Abstract Data Type Abstract Data Type as a design tool
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Introduction to Data Structure
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Lab4 problems More about templates Some STL
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Standard Template Library
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Standard Template Library
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
structures and their relationships." - Linus Torvalds
Abstract Data Types Stacks CSCI 240
Lecture 9 Concepts of Programming Languages
Abstract Data Types (ADTs)
Data Structures & Programming
Presentation transcript:

STACKS AND QUEUES UNIT 2 DS THROUGH C++

C++ Templates Templates Part of the ongoing development of the C++ language Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type. A template is a blueprint or formula for creating a generic class or a function. Integral part of the larger C++ Standard Library The Standard Template Library Easily create a large range of related functions or classes Function template - the blueprint of the related functions

Class Templates Class templates Format: Allow type-specific versions of generic classes Format: template <class T> class ClassName{ definition } Need not use "T", any identifier will work To create an object of the class, type ClassName< type > myObject; Example: Stack< double > doubleStack;

Template class functions Declared normally, but preceded by template<class T> Generic data in class listed as type T Binary scope resolution operator used Template class function definition: template<class T> MyClass< T >::MyClass(int size) { myArray = new T[size]; } Constructor definition - creates an array of type T

Class Templates and Non-type Parameters Can use non-type parameters in templates Default argument Treated as const Example: template< class T, int elements > Stack< double, 100 > mostRecentSalesFigures; Declares object of type Stack< double, 100> This may appear in the class definition: T stackHolder[ elements ]; //array to hold stack Creates array at compile time, rather than dynamic allocation at execution time

Classes can be overridden For template class Array, define a class named Array<myCreatedType> This new class overrides then class template for myCreatedType The template remains for unoverriden types

Templates and Inheritance A class template can be derived from a template class A class template can be derived from a non-template class A template class can be derived from a class template A non-template class can be derived from a class template

Templates and friends Friendships allowed between a class template and Global function Member function of another class Entire class friend functions Inside definition of class template X: friend void f1(); f1() a friend of all template classes friend void f2( X< T > & ); f2( X< int > & ) is a friend of X< int > only. The same applies for float, double, etc. friend void A::f3(); Member function f3 of class A is a friend of all template classes

friend classes friend void C< T >::f4( X< T > & ); C<float>::f4( X< float> & ) is a friend of class X<float> only friend classes friend class Y; Every member function of Y a friend with every template class made from X friend class Z<T>; Class Z<float> a friend of class X<float>, etc.

Templates and static Members Non-template class static data members shared between all objects Template classes Each class (int, float, etc.) has its own copy of static data members static variables initialized at file scope Each template class gets its own copy of static member functions

Templates to Represent Container Classes The C++ standard library: provides support for language features (memory management, RTTI) supplies info about implementation-dependent aspects (largest float value etc) supplies functions that cannot be implemented optimally in the language itself for every system (sqrt, memmove etc.) provides strings and I/O streams (with internationalization and localization support) provides a framework of containers (vectors, maps etc.) and generic algorithms (traversals, sorting, merging etc.) supports numerical computations (complex numbers, BLAS=Basic Linear Algebra Subprograms etc.) provides a framework for extending the provided facilities provides a common foundation for other libraries Framework of containers, algorithms and iterators is commonly referred as STL = Standard Template Library [Stepanov, 1994]

Containers DEFINITON [Container] A container is an object that holds other objects. Examples: list, vectors, stacks etc. Using abstract classes to provide a standard interface for containers and iterators – it doesn’t meet the efficiency requirements! (small functions, that cannot be inlined because they are virtual). Advantages of containers: simple and efficient each container provides a set of common operations; they can be used interchangeably wherever reasonable standard iterators are available for each container type-safe and homogeneous are non-intrusive (i.e. an object doesn’t need to have a special base class to be a member of the container) each container takes an argument called allocator, which can be used as a handle for implementing services for every container (persistence, I/O)

Basic containers: vector, list, deque. Other containers: stack, queue, priority queue (implemented using basic containers). Associative containers: map, multimap Almost containers: bitset, String, valarray

Abstract Data Type Abstract Data Type as a design tool Concerns only on the important concept or model No concern on implementation details. Stack & Queue is an example of ADT

Stack & Queue vs. Array Stack & Queue vs. Array Arrays are data storage structures while stacks and queues are specialized DS and used as programmer’s tools. Stack – a container that allows push and pop Queue - a container that allows enqueue and dequeue No concern on implementation details. In an array any item can be accessed, while in these data structures access is restricted. They are more abstract than arrays.

Stacks Allows access to only the last item inserted. An item is inserted or removed from the stack from one end called the “top” of the stack. This mechanism is called Last-In-First-Out (LIFO).

Last In First Out B A C B A D C B A E D C B A top D C B A top top top

Stack Applications Real life Pile of books Plate trays More applications related to computer science Program execution stack (read more from your text) Evaluating expressions

Stack ADT objects: a finite ordered list with zero or more elements. methods: for all stack  Stack, item  element, max_stack_size  positive integer Stack createS(max_stack_size) ::= create an empty stack whose maximum size is max_stack_size Boolean isFull(stack, max_stack_size) ::= if (number of elements in stack == max_stack_size) return TRUE else return FALSE Stack push(stack, item) ::= if (IsFull(stack)) stack_full else insert item into top of stack and return

Boolean isEmpty(stack) ::= if(stack == CreateS(max_stack_size)) return TRUE else return FALSE Element pop(stack) ::= if(IsEmpty(stack)) return else remove and return the item on the top f the stack.

Array-based Stack Implementation Allocate an array of some size (pre-defined) Maximum N elements in stack Bottom stack element stored at element 0 last index in the array is the top Increment top when one element is pushed, decrement after pop

Stack createS(max_stack_size) ::= #define MAX_STACK_SIZE 100 / Stack createS(max_stack_size) ::= #define MAX_STACK_SIZE 100 /* maximum stack size */ typedef struct { int key; /* other fields */ } element; element stack[MAX_STACK_SIZE]; int top = -1; Boolean isEmpty(Stack) ::= top< 0; Boolean isFull(Stack) ::= top >= MAX_STACK_SIZE-1;

Push void push(int *top, element item) { /* add an item to the global stack */ if (*top >= MAX_STACK_SIZE-1) { stack_full( ); return; } stack[++*top] = item; }

Pop element pop(int *top) { /* return the top element from the stack */ if (*top == -1) return stack_empty( ); /* returns and error key */ return stack[(*top)--]; }

The Towers of Hanoi A Stack-based Application GIVEN: three poles a set of discs on the first pole, discs of different sizes, the smallest discs at the top GOAL: move all the discs from the left pole to the right one. CONDITIONS: only one disc may be moved at a time. A disc can be placed either on an empty pole or on top of a larger disc.

void hanoi (int discs, Stack fromPole, Stack toPole, Stack aux) { Disc d; if( discs >= 1) { hanoi(discs-1, fromPole, aux, toPole); d = fromPole.pop(); toPole.push(d); hanoi(discs-1,aux, toPole, fromPole); }