之前版本: 1.v1.0
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
#define LEN 100
struct Queue
{
int num[LEN];
int front;
int rear;
};
int c = 0;
void initQueue( struct Queue &a )
{
a.front = -1;
a.rear = 0;
return;
}
bool isEmpty( struct Queue &a )
{
bool ans;
ans = false;
if( a.front == a.rear )
{
ans = true;
}
return ans;
}
bool isFull( struct Queue &a )
{
bool ans;
ans = false;
if( (a.rear + 1) % LEN == a.front )
{
ans = true;
}
return ans;
}
void enterQueue( struct Queue &a, int data )
{
if( !isFull(a) )
{
a.num[a.rear] = data;
a.rear = ( a.rear + 1 ) % LEN;
}
else
{
cout << "Error:队列满了!禁止输入!"<< endl;
exit( -1 );
}
return;
}
int getQueue( struct Queue &a )
{
int ans;
if( isEmpty(a) )
{
ans = a.num [a.front ];
}
else
{
cout << "Error:队列空了" << endl;
exit( -1 );
}
return ans;
}
void delQueue( struct Queue &a )
{
if( !isEmpty( a ) )
{
a.front = ( a.front + 1 ) % LEN;
}
else
{
cout << "Error:队列空了!不要删了!"<< endl;
exit( -1 );
}
return;
}
int main()
{
struct Queue num;
int isEnd;
for( int i = 0; i < LEN; i++ )
{
num.num [i] = -1;
}
initQueue(num);
isEnd = 0;
while( isEnd != 7 )
{
cout << "要对队列进行哪些操作?" << endl;
cout << "1.重新初始化" << endl;
cout << "2.判空" << endl;
cout << "3.判满" << endl;
cout << "4.输入" << endl;
cout << "5.删除" << endl;
cout << "6.看看头" << endl;
cout << "7.结束操作" << endl;
cin >> isEnd;
switch( isEnd )
{
case 1:
initQueue(num);
break;
case 2:
if(isEmpty(num))
{
cout << "空的" << endl;
}
else
{
cout << "有的" << endl;
}
break;
case 3:
if(isFull(num))
{
cout << "满的" << endl;
}
else
{
cout << "有空的" << endl;
}
break;
case 4:
int data;
if( c == 0 )
{
num.front+=1;
c++;
}
cout << "cin a num(输入一个数)" << endl;
cin >> data;
enterQueue(num,data);
break;
case 5:
delQueue(num);
break;
case 6:
cout << "头是" << getQueue(num) << endl;
break;
case 7:
cout << "over!The end." << endl;
break;
}
}
return 0;
}
当前版本v1.1
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
#define LEN 100
struct Queue
{
int num[LEN];
int front;
int rear;
};
int c = 0;
void initQueue( struct Queue &a )
{
a.front = -1;
a.rear = 0;
return;
}
bool isEmpty( struct Queue &a )
{
bool ans;
ans = false;
if( a.front == a.rear )
{
ans = true;
}
return ans;
}
bool isFull( struct Queue &a )
{
bool ans;
ans = false;
if( (a.rear + 1) % LEN == a.front )
{
ans = true;
}
return ans;
}
void enterQueue( struct Queue &a, int data )
{
if( !isFull(a) )
{
a.num[a.rear] = data;
a.rear = ( a.rear + 1 ) % LEN;
}
else
{
cout << "Error:队列满了!禁止输入!"<< endl;
exit( -1 );
}
return;
}
int getQueue( struct Queue &a )
{
int ans;
if( isEmpty(a) )
{
ans = a.num [a.front ];
}
else
{
cout << "Error:队列空了" << endl;
exit( -1 );
}
return ans;
}
void delQueue( struct Queue &a )
{
if( !isEmpty( a ) )
{
a.front = ( a.front + 1 ) % LEN;
}
else
{
cout << "Error:队列空了!不要删了!"<< endl;
exit( -1 );
}
return;
}
int main()
{
struct Queue num;
int isEnd;
for( int i = 0; i < LEN; i++ )
{
num.num [i] = -1;
}
initQueue(num);
isEnd = 0;
while( isEnd != 7 )
{
cout << "要对队列进行哪些操作?" << endl;
cout << "1.重新初始化" << endl;
cout << "2.判空" << endl;
cout << "3.判满" << endl;
cout << "4.输入" << endl;
cout << "5.删除" << endl;
cout << "6.看看头" << endl;
cout << "7.结束操作" << endl;
cin >> isEnd;
switch( isEnd )
{
case 1:
initQueue(num);
break;
case 2:
if(isEmpty(num))
{
cout << "空的" << endl;
}
else
{
cout << "有的" << endl;
}
break;
case 3:
if(isFull(num))
{
cout << "满的" << endl;
}
else
{
cout << "有空的" << endl;
}
break;
case 4:
int data;
if( c == 0 )
{
num.front+=1;
c++;
}
cout << "cin a num(输入一个数)" << endl;
cin >> data;
enterQueue(num,data);
break;
case 5:
delQueue(num);
break;
case 6:
cout << "头是" << getQueue(num) << endl;
break;
case 7:
cout << "over!The end." << endl;
break;
}
}
return 0;
}
请复制代码至C++并使用 感谢各位的理解与支持