• c语言编程之循环队列


            利用链表实现的循环队列,完成了队列的入队和出队,对于队空和队满用了一个flag进行标记。入队flag++,出队flag--

      1 #include"stdio.h"
      2 
      3 typedef int element;
      4 
      5 typedef struct Node{
      6         struct Node *next;
      7         element data;
      8 }*pNode;
      9 
     10 typedef struct QNode{
     11         pNode front,rear;
     12         element flag;
     13 }*Linknode;
     14 
     15 //init a empty queue
     16 element Init_queue(Linknode *pLinknode)
     17         {
     18            Linknode P;
     19            P=*pLinknode;
     20            P->front=P->rear=(pNode)malloc(sizeof(struct Node));
     21            P->front->next=NULL;
     22            P->rear->next=P->front;     // make loop queue font linked rear,in usuall queue this is P->rear->next=NULL;make a change become 
    //Loop queue;
    23 P->flag=0; 24 } 25 26 //add a data to queue rear 27 element Add_queue(Linknode *pLinknode,int num) 28 { 29 Linknode P; 30 P=*pLinknode; 31 pNode s=(pNode)malloc(sizeof(struct Node)); 32 s->data=num; 33 s->next=P->front; //in this position we can see the new add memory s is point to front,in usuall queue we can see s->next=NULL;
     34           P->rear->next=s;
     35           P->rear=s;                   
     36           ++P->flag;
     37           printf("add data:%d add flag:%d
    ",P->rear->data,P->flag);
     38         }
     42         {
     43           if((*pLinknode)->flag==0)
     44                 {
     45                   printf("queue is empty
    ");
     46                   return 0;
     47                 }
     48           //if(((*pLinknode)->front)==((*pLinknode)->rear))
     49           //    {
     50           //            printf("queue is empty!!");
     51           //            return 0;
     52           //      }
     53           Linknode P;
     54           pNode k;
     55           P=*pLinknode;
     56           k=P->front->next;
     57           P->front->next=k->next;
     58           printf("delet data:%d
    ",k->data);
     59           //--P->flag;
     60           free(k);
     61           printf("delete flag:%d
    ",P->flag);
     62           --P->flag;
     63           return 0;
     64         }
     65 
     66 element Print_queue(Linknode pLinknode)
     67         {
     68           pNode p;
     69           p=pLinknode->front;
     71           while(p!=pLinknode->rear&&pLinknode->flag)    //it is importand,because in loop queue fornt==rear can means empty or full,it is a                                                  //loop,we can't determin this loop queue is empty by using front==rear,must add num as flag;
     72                 {
     73                   p=p->next;
     74                   printf("data:%d
    ",p->data);
     75                 }
     76           return 0;
     77         }
     78 element main()
     79         {
     80           Linknode pQnode;
     81           pQnode=(Linknode)malloc(sizeof(struct QNode));
     82           Init_queue(&pQnode);
     83           Add_queue(&pQnode,99);
     84           Add_queue(&pQnode,11);
     85           Add_queue(&pQnode,22);
     86           Add_queue(&pQnode,33);
     87           Add_queue(&pQnode,44);
     88           Add_queue(&pQnode,55);
     89           Print_queue(pQnode);
     90           Delet_queue(&pQnode);
     91           Delet_queue(&pQnode);
     92           Delet_queue(&pQnode);
     93           Delet_queue(&pQnode);
     94           Delet_queue(&pQnode);
     95           Delet_queue(&pQnode);
     96           Delet_queue(&pQnode);
     97           Print_queue(pQnode);
     98           return 0;
     99         }
    100 
  • 相关阅读:
    数字签名与HTTPS详解
    利用策略模式优化过多 if else 代码
    Redis 的事务到底是不是原子性的
    Spring Boot项目的接口防刷
    深入分析 ThreadLocal
    什么是四层和七层负载均衡?他们之间的区别是什么?
    MyEclipse或Eclipse中project的导入和导出
    org.hibernate.exception.ConstraintViolationException: could not insert:
    C++ STL vector(向量容器)的使用(附完整程序代码)
    Swift2.0语言教程之函数嵌套调用形式
  • 原文地址:https://www.cnblogs.com/qiuheng/p/5783996.html
Copyright © 2020-2023  润新知