Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * masfiq
- * 17 march, 2017
- *
- */
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- class twoStacks
- {
- private:
- int *arr;
- int size;
- int top1;
- int top2;
- public:
- twoStacks(int size) //Constructor
- {
- this->size = size;
- this->arr = new int[size];
- top1 = -1;
- top2 = size;
- }
- void push1(int x)
- {
- if(top2 == top1+1)
- {
- cout<<"stack overflowed."<<endl;
- exit(1);
- }
- else
- {
- top1++;
- arr[top1] = x;
- }
- }
- void push2(int x)
- {
- if(top2 == top1+1)
- {
- cout<<"stack overflowed."<<endl;
- exit(1);
- }
- else
- {
- top2--;
- arr[top2] = x;
- }
- }
- int pop1()
- {
- if(top1 == -1)
- {
- cout<<"stack underflowed."<<endl;
- exit(1);
- }
- else
- {
- int x = arr[top1];
- top1--;
- return x;
- }
- }
- int pop2()
- {
- if(top2 == size)
- {
- cout<<"stack underflowed"<<endl;
- exit(1);
- }
- else
- {
- int x = arr[top2];
- top2++;
- return x;
- }
- }
- void print_stack1(){
- for(int i=0; i<= top1; i++){
- cout<<arr[i]<<"->";
- }
- cout<<endl;
- }
- void print_stack2(){
- for(int i=size-1; i >= top2; i--){
- cout<<arr[i]<<"->";
- }
- cout<<endl;
- }
- };
- int main()
- {
- twoStacks ts(5);
- ts.push1(5);
- ts.push2(10);
- ts.push2(15);
- ts.push1(11);
- ts.push2(7);
- ts.print_stack1();
- ts.print_stack2();
- // cout << "Popped element from stack1 is " << ts.pop1();
- // ts.push2(40);
- // cout << "\nPopped element from stack2 is " << ts.pop2();
- return 0;
- }
- /*
- Output:
- 5->11->
- 10->15->7->
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement