【发布时间】:2014-03-01 16:05:39
【问题描述】:
#include<iostream>
#include<string.h>
using namespace std;
class queue
{
int f,r;
int a[5];
public:
queue()
{
f=r=-1;
}
在数组中插入元素
void enqueu(int n)
{
if(r==4)
cout<<"Overflow"<<endl;
else
{
r++;
a[r]=n;
}
if(f==-1)
f=0;
}
从数组中删除元素
void dequeu()
{
if(f==-1)
cout<<"Underflow"<<endl;
else if(f==4)
f=-1;
else
{
cout<<"The Deleted Element is "<<a[f]<<endl;
f++;
}
}
当我尝试显示 Array 的元素时,我删除的元素也会出现。
void show()
// showing array elements
{
for(int i=0;i<5;i++)
cout<<a[i]<<endl;
}
};
int main()
{
queue obj;
int n,num;
我在这里使用 do while 会不断询问是插入还是删除。
do
{
cout<<"1: Insert Element In Queue"<<" 2: Delete Element "<<" 3:Press 0 to Exit<<endl;
cin>>n;
switch(n)
{
case 1:
{
cout<<"Enter Element to insert"<<endl;
cin>>num;
obj.enqueu(num);
break;
}
case 2:
{
obj.dequeu();
break;
}
}
}
while(n!=0);
obj.show();
return 0;
}
【问题讨论】:
-
另外,在
dequeu()中,如果f==r,删除元素并将f和r都更改为-1。 -
为什么你的缩进如此时髦?