Chinaunix首页 | 论坛 | 博客
  • 博客访问: 368186
  • 博文数量: 62
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 557
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-01 14:04
文章分类

全部博文(62)

文章存档

2014年(1)

2013年(61)

分类: C/C++

2013-10-14 14:48:39

原文地址:队列 _0311 作者:丫叩酱


点击(此处)折叠或打开

  1. //queue.c

  2. //实现数组的简单队列
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>

  6. #define MAX 10

  7. int que[MAX];
  8. int head = 0, tail = 0;

  9. void in_queue(int data)
  10. {
  11.     que[tail] = data;
  12.     tail ++;
  13. }

  14. int out_queue(void)
  15. {
  16.     return que[head ++];
  17. }

  18. bool is_empty(void)
  19. {
  20.     return head == tail;
  21. }

  22. bool is_full(void)
  23. {
  24.     return tail == MAX;
  25. }


  26. int main(int argc, const char *argv[])
  27. {
  28.     int i = 0;

  29.     while(! is_full())
  30.         in_queue(i++);

  31.     for (i = 0; i < MAX; i++)
  32.     {
  33.         printf("%d ", que[i]);
  34.     }
  35.     putchar('\n');

  36.     while(! is_empty())
  37.         printf("%d ", out_queue());
  38.     putchar('\n');

  39.     return 0;
  40. }

点击(此处)折叠或打开

  1. //queue_link.c

  2. //用链表实现 队列
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6. #include <assert.h>

  7. #define MAX 10

  8. typedef struct node
  9. {
  10.     char ch;
  11.     struct node *next;
  12. }node_t;

  13. typedef struct queue
  14. {
  15.    node_t *head;
  16.    node_t *tail;
  17. }que_t;

  18. void init_que(que_t *que)
  19. {
  20.     que->head = NULL;
  21.     que->tail = NULL;
  22. }

  23. node_t *make_node(void *p)
  24. {
  25.    node_t *cur;

  26.    cur = malloc(sizeof(node_t));
  27.    assert(cur);

  28.    cur->ch = *((char *)p);
  29.    cur->next = NULL;
  30.     
  31.    return cur;
  32. }

  33. void in_queue(que_t *que, void *p)
  34. {
  35.    node_t *cur = p;

  36.    if(que->head == NULL)
  37.        que->head = que->tail = cur;
  38.    else
  39.    {
  40.         que->tail->next = cur;
  41.         que->tail = cur;
  42.    }
  43. }

  44. node_t *out_queue(que_t *que)
  45. {
  46.    node_t *save;

  47.    save = que->head;
  48.    que->head = que->head->next;

  49.    return save;
  50. }

  51. bool is_empty(que_t *que)
  52. {
  53.    return que->head == NULL;
  54. }


  55. void show_queue(que_t *que)
  56. {
  57.     node_t *cur;

  58.     printf("show queue:");
  59.     for(cur = que->head; cur != NULL; cur = cur->next)
  60.         printf("%c-", cur->ch);
  61.     putchar('\n');
  62. }


  63. int main(int argc, const char *argv[])
  64. {
  65.     char ch;
  66.     char *p;
  67.     que_t que;

  68.     init_que(&que);

  69.     printf("in queue:");
  70.     while((ch = getchar()) != '\n')
  71.     {
  72.         p = &ch;
  73.         in_queue(&que, make_node(p));
  74.     }
  75.     show_queue(&que);

  76.     printf("out queue:");
  77.     while(! is_empty(&que))
  78.     {
  79.         node_t *save_node;
  80.         save_node = out_queue(&que);
  81.         printf("%c-", save_node->ch);
  82.         free(save_node) ;
  83.     }
  84.     putchar('\n');

  85.     return 0;
  86. }

点击(此处)折叠或打开

  1. //queue_ring.c

  2. //实现数组的环队列

  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>

  6. #define MAX 10

  7. int que[MAX];
  8. int head = 0, tail = 0, couter = 0;

  9. void in_queue(int data)
  10. {
  11.     que[tail % MAX] = data;
  12.     tail ++;
  13.     couter ++;
  14. }

  15. int out_queue(void)
  16. {
  17.     int num ;
  18.     num = que[head % MAX];
  19.     head ++;
  20.     couter --;

  21.     return num;
  22. }

  23. bool is_empty(void)
  24. {
  25.     return couter == 0;
  26. }

  27. bool is_full(void)
  28. {
  29.     return couter == MAX;
  30. }

  31. void show_queue(void)
  32. {
  33.     int i;

  34.     for(i = head; i < tail; i++)
  35.         printf("%d ", que[head++]);
  36.     putchar('\n');
  37. }

  38. int main(int argc, const char *argv[])
  39. {
  40.     int i = 0;

  41.     while(! is_full())
  42.         in_queue(i++);

  43.     for (i = 0; i < MAX; i++)
  44.     {
  45.         printf("%d ", que[i]);
  46.     }
  47.     putchar('\n');

  48.     while(! is_empty())
  49.         printf("%d ", out_queue());
  50.     putchar('\n');
  51.     return 0;
  52. }

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

上一篇:栈_0311

下一篇:排序_查找 _0310

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