Sessions 26 and 27 - Data Structures & Algorithm

Removefromfront data template stack class definition

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 27: Data Structures & Algorithm 38 Stacks Upcoming program Create stack from list insertAtFront, removeFromFront Software reusability Inheritance Stack inherits from List Composition Stack contains a private List object Performs operations on that object Makes stack implementation simple Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 39 Bottom of stack has null link 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 composed list object: queue like stack List< STACKTYPE > stackList; // composed List object from list return stackList.removeFromFront( data ); // Template Stack class definition derived from class List. #ifndef STACK_H #define STACK_H Stack inherits from List. #include "list.h" // List class definition template< class STACKTYPE > class Stack : private List< STACKTYPE > { public: // push calls List function insertAtFront void push( const STACKTYPE &data ) { insertAtFront( data ); Define push and pop, which call insertAtFront insertAtFront and removeFromFront. } // end function push // pop calls List function removeFromFront bool pop( STACKTYPE &data ) { return removeFromFront( data ); } // end function pop Sharif University of Technology Stack< double > doubStack; // create Stack of ints double value=1; doubleStack.push( value ); Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 40 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 // isStackEmpty calls List function isEmpty bool isStackEmpty() const { return isEmpty(); } // end function isStackEmpty // printStack calls List function print void printStack() const { print(); } // end function print }; // end class Stack #endif Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 41 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 16 17 18 19 20 21 22 23 24 // Template Stack class test program. #include <iostream> using std::endl; #include "stack.h" // Stack class definition int main() { Stack< int > intStack; // create Stack of ints cout << "processing an integer Stack" << endl; // push integers onto intStack for ( int i = 0; i < 4; i++ ) { intStack.push( i ); intStack.printStack(); } // end for // pop integers from intStack int popInteger; Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 42 25 26 27 28 29 30 31 32 33 34 35 36 37 37 38 39 40 41 42 43 44 while ( !intStack.isStackEmpty() ) { intStack.pop( popInteger ); cout << popInteger << " popped from stack" << endl; intStack.printStack(); } // end while Stack< double > doubleStack; // create Stack of doubles double value = 1.1; cout << "processing a double Stack" << endl; // push floating-point values onto doubleStack for ( int j = 0; j< 4; j++ ) { doubleStack.push( value ); doubleStack.printStack(); value += 1.1; } // end for Sharif University of Technology Introduction to Programmin...
View Full Document

This note was uploaded on 09/01/2013 for the course CE CE230 taught by Professor Arash during the Fall '13 term at Sharif University of Technology.

Ask a homework question - tutors are online