【数据结构】队列

一、基础知识
        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. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值