zoukankan      html  css  js  c++  java
  • 队的链式结构表示

    from:shiyanlou

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 #define TRUE 1
      5 #define FALSE 0
      6 #define OK 1
      7 #define ERROR 0
      8 #define OVERFLOW -2
      9 
     10 typedef int QElemType;
     11 typedef int Status;
     12 
     13 /*
     14  * 存储结构
     15  */
     16 typedef struct QNode
     17 {
     18     QElemType data;
     19     struct QNode *next;
     20 }QNode, *QueuePtr;
     21 
     22 typedef struct
     23 {
     24     QueuePtr front;    //队头指针
     25     QueuePtr rear;    //队尾指针
     26 }LinkQueue;
     27 
     28 /*
     29  * 初始化队列
     30  */
     31 Status InitQueue(LinkQueue *Q)
     32 {
     33     Q->front = Q->rear = (QueuePtr) malloc(sizeof(QNode));
     34     if (!Q->front)
     35     {
     36         exit(OVERFLOW);
     37     }
     38     Q->front->next = NULL;
     39     return OK;
     40 }
     41 
     42 /*
     43  * 销毁队列
     44  */
     45 Status DestroyQueue(LinkQueue *Q)
     46 {
     47     while (Q->front)
     48     {
     49         Q->rear = Q->front->next;
     50         free(Q->front);
     51         Q->front = Q->rear;
     52     }
     53     return OK;
     54 }
     55 
     56 /*
     57  * 清空队列
     58  */
     59 Status ClearQueue(LinkQueue *Q)
     60 {
     61     DestroyQueue(Q);
     62     InitQueue(Q);
     63 }
     64 
     65 /*
     66  * 判断队列是否为空
     67  */
     68 Status IsEmpty(LinkQueue Q)
     69 {
     70     if (Q.front->next == NULL)
     71     {
     72         return TRUE;
     73     }
     74     else
     75     {
     76         return FALSE;
     77     }
     78 }
     79 
     80 /*
     81  * 获取队列的长度
     82  */
     83 int GetLength(LinkQueue Q)
     84 {
     85     int i = 0;
     86     QueuePtr p = Q.front;
     87     while (Q.rear != p)
     88     {
     89         i++;
     90         p = p->next;
     91     }
     92     return i;
     93 }
     94 
     95 /*
     96  * 获取队头元素
     97  */
     98 Status GetHead(LinkQueue Q, QElemType *e)
     99 {
    100     QueuePtr p;
    101     if (Q.front == Q.rear)
    102     {
    103         return ERROR;
    104     }
    105     p = Q.front->next;
    106     *e = p->data;
    107     return OK;
    108 }
    109 
    110 /*
    111  * 入队
    112  */
    113 Status EnQueue(LinkQueue *Q, QElemType e)
    114 {
    115     QueuePtr p = (QueuePtr) malloc(sizeof(QNode));//malloc动态申请的内存程序结束后自行释放,不需要free
    116     if (!p)
    117     {
    118         exit(OVERFLOW);
    119     }
    120     p->data = e;
    121     p->next = NULL;
    122     Q->rear->next = p;
    123     Q->rear = p;
    124     return OK;
    125 }
    126 
    127 /*
    128  * 出队
    129  */
    130 Status DeQueue(LinkQueue *Q, QElemType *e)
    131 {
    132     QueuePtr p;
    133     if (Q->front == Q->rear)
    134     {
    135         return ERROR;
    136     }
    137     p = Q->front->next;
    138     *e = p->data;
    139     Q->front->next = p->next;
    140     if (Q->rear == p)
    141     {
    142         Q->rear = Q->front;
    143     }
    144     free(p);
    145     return OK;
    146 }
    147 
    148 /*
    149  * 访问元素
    150  */
    151 void visit(QElemType e)
    152 {
    153     printf("%d ", e);
    154 }
    155 
    156 /*
    157  * 遍历队列
    158  */
    159 Status TraverseQueue(LinkQueue Q, void (*visit)(QElemType))
    160 {
    161     QueuePtr p = Q.front->next;
    162     while (p)
    163     {
    164         visit(p->data);
    165         p = p->next;
    166     }
    167     return OK;
    168 }
    169 
    170 int main()
    171 {
    172     LinkQueue Q;
    173     if (InitQueue(&Q))
    174     {
    175         QElemType e;
    176         int i;
    177 
    178         printf("init_success
    ");
    179 
    180         if (IsEmpty(Q))
    181         {
    182             printf("queue is empty
    ");
    183         }
    184 
    185         for (i = 0; i < 10; i++)
    186         {
    187             EnQueue(&Q, i);
    188         }
    189 
    190         GetHead(Q, &e);
    191         printf("The first element is %d
    ", e);
    192 
    193         printf("length is %d
    ", GetLength(Q));
    194 
    195         DeQueue(&Q, &e);
    196         printf("delete element is %d
    ", e);
    197 
    198         TraverseQueue(Q, *visit);
    199 
    200         if (DestroyQueue(&Q))
    201         {
    202             printf("
    destroy_success
    ");
    203         }
    204     }
    205 }
  • 相关阅读:
    js获取url参数值[转]
    jQuery.validate 中文API【转】
    tinyMCE使用详解
    使IE6下PNG背景透明的七种方法任你选
    MVC3 上传文件【转】
    兼容google浏览器的CSS代码
    MVC3 输出HTML标签
    正则表达式 非常实用
    silverlight 压缩算法
    silverlight 4调试离线应用
  • 原文地址:https://www.cnblogs.com/niceforbear/p/4526537.html
Copyright © 2011-2022 走看看