鱼C论坛

 找回密码
 立即注册
查看: 2639|回复: 0

[技术交流] DS\队列操作集合

[复制链接]
发表于 2014-5-19 13:06:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.循环队列(顺序队列)
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. #define MAXSIZE 5

  4. typedef char ElemType;
  5. typedef struct CycleQueue
  6. {
  7.         ElemType *base;
  8.        
  9.         int front;
  10.         int rear;
  11. }CycleQueue;

  12. void InitQueue(CycleQueue *q)
  13. {
  14.         q->base = (ElemType *)malloc(MAXSIZE*sizeof(ElemType));
  15.         if(!q->base)
  16.         {
  17.                 printf("InitQueue:initialize falied!");
  18.                 exit(0);
  19.         }
  20.         q->front = q->rear = 0;
  21. }

  22. void InsertQueue(CycleQueue *q,ElemType e)
  23. {
  24.         if((q->rear+1)%MAXSIZE ==  q->front)
  25.                 return;
  26.         q->base[q->rear] = e;
  27.         q->rear = (q->rear+1)%MAXSIZE;
  28. }

  29. void DeleteQueue(CycleQueue *q,ElemType *e)
  30. {
  31.         if(q->front == q->rear)
  32.                 return;
  33.         *e = q->base[q->front];
  34.         q->front = (q->front+1)%MAXSIZE;
  35. }

  36. int main()
  37. {
  38.         char e;
  39.         CycleQueue s;
  40.        
  41.         InitQueue(&s);
  42.        
  43.     printf("请输入<#结束>:");
  44.         e = getchar();
  45.         while(e != '#')
  46.         {
  47.                 InsertQueue(&s,e);
  48.                 e = getchar();
  49.         }
  50.        
  51.         printf("打印队列中的元素:");
  52.         while(s.front != s.rear)
  53.         {
  54.                 DeleteQueue(&s,&e);
  55.                 printf("%c",e);
  56.         }
  57.         printf("\n");
  58.        
  59.         return 0;
  60. }
复制代码
2.链队列

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

  3. typedef char ElemType;

  4. typedef struct QNode
  5. {
  6.         ElemType data;
  7.         struct QNode *next;
  8. }QNode,*QueuePtr;

  9. typedef struct LinkQueue
  10. {
  11.         QueuePtr front,rear;
  12. }LinkQueue;

  13. void InitQueue(LinkQueue *q)
  14. {
  15.         q->front = q->rear=(QueuePtr)malloc(sizeof(QNode));
  16.         if(!q->front)
  17.         {
  18.                 printf("Initialization failed!");
  19.                 exit(0);
  20.         }
  21.         q->front->next = NULL;
  22. }

  23. void InsertQueue(LinkQueue *q,ElemType e)
  24. {
  25.         QueuePtr p;
  26.         p = (QueuePtr)malloc(sizeof(QNode));
  27.         if(p==NULL)
  28.         {
  29.                 printf("InsetQueue:initialize p failed!");
  30.                 exit(0);
  31.         }
  32.         p->data = e;
  33.         p->next = NULL;
  34.         q->rear->next = p;
  35.         q->rear = p;
  36. }

  37. void DeleteQueue(LinkQueue *q,ElemType *e)
  38. {
  39.         QueuePtr p;
  40.         if(q->front == q->rear)
  41.                 return;
  42.         p = q->front->next;
  43.         *e = p->data;
  44.         q->front->next = p->next;
  45.         if(q->rear == p)
  46.                 q->rear = q->front;
  47.        
  48.         free(p);       
  49. }

  50. void DestroyQueue(LinkQueue *q)
  51. {
  52.         while(q->front)
  53.         {
  54.                 q->rear = q->front->next;
  55.                 free(q->front);
  56.                 q->front = q->rear;
  57.         }
  58. }

  59. int main()
  60. {
  61.         LinkQueue s;
  62.         ElemType c,d,e;
  63.        
  64.         InitQueue(&s);
  65.        
  66.         printf("请输入整数<#结束>:");
  67.         scanf("%c",&c);
  68.         while(c != '#')
  69.         {
  70.                 InsertQueue(&s,c);
  71.                 scanf("%c",&c);
  72.         }
  73.        
  74.         getchar();
  75.        
  76.         printf("打印队列中的元素:");
  77.         while(s.front != s.rear)
  78.         {
  79.                 DeleteQueue(&s,&e);
  80.                 printf("%c",e);
  81.         }
  82.         printf("\n");
  83.        
  84.         return 0;
  85.    
  86.    
  87. }
复制代码




本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 19:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表