|
发表于 2014-7-10 15:11:43
|
显示全部楼层
本帖最后由 小泉向西流 于 2014-7-10 15:15 编辑
队列的出入输出作业:
- #include <stdio.h>
- #include <stdlib.h>
- typedef char ElemType;
- typedef struct QNode
- {
- ElemType data;
- struct QNode *next;
- }QNode, *QueuePrt;
- typedef struct
- {
- QueuePrt front, rear;
- }LinkQuene;
- void InitQuene(LinkQuene *q)
- {
- q->front=q->rear=(QueuePrt)malloc(sizeof(QNode));
- if(!q->front)
- exit(0);
- q->front->next=NULL;
- }
- void InsertQueue(LinkQuene *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;
- }
- void DeleteQuene(LinkQuene *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);
- }
- void DestroyQuene(LinkQuene *q) //销毁队列
- {
- while(q->front)
- {
- q->rear=q->front->next;
- free(q->front);
- q->front=q->rear;
- }
- q->front=NULL;
- }
- void GetHead(LinkQuene *q) //获取队列的第一个元素,并输出
- {
- QueuePrt p=q->front->next;
- printf("%c\n", p->data);
- }
- void PrintQuene(LinkQuene *q) //队列输出
- {
- QueuePrt p;
- p=q->front->next;
- while(p)
- {
- printf("%c", p->data);
- p=p->next;
- }
- }
- int main()
- {
- QNode q;
- char c,e;
- InitQuene(&q);
- printf("请输入一串字符,以#表示结束:\n");
- scanf("%c", &c);
- while(c!='#')
- {
- InsertQueue(&q, c);
- scanf("%c", &c);
- }
- getchar();
- printf("队列的第一个字符为: ");
- GetHead(&q);
- printf("\n");
- printf("队列输出: ");
- PrintQuene(&q);
- printf("\n\n");
- DeleteQuene(&q,&e);
- printf("删除的元素为:%c\n",e);
- printf("新的队列输出:");
- PrintQuene(&q);
- printf("\n\n");
- return 0;
- }
复制代码
|
|