Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int size=5;
- class cstack
- {
- public:
- void print();
- bool isempty();
- bool isfull();
- void push(int el); // add last
- void pop(); // delete last
- int search(int el);
- void gettop();
- cstack();
- private:
- int stack[size];
- int top;
- };
- #include "class.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- cstack s1;
- s1.push(4);
- s1.push(2);
- s1.push(5);
- s1.pop();
- s1.print();
- } // end main
- cstack::cstack()
- {
- top=0;
- }
- bool cstack::isempty()
- {
- if (top==0)
- return true;
- else
- return false;
- }
- bool cstack::isfull()
- {
- if (top==size)
- return true;
- else
- return false;
- }
- void cstack::push(int el)
- { // isfull check before
- stack[top]=el;
- top++;
- }
- void cstack::pop()
- {
- if(!isempty()){
- stack[top-1];
- top--;}
- else
- cout<<"empty";
- }
- int cstack::search(int el)
- {
- int pos=-1;
- //isempty check here
- for(int i=top; i>=0;i--)
- {
- if(stack[i]==el)
- {pos = i;
- break;}
- else
- cout<<"element not found"<<endl;
- }
- return pos;
- }
- void cstack::gettop()
- {
- stack[top-1];
- }
- void cstack::print()
- {
- for(int i=top-1; i>=0;i--)
- {
- cout<<stack[i]<<" ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement