第七周醒目4队列数组
来源:互联网 发布:淘宝包包原单店铺推荐 编辑:程序博客网 时间:2024/04/29 23:06
- #include <stdio.h>
- #include <malloc.h>
- #include "liqueue.h"
- #define N 10
-
- int main()
- {
- int i, a;
- LiQueue *qu[N];
- for (i=0; i<N; i++)
- InitQueue(qu[i]);
-
-
- printf("输入若干正整数,以0结束: ");
- scanf("%d", &a);
- while(a)
- {
- enQueue(qu[a%10], a);
- scanf("%d", &a);
- }
-
-
- printf("按个位数整理到各个队列中后,各队列出队的结果是: \n");
- for (i=0; i<N; i++)
- {
- printf("qu[%d]: ", i);
- while(!QueueEmpty(qu[i]))
- {
- deQueue(qu[i], a);
- printf("%d ", a);
- }
- printf("\n");
- }
-
-
- for (i=0; i<N; i++)
- DestroyQueue(qu[i]);
- return 0;
- }
- #ifndef LIQUEUE_H_INCLUDED
- #define LIQUEUE_H_INCLUDED
-
- typedef int ElemType;
- typedef struct qnode
- {
- ElemType data;
- struct qnode *next;
- } QNode;
-
- typedef struct
- {
- QNode *front;
- QNode *rear;
- } LiQueue;
- void InitQueue(LiQueue *&q);
- void DestroyQueue(LiQueue *&q);
- bool QueueEmpty(LiQueue *q);
- int QueueLength(LiQueue *q);
- void enQueue(LiQueue *&q,ElemType e);
- bool deQueue(LiQueue *&q,ElemType &e);
-
- #endif // LIQUEUE_H_INCLUDED
- #include <stdio.h>
- #include <malloc.h>
- #include "liqueue.h"
-
- void InitQueue(LiQueue *&q)
- {
- q=(LiQueue *)malloc(sizeof(LiQueue));
- q->front=q->rear=NULL;
- }
- void DestroyQueue(LiQueue *&q)
- {
- QNode *p=q->front,*r;
- if (p!=NULL)
- {
- r=p->next;
- while (r!=NULL)
- {
- free(p);
- p=r;
- r=p->next;
- }
- }
- free(p);
- free(q);
- }
- bool QueueEmpty(LiQueue *q)
- {
- return(q->rear==NULL);
- }
- int QueueLength(LiQueue *q)
- {
- int n=0;
- QNode *p=q->front;
- while (p!=NULL)
- {
- n++;
- p=p->next;
- }
- return(n);
- }
- void enQueue(LiQueue *&q,ElemType e)
- {
- QNode *p;
- p=(QNode *)malloc(sizeof(QNode));
- p->data=e;
- p->next=NULL;
- if (q->rear==NULL)
- q->front=q->rear=p;
- else
- {
- q->rear->next=p;
- q->rear=p;
- }
- }
- bool deQueue(LiQueue *&q,ElemType &e)
- {
- QNode *t;
- if (q->rear==NULL)
- return false;
- t=q->front;
- if (q->front==q->rear)
- q->front=q->rear=NULL;
- else
- q->front=q->front->next;
- e=t->data;
- free(t);
- return true;
- }
运行结果: