【数据结构】队列

来源:互联网 发布:网页源码怎么修改 编辑:程序博客网 时间:2024/06/15 08:04
一、基础知识
        1.队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。
        2.队列是一种先进先出的线性表。
        3.与栈相同,队列也是一种重要的线性结构,实现一个队列同样需要顺序表链表作为基础。
        4.我们一般用顺序表来实现,而队列我们常用链表来实现,简称为链队列。
二、队列的链式存储结构
        将队头指针front指向链队列的头结点,队尾指针rear指向终端结点。

        空队列时,队头指针front和队尾指针rear都指向头结点

  1. typedef char ElemType;
  2. typedef struct Qnode//结点结构
  3. {
  4. ElemType data;
  5. struct Qnode *next;
  6. }Qnode;
  7. typedef struct Qnode * QueuePtr;    //指向结点结构的指针
  8. typedef struct //队列的链表结构
  9. {
  10. QueuePtr front;//队头指针
  11. QueuePtr rear;//队尾指针
  12. }LinkQueue;
三、创建一个队列
        Step.1  在内存中生成一个头结点。
        Step.2  将队列的头指针和尾指针指向这个生成的头结点。
  1. //创建一个队列
  2. void InitQueue(LinkQueue *q)
  3. {
  4. q->front = q->rear = (QueuePtr)malloc(sizeof(Qnode));
  5. if (!q->front)
  6. {
  7. exit(0);
  8. }
  9. q->front->next = NULL;
  10. }
四、入队列操作(尾部插入)
  1. //入队列操作
  2. void InsertQueue(LinkQueue *q, ElemType e)
  3. {
  4. QueuePtr p;
  5. p = (QueuePtr)malloc(sizeof(Qnode));
  6. if (!p)
  7. {
  8. exit(0);
  9. }
  10. p->data = e;
  11. p->next = NULL;
  12. q->rear->next = p;
  13. q->rear = p;
  14. }
五、出队列操作(头部删除)
  1. //出队列操作
  2. void DeleteQueue(LinkQueue *q, ElemType *e)
  3. {
  4. QueuePtr p;
  5. if (q->front = q->rear)//空队列
  6. {
  7. return;
  8. }
  9. p = q->front->next;
  10. *e = p->data;
  11. q->front->next = p->next;
  12. if (q->rear = p)            //原队列只有一个元素的情况,删除该节点,队列为空队列
  13. {
  14. q->rear = q->front;
  15. }
  16. free(p);
  17. }
六、销毁一个队列
        由于链队列建立在内存的动态区,因此当一个队列不再有用时,应该及时将他销毁,以免过多的占用内存空间。
  1. //销毁一个队列
  2. void DestoryQueue(LinkQueue *q)
  3. {
  4. while (q->front)
  5. {
  6. q->rear = q->front->next;        //从头向后删
  7. free(q->front);
  8. q->front = q->rear;
  9. }
  10. }
七、求队列的长度
  1. //求队列长度
  2. int LenQueue(LinkQueue *q)
  3. {
  4. QueuePtr t = q->front->next;
  5. int len = 0;
  6. while (t)
  7. {
  8. t = t->next;
  9. len++;
  10. }
  11. return len;
  12. }
八、判断队列是否为空
  1. //判断队列是否为空
  2. bool EmptyQueue(LinkQueue *q)
  3. {
  4. if (q->front == q->rear)
  5. return true;
  6. else
  7. return false;
  8. }
九、输出队列元素
  1. //输出队列元素
  2. void DisQueue(LinkQueue *q)
  3. {
  4. QueuePtr p = q->front->next;
  5. printf("此时的链队列输出:\n");
  6. while (p)
  7. {
  8. printf("%c", p->data);
  9. p = p->next;
  10. }
  11. printf("\n");
  12. }
十、获取队首、队尾元素
  1. //获取队首元素
  2. ElemType QueueFront(LinkQueue *q)
  3. {
  4. return q->front->data;
  5. }
  6. //获取队尾元素
  7. ElemType QueueRear(LinkQueue *q)
  8. {
  9. return q->rear->data;
  10. }
十一、创建一个长度由自己决定的队列并初始化队列
  1. //创建一个长度由自己确定的队列并初始化
  2. void CreateQueue(LinkQueue *q)
  3. {
  4. ElemType d;
  5. int len, i;
  6. QueuePtr p;
  7. printf("请输入队列的长度:");
  8. scanf("%d", &len);
  9. for (i = 0; i < len; i++)
  10. {
  11. p = (QueuePtr)malloc(sizeof(Qnode));
  12. if (!p)
  13. {
  14. exit(0);
  15. }
  16. scanf("%c", &d);
  17. p->data = d;
  18. p->next = NULL;
  19. q->rear->next = p;
  20. q->rear = p;
  21. }
  22. }
原创粉丝点击