数据结构C语言实现之链式队列的6种算法代码

来源:互联网 发布:娇喘合集 网络歌手 编辑:程序博客网 时间:2024/05/16 14:36
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef int elemType;
  4. /************************************************************************/
  5. /* 以下是关于队列链接存储操作的6种算法 */
  6. /************************************************************************/
  7. struct sNode{
  8.     elemType data; /* 值域 */
  9.     struct sNode *next; /* 链接指针 */
  10. };
  11. struct queueLK{
  12.     struct sNode *front; /* 队首指针 */
  13.     struct sNode *rear; /* 队尾指针 */
  14. };

  15. /* 1.初始化链队 */
  16. void initQueue(struct queueLK *hq)
  17. {
  18.     hq->front = hq->rear = NULL; /* 把队首和队尾指针置空 */
  19.     return;
  20. }

  21. /* 2.向链队中插入一个元素x */
  22. void enQueue(struct queueLK *hq, elemType x)
  23. {
  24.     /* 得到一个由newP指针所指向的新结点 */
  25.     struct sNode *newP;
  26.     newP = malloc(sizeof(struct sNode));
  27.     if(newP == NULL){
  28.         printf("内存空间分配失败! ");
  29.         exit(1);
  30.     }
  31.     /* 把x的值赋给新结点的值域,把新结点的指针域置空 */
  32.     newP->data = x;
  33.     newP->next = NULL;
  34.     /* 若链队为空,则新结点即是队首结点又是队尾结点 */
  35.     if(hq->rear == NULL){
  36.         hq->front = hq->rear = newP;
  37.     }else{ /* 若链队非空,则依次修改队尾结点的指针域和队尾指针,使之指向新的队尾结点 */
  38.         hq->rear = hq->rear->next = newP; /* 注意赋值顺序哦 */
  39.     }
  40.     return;
  41. }

  42. /* 3.从队列中删除一个元素 */
  43. elemType outQueue(struct queueLK *hq)
  44. {
  45.     struct sNode *p;
  46.     elemType temp;
  47.     /* 若链队为空则停止运行 */
  48.     if(hq->front == NULL){
  49.         printf("队列为空,无法删除! ");
  50.         exit(1);
  51.     }
  52.     temp = hq->front->data; /* 暂存队尾元素以便返回 */
  53.     p = hq->front; /* 暂存队尾指针以便回收队尾结点 */
  54.     hq->front = p->next; /* 使队首指针指向下一个结点 */
  55.     /* 若删除后链队为空,则需同时使队尾指针为空 */
  56.     if(hq->front == NULL){
  57.         hq->rear = NULL;
  58.     }
  59.     free(p); /* 回收原队首结点 */
  60.     return temp; /* 返回被删除的队首元素值 */
  61. }

  62. /* 4.读取队首元素 */
  63. elemType peekQueue(struct queueLK *hq)
  64. {
  65.     /* 若链队为空则停止运行 */
  66.     if(hq->front == NULL){
  67.         printf("队列为空,无法删除! ");
  68.         exit(1);
  69.     }
  70.     return hq->front->data; /* 返回队首元素 */
  71. }

  72. /* 5.检查链队是否为空,若为空则返回1, 否则返回0 */
  73. int emptyQueue(struct queueLK *hq)
  74. {
  75.     /* 判断队首或队尾任一个指针是否为空即可 */
  76.     if(hq->front == NULL){
  77.         return 1;
  78.     }else{
  79.         return 0;
  80.     }
  81. }

  82. /* 6.清除链队中的所有元素 */
  83. void clearQueue(struct queueLK *hq)
  84. {
  85.     struct sNode *= hq->front; /* 队首指针赋给p */
  86.     /* 依次删除队列中的每一个结点,最后使队首指针为空 */
  87.     while(!= NULL){
  88.         hq->front = hq->front->next;
  89.         free(p);
  90.         p = hq->front;
  91.     } /* 循环结束后队首指针已经为空 */
  92.     hq->rear = NULL; /* 置队尾指针为空 */
  93.     return;
  94. }

  95. /************************************************************************/

  96. int main(int argc, char* argv[])
  97. {
  98.     struct queueLK q;
  99.     int a[8] = {3, 8, 5, 17, 9, 30, 15, 22};
  100.     int i;
  101.     initQueue(&q);
  102.     for(= 0; i < 8; i++){
  103.         enQueue(&q, a[i]);
  104.     }
  105.     printf("%d ", outQueue(&q)); printf("%d ", outQueue(&q));
  106.     enQueue(&q, 68);
  107.     printf("%d ", peekQueue(&q)); printf("%d ", outQueue(&q));
  108.     while(!emptyQueue(&q)){
  109.         printf("%d ", outQueue(&q));
  110.     }
  111.     printf(" ");
  112.     clearQueue(&q);
  113.     system("pause");
  114. }
  115. ##################################################################
  116. PART II


  117. #include <stdio.h>
  118. #include <stdlib.h>

  119. typedef int elemType;
  120. /************************************************************************/
  121. /* 以下是关于队列顺序存储操作的6种算法 */
  122. /************************************************************************/

  123. struct queue{
  124.     elemType *queue; /* 指向存储队列的数组空间 */
  125.     int front, rear, len; /* 队首指针(下标),队尾指针(下标),队列长度变量 */
  126.     int maxSize; /* queue数组长度 */
  127. };

  128. void againMalloc(struct queue *q)
  129. {
  130.     /* 空间扩展为原来的2倍,原内容被自动拷贝到p所指向的存储空间中 */
  131.     elemType *p;
  132.     p = realloc(q->queue, 2 * q->maxSize * sizeof(elemType));
  133.     /* 动态存储空间分配,若失败则退出运行 */
  134.     if(!p){
  135.         printf("空间分配失败! ");
  136.         exit(1);
  137.     }
  138.     q->queue = p; /* 使queue指向新的队列空间 */
  139.     /* 把原队列的尾部内容后移maxSize个位置 */
  140.     if(q->rear != q->maxSize -1){
  141.         int i;
  142.         for(= 0; i <= q->rear; i++){
  143.             q->queue[i+q->maxSize] = q->queue[i];
  144.         }
  145.         q->rear += q->maxSize; /* 队尾指针后移maxSize个位置 */
  146.     }
  147.     q->maxSize = 2 * q->maxSize; /* 把队列空间大小修改为新的长度 */
  148.     return;
  149. }

  150. /* 1.初始化队列 */
  151. void initQueue(struct queue *q, int ms)
  152. {
  153.     /* 检查ms是否有效,若无效则退出运行 */
  154.     if(ms <= 0){
  155.         printf("ms值非法! ");
  156.         exit(1);
  157.     }
  158.     q->maxSize = ms; /* 置队列空间大小为ms */
  159.     /* 动态存储空间分配,若失败则退出运行 */
  160.     q->queue = malloc(ms * sizeof(elemType));
  161.     if(!q->queue){
  162.         printf("内存空间分配失败! ");
  163.         exit(1);
  164.     }
  165.     q->front = q->rear = 0; /* 初始置队列为空 */
  166.     return;
  167. }

  168. /* 2.向队列中插入元素x */
  169. void enQueue(struct queue *q, elemType x)
  170. {
  171.     /* 当队列满时进行动态生分配 */
  172.     if((q->rear + 1) % q->maxSize == q->front){
  173.         againMalloc(q);
  174.     }
  175.     q->rear = (q->rear + 1) % q->maxSize; /* 求出队尾的下一个位置 */
  176.     q->queue[q->rear] = x; /* 把x的值赋给新的队尾 */
  177.     return;
  178. }

  179. /* 3.从队列中删除元素并返回 */
  180. elemType outQueue(struct queue *q)
  181. {
  182.     /* 若队列为空则终止运行 */
  183.     if(q->front == q->rear){
  184.         printf("队列为空,无法删除! ");
  185.         exit(1);
  186.     }
  187.     q->front = (q->front +1) % q->maxSize; /* 使队首指针指向下一个位置 */
  188.     return q->queue[q->front]; /* 返回队首元素 */
  189. }

  190. /* 4.读取队首元素,不改变队列状态 */
  191. elemType peekQueue(struct queue *q)
  192. {
  193.     /* 若队列为空则终止运行 */
  194.     if(q->front == q->rear){
  195.         printf("队列为空,无法删除! ");
  196.         exit(1);
  197.     }
  198.     return q->queue[(q->front +1) % q->maxSize];/* 队首元素是队首指针的下一个位置中的元素 */
  199. }

  200. /* 5.检查一个队列是否为空,若是则返回1,否则返回0 */
  201. int emptyQueue(struct queue *q)
  202. {
  203.     if(q->front == q->rear){
  204.         return 1;
  205.     }else{
  206.         return 0;
  207.     }
  208. }

  209. /* 6.清除一个队列,并释放动态存储空间 */
  210. void clearQueue(struct queue *q)
  211. {
  212.     if(q->queue != NULL){
  213.         free(q->queue);
  214.         q->queue = NULL; /* 设置队列空间指针为空 */
  215.         q->front = q->rear = 0; /* 设置队列为空 */
  216.         q->maxSize = 0; /* 设置队列大小为0 */
  217.     }
  218.     return;
  219. }

  220. /************************************************************************/

  221. int main(int argc, char* argv[])
  222. {
  223.     struct queue q;
  224.     int a[8] = {3, 8, 5, 17, 9, 30, 15, 22};
  225.     int i;
  226.     initQueue(&q, 5);
  227.     for(= 0; i < 8; i++){
  228.         enQueue(&q, a[i]);
  229.     }
  230.     printf("%d ", outQueue(&q)); printf("%d ", outQueue(&q));
  231.     enQueue(&q, 68);
  232.     printf("%d ", peekQueue(&q)); printf("%d ", outQueue(&q));
  233.     while(!emptyQueue(&q)){
  234.         printf("%d ", outQueue(&q));
  235.     }
  236.     printf(" ");
  237.     clearQueue(&q);
  238.     system("pause");
  239.     return 0;
  240. }
0 0
原创粉丝点击