Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1064463
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: C/C++

2015-12-02 14:49:41


点击(此处)折叠或打开

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>

  4. #define STRINGSIZE 128

  5. typedef char STRING[STRINGSIZE];

  6. /*队列节点的定义*/
  7. typedef struct queuenode
  8. {
  9.     STRING string;
  10.     struct queuenode * next;
  11. }QUEUENODE;

  12. typedef QUEUENODE * QueueNodePtr;

  13. typedef struct linkqueue
  14. {
  15.     QueueNodePtr front; /*队头指针:始终指向链表头节点*/
  16.     QueueNodePtr rear; /*队尾指针*/
  17. }LINKQUEUE;

  18. int InitQueue(LINKQUEUE * linkqueue);
  19. int QueueIsEmpty(LINKQUEUE linkqueue);
  20. int QueueLen(LINKQUEUE linkqueue);
  21. int CreateQueueNode(QueueNodePtr * queuenodeptr, STRING string);
  22. int GetQueueHead(LINKQUEUE linkqueue, STRING string);
  23. int PrintQueue(LINKQUEUE linkqueue);
  24. int EnQueue(LINKQUEUE * linkqueue, STRING string);
  25. int DeQueue(LINKQUEUE * linkqueue, STRING string);
  26. int DestroyQueue(LINKQUEUE * linkqueue);

  27. int main(int argc, char ** argv)
  28. {
  29.     printf("队列的链式存储结构!\n");
  30.     LINKQUEUE linkqueue;
  31.     InitQueue(&linkqueue);
  32.     int flag = -1;
  33.     flag = QueueIsEmpty(linkqueue);
  34.     if(flag == 1)
  35.         printf("是空队列!\n");
  36.     else if(flag == 2)
  37.         printf("是非空队列!\n");
  38.         
  39.     PrintQueue(linkqueue);
  40.     
  41.     EnQueue(&linkqueue, "AAA");
  42.     
  43.     flag = QueueIsEmpty(linkqueue);
  44.     if(flag == 1)
  45.         printf("是空队列!\n");
  46.     else if(flag == 2)
  47.         printf("是非空队列!\n");
  48.         
  49.     EnQueue(&linkqueue, "BBB");
  50.     EnQueue(&linkqueue, "CCC");
  51.     EnQueue(&linkqueue, "DDD");
  52.     EnQueue(&linkqueue, "EEE");
  53.     EnQueue(&linkqueue, "FFF");
  54.     EnQueue(&linkqueue, "GGG");
  55.     
  56.     PrintQueue(linkqueue);
  57.     
  58.     STRING string;
  59.     memset(string, 0, sizeof(STRING));
  60.     DeQueue(&linkqueue, string);
  61.     printf("出列元素1=[%s]\n", string);
  62.     memset(string, 0, sizeof(STRING));
  63.     DeQueue(&linkqueue, string);
  64.     printf("出列元素2=[%s]\n", string);
  65.     
  66.     memset(string, 0, sizeof(STRING));
  67.     GetQueueHead(linkqueue, string);
  68.     printf("取得的对头元素=[%s]\n", string);
  69.     
  70.     PrintQueue(linkqueue);
  71.     
  72.     memset(string, 0, sizeof(STRING));
  73.     DeQueue(&linkqueue, string);
  74.     memset(string, 0, sizeof(STRING));
  75.     DeQueue(&linkqueue, string);
  76.     memset(string, 0, sizeof(STRING));
  77.     DeQueue(&linkqueue, string);
  78.     memset(string, 0, sizeof(STRING));
  79.     DeQueue(&linkqueue, string);
  80.     memset(string, 0, sizeof(STRING));
  81.     DeQueue(&linkqueue, string);
  82.     memset(string, 0, sizeof(STRING));
  83.     DeQueue(&linkqueue, string);
  84.     memset(string, 0, sizeof(STRING));
  85.     DeQueue(&linkqueue, string);
  86.     /*DestroyQueue(&linkqueue);*/
  87.     PrintQueue(linkqueue);
  88.     
  89.     return 0;
  90. }

  91. /*功能:判断链式队列是否为空,1:空, 2:非空*/
  92. int QueueIsEmpty(LINKQUEUE linkqueue)
  93. {
  94.     if(linkqueue.front == linkqueue.rear)
  95.         return 1;
  96.     else
  97.         return 2;
  98. }

  99. /*功能:初始化链队列:创建队列空头节点,front队头指针始终指向他*/
  100. int InitQueue(LINKQUEUE * linkqueue)
  101. {
  102.     linkqueue->front = (QUEUENODE*)malloc(sizeof(QUEUENODE));
  103.     if(linkqueue->front == NULL)
  104.     {
  105.         printf("InitQueue malloc err!\n");
  106.         exit(-1);
  107.     }
  108.     memset(linkqueue->front->string, 0, sizeof(STRING));
  109.     linkqueue->front->next = NULL; /*front指针指向空头节点,front指针的下一个节点才是队头元素*/
  110.     linkqueue->rear = linkqueue->front;
  111.     
  112.     return 0;
  113. }

  114. /*功能:创建一个队列节点元素*/
  115. int CreateQueueNode(QueueNodePtr * queuenodeptr, STRING string)
  116. {
  117.     *queuenodeptr = (QueueNodePtr)malloc(sizeof(QUEUENODE));
  118.     if(*queuenodeptr == NULL)
  119.     {
  120.         printf("CreateQueueNode malloc err!\n");
  121.         exit(-1);
  122.     }
  123.     strcpy((*queuenodeptr)->string, string);
  124.     
  125.     return 0;
  126. }

  127. /*功能:求队列中元素的个数*/
  128. int QueueLen(LINKQUEUE linkqueue)
  129. {
  130.     int len = 0;
  131.     QueueNodePtr p = NULL;
  132.     p = linkqueue.front;
  133.     if(p == NULL)
  134.     {
  135.         //printf("链式队列不存在,或者已经销毁!\n");
  136.         return len;
  137.     }
  138.     while(p->next != NULL)
  139.     {
  140.         p = p->next;
  141.         len++;
  142.     }
  143.     
  144.     return len;
  145. }

  146. /*功能:打印队列中的所有元素*/
  147. int PrintQueue(LINKQUEUE linkqueue)
  148. {
  149.     
  150.     STRING string;
  151.     int i = 0;
  152.     int flag = -1;
  153.     
  154.     if(QueueIsEmpty(linkqueue) == 1)
  155.     {
  156.         printf("是空队列,无需打印!\n");
  157.         return 1;
  158.     }
  159.     printf("队列中元素个数=[%d]\n", QueueLen(linkqueue));
  160.     
  161.     QueueNodePtr p = NULL;
  162.     p = linkqueue.front;
  163.     printf("从队列头开始:\n");
  164.     while(p->next != NULL)
  165.     {
  166.         p = p->next;
  167.         memset(string, 0, sizeof(STRING));
  168.         strcpy(string, p->string);
  169.         i++;
  170.         printf("(第%d个元素=[%s])\n", i, string);
  171.     }
  172.     
  173.     return 0;
  174. }

  175. /*功能:入队:向队列尾插入一个元素*/
  176. int EnQueue(LINKQUEUE * linkqueue, STRING string)
  177. {
  178.     QueueNodePtr queuenodeptr = NULL;
  179.     CreateQueueNode(&queuenodeptr, string); /*创建一个队列节点元素*/
  180.     
  181.     linkqueue->rear->next = queuenodeptr; /*连上新的节点*/
  182.     queuenodeptr->next = NULL;
  183.     linkqueue->rear = queuenodeptr; /*队尾指针指向新加入的节点*/
  184.     printf("入队一个元素=[%s], 链队中元素个数=[%d]\n", linkqueue->rear->string, QueueLen(*linkqueue));
  185.     
  186.     return 0;
  187. }

  188. /*功能:出队:从队头删除一个元素*/
  189. int DeQueue(LINKQUEUE * linkqueue, STRING string)
  190. {
  191.     if(QueueIsEmpty(*linkqueue) == 1)
  192.     {
  193.         printf("队列为空,无元素可出队!\n");
  194.         return -1;
  195.     }
  196.     QueueNodePtr tempnodeptr = linkqueue->front->next;
  197.     if(linkqueue->front->next == linkqueue->rear) /*如果只有一个队尾元素了,则将队尾指针指向头节点*/
  198.     {
  199.         linkqueue->rear = linkqueue->front;
  200.     }
  201.     linkqueue->front->next = linkqueue->front->next->next; /*空头节点后的第一个元素就是队头元素:将队头元素出列*/

  202.     strcpy(string, tempnodeptr->string);
  203.     printf("出队一个元素=[%s], 链队中剩余元素个数=[%d]\n", tempnodeptr->string, QueueLen(*linkqueue));
  204.     free(tempnodeptr); /*删除的节点要释放*/
  205.     tempnodeptr == NULL;
  206.     
  207.     return 0;
  208. }

  209. /*功能:取得队列头元素*/
  210. int GetQueueHead(LINKQUEUE linkqueue, STRING string)
  211. {
  212.     if(QueueIsEmpty(linkqueue) == 1);
  213.     {
  214.         printf("队列为空,无元素可取!\n");
  215.         return -1;
  216.     }
  217.     
  218.     strcpy(string, linkqueue.front->next->string);
  219.     
  220.     return 0;
  221. }

  222. /*功能:销毁队列*/
  223. int DestroyQueue(LINKQUEUE * linkqueue)
  224. {
  225.     QueueNodePtr p = NULL;
  226.     QueueNodePtr q = NULL;
  227.     p = linkqueue->front;
  228.     q = linkqueue->front;
  229.     if(p == NULL)
  230.     {
  231.         //printf("链式队列不存在,或者已经销毁!\n");
  232.         return 0;
  233.     }
  234.     while(q->next != NULL)
  235.     {
  236.         p = q->next;
  237.         free(q);
  238.         q = p;
  239.     }
  240.     free(p);
  241.     p = NULL;
  242.     q = NULL;
  243.     linkqueue->front = NULL;
  244.     linkqueue->rear = NULL;
  245.     
  246.     printf("链式队列已销毁!\n");
  247.     
  248.     return 0;
  249. }

阅读(456) | 评论(0) | 转发(0) |
0

上一篇:实现string.c

下一篇:顺序队列

给主人留下些什么吧!~~