|
发表于 2018-2-2 21:47:37
|
显示全部楼层
#include <iostream>
using namespace std;
typedef char ElemType;
typedef int Status;
typedef struct Qnode{
ElemType data;
struct Qnode *Next;
}*Queue;
typedef struct{
Queue front;
Queue rear;
}LinkQueue;
Status CreatQueue(LinkQueue *q){
q->front=new Qnode;
q->rear=new Qnode;
q->front=q->rear;
if(!q->front){
cout<<"队列创建不成功";
}
q->front->Next=NULL;
}
void InsertQueue(LinkQueue *q,ElemType e){
Queue p;
p=new Qnode;
if(p==NULL){
cout<<"p结点开辟空间失败"<<endl;
}
p->data=e;
p->Next=NULL;
q->rear->Next=p;
q->rear=p;
}
void DeleteQueue(LinkQueue *q,ElemType *e){
Queue p;
if(q->front==q->rear){
cout<<"出错:此时队列为空"<<endl;
}
p=q->front->Next;
*e=p->data;
q->front->Next=p->Next;
if(q->rear==p){
q->rear=q->front;
}
delete(p);
}
void DestroyQueue(LinkQueue *q){
while(q->front){
q->rear=q->front->Next;
free(q->front);
q->front=q->rear;
}
}
Status QueueLen(LinkQueue q){
return (q.rear-q.front);
}
int main() {
LinkQueue q;
CreatQueue(&q);
ElemType c;
cout<<"输入想要输入的数字,以#号结束"<<endl;
cin>>c;
while('#'!=c){
InsertQueue(&q,c);
cin>>c;
}
getchar();
for(int i=1;i<QueueLen(q);i++) {
DeleteQueue(&q, &c);
cout << c << " ";
}
return 0;
}
在此附上本人所写的C++代码 |
|