Presentation is loading. Please wait.

Presentation is loading. Please wait.

STACKS AND QUEUES UNIT 2 DS THROUGH C++.

Similar presentations


Presentation on theme: "STACKS AND QUEUES UNIT 2 DS THROUGH C++."— Presentation transcript:

1 STACKS AND QUEUES UNIT 2 DS THROUGH C++

2 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

3 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;

4 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

5 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

6 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

7 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

8 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

9 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.

10 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

11 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]

12 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)

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

14 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

15 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.

16 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).

17 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

18 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

19 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

20 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.

21 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

22 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;

23 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; }

24 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)--]; }

25 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.

26 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); }

27


Download ppt "STACKS AND QUEUES UNIT 2 DS THROUGH C++."

Similar presentations


Ads by Google