Implementing Queue and Stack with array Overview Implementin
Solution
implement the Stack using an array
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<\"stack over flow\";
return;
}
stk[++top]=x;
cout <<\"inserted\" <<x;
}
void pop()
{
if(top <0)
{
cout <<\"stack under flow\";
return;
}
cout <<\"deleted\" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<\" stack empty\";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<\" \";
}
};
main()
{
int ch;
stack st;
while(1)
{
cout <<\"\ 1.push 2.pop 3.display 4.exit\ Enter ur choice\";
cin >> ch;
switch(ch)
{
case 1: cout <<\"enter the element\";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}
implement the Queue using an array
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<\"queue over flow\";
front=rear=-1;
return;
}
queue1[++rear]=x;
cout <<\"inserted\" <<x;
}
void delet()
{
if(front==rear)
{
cout <<\"queue under flow\";
return;
}
cout <<\"deleted\" <<queue1[++front];
}
void display()
{
if(rear==front)
{
cout <<\" queue empty\";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<queue1[i]<<\" \";
}
};
main()
{
int ch;
queue qu;
while(1)
{
cout <<\"\ 1.insert 2.delet 3.display 4.exit\ Enter ur choice\";
cin >> ch;
switch(ch)
{
case 1: cout <<\"enter the element\";
cin >> ch;
qu.insert(ch);
break;
case 2: qu.delet(); break;
case 3: qu.display();break;
case 4: exit(0);
}
}
return (0);
}




