Saturday, February 7, 2015

Implementation of stack containing queues as elements data structure in C++

#include<iostream>
using namespace std;
class queue
{public:
int elmnt[6];
int front;
int rear;
int isempty();
void enqueue(int e);
    int dequeue();
    queue()
    {rear=-1;
     front=-1;
    }
};
int queue::isempty()
{if(front==-1)
  {
   return 1;
  }
  else
  return 0;
}
void queue::enqueue(int e)
{ rear=(rear+1)%3;
  elmnt[rear]=e;
  if(front==-1)
  front++;
}
int queue::dequeue()
{int c;
  if(isempty());
  else
   {c=elmnt[front];
    if(front==rear)
    {front=-1;
     rear=-1;
     return c;
    }
    else
     front=(front+1)%3;
    return(c);
   }
}
class stack
{public:
  queue q[10],r;
  int top,t,arr[50];
  void push(queue f);
  queue pop();
};
void stack::push(queue f)
{q[++top]=f;
}
queue stack::pop()
{
return q[top--];
}

int main()
{stack s;
 int no,i,j,n;
 s.top=-1;
 cout<<"Enter the no of stacks:";
 cin>>n;
 for(i=0;i<n;i++)
 {  cout<<"Enter in the "<<i+1<<" stack:\n";
    for(j=0;j<3;j++)
     {cin>>no;
 s.r.enqueue(no);
     }
    s.push(s.r);
 }
 for(i=n;i>0;i--)
  {cout<<"Thus the "<<i<<" stack contains;\n";
     s.r=s.pop();
      for(j=0;j<3;j++)
       {cout<<s.r.dequeue()<<" ";
       }
       cout<<endl;
  }
}

No comments:

Post a Comment

Contributors

Translate