You are on page 1of 2

// // // // //

Justin C. Miller made for : http://www.geocities.com/neonprimetime.geo/index.html 3-15-2001 Description : Stack of 10 C Strings (names) at max made on MS Visual C++ 6.0

#include <iostream.h> #include <stdlib.h> #include <string.h> const int max = 10 ; class Stack{ private: char * stack[max] ; int size ; int top ; public: Stack(){ size = 0 ; for(int i = 0 ; i < max ; i++) stack[i] = new char[25] ; top = -1 ; } ~Stack(){} bool Push(char * x){ if(top < max){ strcpy(stack[top+1], x) ; top = top + 1 ; return true ; } return false ; } char * Pop(){ if(top >= 0){ top = top - 1; return stack[top + 1] ; } return NULL ; } void PrintStack(){ cout << "Stack" << endl ; cout << "-----" << endl ; for(int i = top ; i >= 0 ; i--) cout << stack[i] << endl ; } }; int main(){ Stack a ; char * x = new char[25]; for(int i = 0 ; i < 10 ; i++){ cout << "Please enter person's name #" << (i+1) << endl ; cin >> x ; if(!a.Push(x)){ cout << "Stack is Full!" << endl ; cout << "Name not inserted!" << endl ; } }

a.PrintStack() ; system("pause") ; cout << "\nThese 2 names are being popped of the stack" << endl ; for(int y = 0 ; y < 2 ; y++) cout << a.Pop() << endl ; a.PrintStack() ; system("pause") ; return 0 ; }

You might also like