顺序队列 操作
2009-12-25 23:35
387 查看
#include <iostream>
using namespace std;
typedef int ElemType ;
#define MAXSIZE 100
#define FALSE 0
#define TRUE 1
typedef struct
{ElemType data[MAXSIZE];
int front,rear;
}SeqQueue;
//初始化
SeqQueue SeqQueueInit()
{SeqQueue Q;
Q.front=Q.rear=-1;
return Q;
}
//入队
SeqQueue SeqQueueIn(SeqQueue Q,ElemType x)
{if((Q.rear+1)%MAXSIZE==Q.front)
{cout<<"队满!/n";
exit(0);
}
Q.rear=(Q.rear+1)%MAXSIZE;
Q.data[Q.rear]=x;
return Q;
}
//出队
SeqQueue SeqQueueOut(SeqQueue Q)
{if(Q.front==Q.rear)
{cout<<"队空!/n";
}
else {Q.front=(Q.front+1)%MAXSIZE;
ElemType x=Q.data[Q.front];
cout<<"出队元素为:"<<x<<endl;
}
return Q;
}
//取队头
ElemType SeqQueueGetHead(SeqQueue Q)
{return (Q.data[Q.front+1]);
}
//判队空
int SeqQueueEmpty (SeqQueue Q)
{if(Q.front==Q.rear)
return TRUE;
else return FALSE;
}
//球队列中的元素个数
int SeqQueueLength(SeqQueue Q)
{return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}
//遍历
void SeqQueueTraverse(SeqQueue Q)
{int i;
if(SeqQueueEmpty(Q))
cout<<"队列为空!/n";
else {cout<<"队列中的元素为:/n";
for(i=Q.front+1;i<SeqQueueLength(Q);i++)
cout<<Q.data[i]<<" ";
}
cout<<"/n";
}
int scan()
{int d;
cout<<"请输入所要进行的操作/n";
cout<<"(1 初始化),(2 入队),(3 出队),(4 取队头元素),(5 判队空),(6元素个数),(7 遍历)/n";
cout<<"其他键 推出........./n";
cin>>d;
return (d);
}
int main()
{int can=0;
SeqQueue Q;
ElemType x;
while(!can)
switch(scan())
{case 1:Q=SeqQueueInit();break;
case 2:cout<<"请输出入队元素/n:";
cin>>x;
Q=SeqQueueIn(Q,x);
break;
case 3:Q=SeqQueueOut(Q);
break;
case 4:if(Q.front==Q.rear)
cout<<"队空!"<<endl;
else cout<<"队头元素为:"<<SeqQueueGetHead(Q)<<endl;
break;
case 5:if(SeqQueueEmpty(Q))
cout<<"队空!/n";
else cout<<"队不空!/n";
break;
case 6:cout<<"对中元素个数为:";
cout<<SeqQueueLength(Q)<<endl;;
break;
case 7:SeqQueueTraverse(Q);
break;
default:can=1;
}
return 0;
}
相关文章推荐
- 链表,顺序表的基本操作及其实现的栈与队列
- 顺序队列基本操作
- 实验三:栈和队列的基本操作实现及其应用——顺序栈
- 顺序环形队列的操作实现
- 《数据结构》严蔚敏版(java解)——第三章 栈和队列03 顺序队列操作
- 顺序队列的基本操作
- 顺序队列的基本操作
- 循环队列的相关操作(顺序结构)
- 实验3:栈和队列的基本操作实现及其应用——顺序队列和链队列
- 顺序队列的基本操作;
- 顺序循环队列一些操作
- 栈和队列的基本操作(分顺序和链式,5种基本操作有创建,判空,判满,入,出)
- 数据结构--顺序队列基本操作
- 顺序队列的操作
- 顺序队列和链队列的各种操作
- 队列的相关操作_顺序存储
- 顺序队列基本操作
- 简单顺序队列操作(不考虑循环)
- 顺序队列基本操作
- 顺序队列基本操作