Implement and test a class deque, which is a double-ended queue that allows pushing and popping at both ends.
class queue
{
public:
void reset() { top = bottom = max_len/2; top--; }
...
private:
char s[max_len];
int bottom,top;
};
Declare and implement push_t(), pop_t(), push_b(), pop_b(), print_stack(), top_of(), bottom_of(), empty(), and full(). The function push_t() stands for push on top and pop_t() for pop on top; push_b() stands for push on bottom and pop_b() for pop on bottom.
The print_stack() function should output the stack from bottom to top. An empty stack is denoted by having the top fall below the bottom. Test each function.
//本程式用VC-SP6編寫,調試通過
#include<iostream>
#include<iomanip>
using namespace std;
const int max_len=20;
class queue
{
public:
void reset()
{
top=bottom=max_len/2;
top--;
};
void push_t(char);
void pop_t();
void push_b(char);
void pop_b();
void print_stack();
void top_of();
void bottom_of();
int empty();
int full();
private:
char s[max_len];
int bottom,top;
};
int queue::empty()
{
if(top<bottom)
return 1;
return 0;
}
int queue::full()
{
if(top==max_len&&bottom==0)
return 1;
return 0;
}
void queue::bottom_of()
{
if(!empty())
cout<<s[bottom]<<endl;
else
cout<<"the queue is empty."<<endl;
}
void queue::top_of()
{
if(!empty())
cout<<s[top]<<endl;
else
cout<<"the queue is empty."<<endl;
}
void queue::pop_b()
{
if(!empty())
bottom++;
}
void queue::pop_t()
{
if(!empty())
top--;
}
void queue::push_b(char a )
{
if(!full()&&bottom!=0)
{
bottom--;
s[bottom]=a;
}
else
cout<<"sorry,the bottom's direction is full."<<endl;
}
void queue::push_t(char a)
{
if(!full()&&top!=max_len-1)
{
top++;
s[top]=a;
}
else
cout<<"sorry,the top's direction is full."<<endl;
}
void queue::print_stack()
{
int i;
if(!empty())
{
for(i=bottom;i<=top;i++)
cout<<s[i]<<setw(2);
cout<<endl;
}
else
cout<<"the queue is empty,nothing is here."<<endl;
}
int main()
{ class queue test;
int type;
char a;
test.reset();
cout<<"please choose a command."<<endl;
cout<<"******************************"<<endl;
cout<<"1:"<<setw(3)<<" bottom of"<<endl;
cout<<"2:"<<setw(3)<<" top of"<<endl;
cout<<"3:"<<setw(3)<<" pop top"<<endl;
cout<<"4:"<<setw(3)<<" push top"<<endl;
cout<<"5:"<<setw(3)<<" pop bottom"<<endl;
cout<<"6:"<<setw(3)<<" push bottom"<<endl;
cout<<"7:"<<setw(3)<<" empty or not"<<endl;
cout<<"8:"<<setw(3)<<" full or not"<<endl;
cout<<"9:"<<setw(3)<<" print queue"<<endl;
cout<<"10:"<<setw(3)<<" reset"<<endl;
cout<<"0:"<<setw(3)<<" exit"<<endl;
cout<<"******************************"<<endl;
cin>>type;
while(type!=0)
{
switch (type)
{
case 1: test.bottom_of();
break;
case 2: test.top_of();
break;
case 3: test.pop_t();
break;
case 4: cout<<"input an element:(the type is char)"<<endl;
cin>>a;
test.push_t(a);
break;
case 5: test.pop_b();
break;
case 6: cout<<"input an element:(the type is char)"<<endl;
cin>>a;
test.push_b(a);
break;
case 7: test.empty();
if(test.empty()==1)
{
cout<<"empty"<<endl;
}
else
cout<<"not empty"<<endl;
break;
case 8: test.full();
if(test.full()==1)
{
cout<<"full"<<endl;
}
else
cout<<"not full"<<endl;
break;
case 9:
test.print_stack();
break;
case 10: test.reset();
break;
default:
return 1;
}
cin>>type;
}
return 0;
}