/********************************************************************** * Copyright (c) 2015,wk Studios * Filename: stack.h * compiler:gcc,vs,vc6.0 Win32 * AUTHOR:WK * time:2015 3 29 ******************* /#include <iostream>using namespace Std;const int size= 10;class stack{private: int stck[size];//array for storing data int tos in stack; Stack top position (subscript of Array) public:stack (); void push (int ch); The function declaration presses the data into the stack fuction int pop (); Declares that data is popped from the stack fuctionvoid showstack (); Declare display stack data function};
/********************************************************************** * Copyright (c) 2015,wk Studios * Filename: stack.cpp * compiler:gcc,vs,vc6.0 Win32 * AUTHOR:WK * time:2015 3 29 ***************** /#include "stack.h" //constructor, implementation of the initialization stack Stack::stack () { tos=0; stck[size]=0;} The implementation of the data function pushed into the stack void stack::p ush (int ch) { if (tos==size) { cout<< "stack is full!\n"; return; } Stck[tos]=ch; tos++; cout<< "You had pushed a data into the stack!\n";} The implementation of the data function is popped from the stack int stack::p op () { if (0==tos) { cout<< "stack is empty!\n"; return 0; } tos--; return Stck[tos];} Implementation of functions that display data in the stack void Stack::showstack () { cout<< "the content of stack:\n"; if (0==tos) { cout<< "the Stack has no data!\n"; return; } for (int i=tos-1;i>=0;i--) { cout<<stck[i]<< '; } cout<< ' \ n ';}
/********************************************************************** * Copyright (c) 2015,wk Studios * Filename:ma In.cpp * compiler:gcc,vs,vc6.0 Win32 * AUTHOR:WK * time:2015 3 29 ************************************************ /#include "stack.h" int main () {cout<<endl;//Flushes the buffer stack SS while wrapping; Defines the object int x=0; Char ch; cout<< "<I>-----push data to stack!\n"; cout<< "<O>-----pop data from stack!\n"; cout<< "<S>-----Show content of stack!\n"; cout<< "<Q>-----Quit!!!!!!! \ n "; while (1) {cout<< "Please select a item:"; Cin>>ch;ch=toupper (ch); switch (CH) {case ' I ': cout<< "Enter The value of the want to push: "; Cin>>x;ss.push (ch); Break;case ' O ': X=ss.pop ();cout<<" Pop "<<x< < "from stack!\n"; Break;case ' S ': ss. Showstack (); break; Case ' Q ': return 0;break; default:cout<< "You have iputted a wrong item!!!! Please try AgAin!\n "; Continue;} }}
C + + simple implementation of pressure stack out of stack