|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- #include<stdlib.h>
- typedef char ElemType;
- typedef struct QNode
- {
- ElemType data;
- struct QNode *next;
- }QNode,*QueuePrt;
- typedef struct{
- QueuePrt front,rear;
- }LinkQueue;
- initQueue(LinkQueue *q)
- {
- q->front=q->rear=(QueuePrt)malloc(sizeof(QNode));
- if(!q->front)
- exit(0);
- q->front->next=NULL;
- }
- InsertQueue(LinkQueue *q,ElemType e)
- {
- QueuePrt p;
- p=(QueuePrt)malloc(sizeof(QNode));
- if(p==NULL)
- exit(0);
- p->data=e;
- p->next=NULL;
- q->rear->next=p;
- q->rear=p;
- }
- DeleteQueue(LinkQueue *q, ElemType *e)
- {
- QueuePrt p;
- if(q->front==q->rear)
- return;
- p=q->front->next;
- *e=p->data;
- q->front->next=p->next;
- if(q->rear==p)
- q->rear=q->front;
- free(p);
- }
- DestroyQueue(LinkQueue *q)
- {
- while(q->front)
- {
- q->rear=q->front->next;
- free(q->front);
- q->front=q->rear;
- }
- }
- int main()
- {
-
- QueuePrt p;
- LinkQueue q;
- ElemType e;
- initQueue(&q);
-
-
- printf("请输入字符串,以#作为结束:\n");
- scanf("%c",&e);
- while(e!='#')
- {
- InsertQueue(&q,e);
- scanf("%c",&e);
- }
- p=q.front->next;
- while(p)
- {
- printf("%c",p->data);
- p=p->next;
- }
- }
复制代码
main函数里ElemType e;为什么这句一定要在initQueue(&q);前面,否则就会报错。
|
|