还是打算写一写吧
- #include<iostream>
- #include<malloc.h>
- #include<stdlib.h>
- using namespace std;
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- typedef int Status;
- typedef int QElemType;
- typedef struct QNode
- {
- QElemType data;
- struct QNode *next;
- }QNode,*QueuePtr;
- typedef struct
- {
- QueuePtr front;
- QueuePtr rear;
- }LinkQueue;
- Status InitQueue(LinkQueue &Q)
- {
- Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
- if(!Q.front) exit(OVERFLOW);
- Q.front->next=NULL;
- return OK;
- }
- Status EmptyQueue(LinkQueue &Q)
- {
- return Q.front==Q.rear?true:false; }
- Status EnQueue(LinkQueue &Q,QElemType e)
- {
- QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
- if(!p) exit(OVERFLOW);
- p->data=e;
- p->next=NULL;
- Q.rear->next=p;
- Q.rear=p;
- return OK;
- }
- Status DeQueue(LinkQueue &Q,QElemType &e)
- {
- if(EmptyQueue(Q)) return ERROR;
- QueuePtr p=Q.front->next;
- e=p->data;
- Q.front->next=p->next;
- if(Q.rear==p) Q.rear=Q.front;
- free(p);
- return OK; }
- Status DestroyQueue(LinkQueue &Q)
- {
- while(Q.front)
- {Q.rear=Q.front->next;
- free(Q.front);
- Q.front=Q.rear;}
- return OK;
- }
- Status ClearQueue(LinkQueue &Q)
- {
- Q.rear=Q.front;
- Q.front->next=NULL;
- return OK; }
- int QueueLength(LinkQueue Q)
- {
- int n=0;
- while(Q.front!=Q.rear)
- {
- n++;
- Q.front==Q.front->next; }
- return n;}
- Status GetHead(LinkQueue Q,QElemType &e)
- {
- if(EmptyQueue(Q)) return ERROR;
- e=Q.front->next->data;
- return OK; }
- void visit(QueuePtr p)
- {
- cout<<p->data<<endl;}
- Status QueueTraverse(LinkQueue Q,void visit(QueuePtr))
- {
- if(EmptyQueue(Q)) return ERROR;
- while(Q.front!=Q.rear)
- {visit(Q.front->next);
- Q.front=Q.front->next;}
- return OK;
- }
- int main()
- {
- LinkQueue Q;
- QElemType e;
- InitQueue(Q);
- for(int i=0;i<5;i++)
- {cin>>e;
- EnQueue(Q,e);}
- DeQueue(Q,e);
- QueueTraverse(Q,visit);
- DestroyQueue(Q);
- return 0;
- }
- 5 4 3 2 1
- 4
- 3
- 2
- 1
- 请按任意键继续. . .