zoukankan      html  css  js  c++  java
  • 链队列

    来源:http://blog.csdn.net/hopeyouknow/article/details/6736987

    队列也是常用的数据结构之一,下面给出一个链式队列的实现~~

    头文件Queue.h

    1. #ifndef Queue_H  
    2. #define Queue_H  
    3.   
    4. typedef int Item;  
    5. typedef struct node * PNode;  
    6. typedef struct node  
    7. {  
    8.     Item data;  
    9.     PNode next;  
    10. }Node;  
    11.   
    12. typedef struct  
    13. {  
    14.     PNode front;  
    15.     PNode rear;  
    16.     int size;  
    17. }Queue;  
    18.   
    19. /*构造一个空队列*/  
    20. Queue *InitQueue();  
    21.   
    22. /*销毁一个队列*/  
    23. void DestroyQueue(Queue *pqueue);  
    24.   
    25. /*清空一个队列*/  
    26. void ClearQueue(Queue *pqueue);  
    27.   
    28. /*判断队列是否为空*/  
    29. int IsEmpty(Queue *pqueue);  
    30.   
    31. /*返回队列大小*/  
    32. int GetSize(Queue *pqueue);  
    33.   
    34. /*返回队头元素*/  
    35. PNode GetFront(Queue *pqueue,Item *pitem);  
    36.   
    37. /*返回队尾元素*/  
    38. PNode GetRear(Queue *pqueue,Item *pitem);  
    39.   
    40. /*将新元素入队*/  
    41. PNode EnQueue(Queue *pqueue,Item item);  
    42.   
    43. /*队头元素出队*/  
    44. PNode DeQueue(Queue *pqueue,Item *pitem);  
    45.   
    46. /*遍历队列并对各数据项调用visit函数*/  
    47. void QueueTraverse(Queue *pqueue,void (*visit)());  
    48.   
    49. #endif  


    实现代码Queue.c如下:

    1. #include"Queue.h"  
    2. #include<malloc.h>  
    3. #include<stdio.h>  
    4.   
    5. /*构造一个空队列*/  
    6. Queue *InitQueue()  
    7. {  
    8.     Queue *pqueue = (Queue *)malloc(sizeof(Queue));  
    9.     if(pqueue!=NULL)  
    10.     {  
    11.         pqueue->front = NULL;  
    12.         pqueue->rear = NULL;  
    13.         pqueue->size = 0;  
    14.     }  
    15.     return pqueue;  
    16. }  
    17.   
    18. /*销毁一个队列*/  
    19. void DestroyQueue(Queue *pqueue)  
    20. {  
    21.     if(IsEmpty(pqueue)!=1)  
    22.         ClearQueue(pqueue);  
    23.     free(pqueue);  
    24. }  
    25.   
    26. /*清空一个队列*/  
    27. void ClearQueue(Queue *pqueue)  
    28. {  
    29.     while(IsEmpty(pqueue)!=1)  
    30.     {  
    31.         DeQueue(pqueue,NULL);  
    32.     }  
    33.   
    34. }  
    35.   
    36. /*判断队列是否为空*/  
    37. int IsEmpty(Queue *pqueue)  
    38. {  
    39.     if(pqueue->front==NULL&&pqueue->rear==NULL&&pqueue->size==0)  
    40.         return 1;  
    41.     else  
    42.         return 0;  
    43. }  
    44.   
    45. /*返回队列大小*/  
    46. int GetSize(Queue *pqueue)  
    47. {  
    48.     return pqueue->size;  
    49. }  
    50.   
    51. /*返回队头元素*/  
    52. PNode GetFront(Queue *pqueue,Item *pitem)  
    53. {  
    54.     if(IsEmpty(pqueue)!=1&&pitem!=NULL)  
    55.     {  
    56.         *pitem = pqueue->front->data;  
    57.     }  
    58.     return pqueue->front;  
    59. }  
    60.   
    61. /*返回队尾元素*/  
    62.   
    63. PNode GetRear(Queue *pqueue,Item *pitem)  
    64. {  
    65.     if(IsEmpty(pqueue)!=1&&pitem!=NULL)  
    66.     {  
    67.         *pitem = pqueue->rear->data;  
    68.     }  
    69.     return pqueue->rear;  
    70. }  
    71.   
    72. /*将新元素入队*/  
    73. PNode EnQueue(Queue *pqueue,Item item)  
    74. {  
    75.     PNode pnode = (PNode)malloc(sizeof(Node));  
    76.     if(pnode != NULL)  
    77.     {  
    78.         pnode->data = item;  
    79.         pnode->next = NULL;  
    80.           
    81.         if(IsEmpty(pqueue))  
    82.         {  
    83.             pqueue->front = pnode;  
    84.         }  
    85.         else  
    86.         {  
    87.             pqueue->rear->next = pnode;  
    88.         }  
    89.         pqueue->rear = pnode;  
    90.         pqueue->size++;  
    91.     }  
    92.     return pnode;  
    93. }  
    94.   
    95. /*队头元素出队*/  
    96. PNode DeQueue(Queue *pqueue,Item *pitem)  
    97. {  
    98.     PNode pnode = pqueue->front;  
    99.     if(IsEmpty(pqueue)!=1&&pnode!=NULL)  
    100.     {  
    101.         if(pitem!=NULL)  
    102.             *pitem = pnode->data;  
    103.         pqueue->size--;  
    104.         pqueue->front = pnode->next;  
    105.         free(pnode);  
    106.         if(pqueue->size==0)  
    107.             pqueue->rear = NULL;  
    108.     }  
    109.     return pqueue->front;  
    110. }  
    111.   
    112. /*遍历队列并对各数据项调用visit函数*/  
    113. void QueueTraverse(Queue *pqueue,void (*visit)())  
    114. {  
    115.     PNode pnode = pqueue->front;  
    116.     int i = pqueue->size;  
    117.     while(i--)  
    118.     {  
    119.         visit(pnode->data);  
    120.         pnode = pnode->next;  
    121.     }  
    122.           
    123. }  


    简单测试程序Test.c

    1. #include"Queue.h"  
    2. #include<stdio.h>  
    3. void print(Item i)  
    4. {  
    5.     printf("该节点元素为%d ",i);  
    6. }  
    7. main()  
    8. {  
    9.     Queue *pq = InitQueue();  
    10.     int i,item;  
    11.     printf("0-9依次入队并输出如下: ");  
    12.     for(i=0;i<10;i++)  
    13.     {  
    14.         EnQueue(pq,i);  
    15.         GetRear(pq,&item);  
    16.         printf("%d ",item);  
    17.     }  
    18.   
    19.     printf(" 从队头到队尾遍历并对每个元素执行print函数: ");   
    20.     QueueTraverse(pq,print);  
    21.   
    22.     printf("队列中元素依次出队列并输出如下: ");  
    23.     for(i=0;i<10;i++)  
    24.     {  
    25.         DeQueue(pq,&item);  
    26.         printf("%d ",item);  
    27.     }  
    28.     ClearQueue(pq);  
    29.     if(IsEmpty(pq))  
    30.         printf(" 将队列置空成功 ");  
    31.     DestroyQueue(pq);  
    32.     printf("队列已被销毁 ");  
    33. }  

    方法二:

      1 /*******************************************************************************
      2 /* <PRE>
      3 /* 版权所有    : -
      4 /* 模块名      : 栈和队列
      5 /* 文件名      : lqueue.cpp
      6 /* 功能描述    : 链队列的表示与实现 
      7 /* 作者        : <xxx>
      8 /* 版本        : 1.0
      9 /* -----------------------------------------------------------------------------
     10 /* 备注        : -
     11 /* -----------------------------------------------------------------------------
     12 /* 修改记录    :
     13 /* 日 期        版本     修改人        修改内容
     14 /* 2011/01/01   1.0      <xxx>         创建
     15 /* </PRE>
     16 *******************************************************************************/
     17 #include <stdio.h>
     18 #include <stdlib.h>
     19 
     20 /******************************************************************************
     21 /* 数据类型和常量定义
     22 /******************************************************************************/
     23 #define OK           1
     24 #define ERROR        0
     25 #define OVERFLOW    -2
     26 
     27 typedef int Status;
     28 typedef int QElemType;
     29 
     30 
     31 /******************************************************************************
     32 /* 数据结构声明
     33 /******************************************************************************/
     34 /* 单链队列 - 队列的链式存储结构 */
     35 typedef struct QNode{
     36     QElemType data;
     37     struct QNode *next;
     38 } QNode, *QueuePtr;
     39 
     40 typedef struct {
     41     QueuePtr front; /* 队头指针 */
     42     QueuePtr rear;  /* 队尾指针 */
     43 } LinkQueue;
     44 
     45 
     46 /******************************************************************************
     47 /* 函数原型声明
     48 /******************************************************************************/
     49 /*队列操作函数*/
     50 Status InitQueue(LinkQueue &Q);
     51 Status DestroyQueue(LinkQueue &Q);
     52 Status ClearQueue(LinkQueue &Q);
     53 Status QueueEmpty(LinkQueue Q);
     54 int QueueLength(LinkQueue Q);
     55 Status GetHead(LinkQueue Q, QElemType &e);
     56 Status EnQueue(LinkQueue &Q, QElemType e);
     57 Status DeQueue(LinkQueue &Q, QElemType &e);
     58 
     59 
     60 /*******************************************************************************
     61 /* <FUNC>
     62 /* 函数名   : InitQueue
     63 /* 功能     : 构造空队列
     64 /* 参数     : -
     65 /* 返回值   : -
     66 /* 备注     : 构造一个空队列Q
     67 /* 作者     : <xxx>
     68 /* </FUNC>
     69 *******************************************************************************/
     70 Status InitQueue(LinkQueue &Q) {
     71     Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
     72     if (!Q.front) exit(OVERFLOW);
     73     Q.front->next = NULL;
     74     return OK;
     75 }
     76 
     77 /*******************************************************************************
     78 /* <FUNC>
     79 /* 函数名   : DestroyQueue
     80 /* 功能     : 销毁队列
     81 /* 参数     : -
     82 /* 返回值   : -
     83 /* 备注     : 销毁队列Q
     84 /* 作者     : <xxx>
     85 /* </FUNC>
     86 *******************************************************************************/
     87 Status DestroyQueue(LinkQueue &Q) {
     88     while (Q.front) {
     89         Q.rear = Q.front->next;
     90         free(Q.front);
     91         Q.front = Q.rear;
     92     }
     93     return OK;
     94 }
     95 
     96 /*******************************************************************************
     97 /* <FUNC>
     98 /* 函数名   : EnQueue
     99 /* 功能     : 入队列
    100 /* 参数     : -
    101 /* 返回值   : -
    102 /* 备注     : 插入元素e为Q的新的队尾元素
    103 /* 作者     : <xxx>
    104 /* </FUNC>
    105 *******************************************************************************/
    106 Status EnQueue(LinkQueue &Q, QElemType e) {
    107     struct QNode *p = (QueuePtr)malloc(sizeof(QNode));
    108     if (!p) exit(OVERFLOW);
    109     p->data = e; p->next = NULL;
    110     Q.rear->next = p;
    111     Q.rear = p;
    112     return OK;
    113 }
    114 
    115 /*******************************************************************************
    116 /* <FUNC>
    117 /* 函数名   : DeQueue
    118 /* 功能     : 出队列
    119 /* 参数     : -
    120 /* 返回值   : -
    121 /* 备注     : 若队列不空, 则删除Q的队头元素, 用e返回其值, 并返回OK; 否则返回ERROR
    122 /* 作者     : <xxx>
    123 /* </FUNC>
    124 *******************************************************************************/
    125 Status DeQueue(LinkQueue &Q, QElemType &e) {
    126     struct QNode *p = NULL;
    127     if (Q.front == Q.rear) return ERROR;
    128     p = Q.front->next;
    129     e = p->data;
    130     Q.front->next = p->next;
    131     if (Q.rear == p) Q.rear = Q.front;
    132     free(p);
    133     return OK;
    134 }
    135 
    136 
    137 /*******************************************************************************
    138 /* <FUNC>
    139 /* 函数名   : main
    140 /* 功能     : 测试函数
    141 /* 参数     : -
    142 /* 返回值   : -
    143 /* 备注     : -
    144 /* 作者     : <xxx>
    145 /* </FUNC>
    146 *******************************************************************************/
    147 void main()
    148 {
    149     LinkQueue Q;  QElemType e;
    150     InitQueue(Q);
    151     EnQueue(Q, 10);
    152     EnQueue(Q, 20);
    153     EnQueue(Q, 30);
    154     if(OK == DeQueue(Q, e)) printf("%d
    ", e);
    155     if(OK == DeQueue(Q, e)) printf("%d
    ", e);
    156     if(OK == DeQueue(Q, e)) printf("%d
    ", e);
    157     if(OK == DeQueue(Q, e)) printf("%d
    ", e);
    158 }

    参考:http://www.cnblogs.com/JCSU/articles/2005839.html

  • 相关阅读:
    【个人笔记】MySQL聚合函数、子查询
    artTemplate的使用总结
    【学习笔记】node.js入门基础
    【知了堂学习笔记】SQL查询基础语句(单表查询、多表查询)
    浅谈java final关键字
    3D魔幻旋转
    3分钟实现星空图
    深入浅出,谈谈面向对象几大特征
    html,css常用标签
    java中常用集合的理解
  • 原文地址:https://www.cnblogs.com/heyonggang/p/3272618.html
Copyright © 2011-2022 走看看