当前位置:   article > 正文

数据结构-线性表-队列_线性表 队列

线性表 队列

 

目录

一、队列

1、队列概念

2、队列的特征

3、队列存储结构

二、顺序队

1、顺序队定义

2、循环队列的四要素

3、循环队列的基本运算算法

(1)代码展示

(2)结果演示

三、链队

1、链队定义

2、链队四要素

3、链队基本运算

(1)代码部分

(3)结果演示

四、总结


一、队列

1、队列概念

队列是一种特殊的线性表,也是一种运算受限的线性表,插入限定在表的某一端进行,删除限定在表的另一端进行。

 

2、队列的特征

先进先出、后进后出

3、队列存储结构

与栈类似,队列也有两种常用存储结构,即顺序存储结构和链式存储结构。

二、顺序队

1、顺序队定义

以顺序存储方式存储的队列称为顺序队。由一个一维数组和队头指针和队尾指针变量组成,一般用循环队列。

2、循环队列的四要素

(1)队空条件:sq.front==qs.rear.

(2)队满条件:(sq.rear+1)%MaxSize==sq.front.

(3)进队操作:sq.rear循环进1;元素进队。

(4)出队操作:sq.front 循环进1;元素出队.

3、循环队列的基本运算算法

(1)代码展示

  1. #include <stdio.h>
  2. #include<malloc.h>
  3. typedef char ElemType;
  4. #define MaxSize 200
  5. typedef struct
  6. {
  7. ElemType data[MaxSize];
  8. int front,rear; //定义队头队尾指针
  9. }SqQueue;
  10. //初始化
  11. void InitQueue(SqQueue &sq)
  12. {
  13. sq.rear=sq.front=0; //指针初始化
  14. }
  15. //销毁
  16. void DestroyQueue(SqQueue sq)
  17. {
  18. }
  19. //进栈
  20. int EnQueue(SqQueue &sq,ElemType x)
  21. {
  22. if((sq.rear+1)%MaxSize==sq.front) //判断队满情况
  23. return 0;
  24. sq.rear=(sq.rear+1)%MaxSize; //从队尾插入,进1
  25. sq.data[sq.rear]=x;
  26. return 1;
  27. }
  28. //出队
  29. int DeQueue(SqQueue &sq,ElemType &x)
  30. {
  31. if(sq.rear==sq.front) //判断队空
  32. return 0;
  33. sq.front=(sq.front+1)%MaxSize; //队头循环进1
  34. x=sq.data[sq.front];
  35. return 1;
  36. }
  37. int GetHead(SqQueue sq,ElemType &x)
  38. {
  39. if(sq.rear==sq.front) //队空情况
  40. return 0;
  41. x=sq.data[(sq.front+1)%MaxSize];
  42. return 1;
  43. }
  44. //判断队空
  45. int QueueEmpty(SqQueue sq)
  46. {
  47. if(sq.rear==sq.front)
  48. return 1;
  49. else return 0;
  50. }
  51. void main()
  52. {
  53. SqQueue sq;
  54. ElemType e;
  55. printf("初始化队列\n");
  56. InitQueue(sq);
  57. printf("队%s\n",(QueueEmpty(sq)==1 ? "空":"不空"));
  58. printf("a进队\n");
  59. EnQueue(sq,'a');
  60. printf("b进队\n");
  61. EnQueue(sq,'b');
  62. printf("c进队\n");
  63. EnQueue(sq,'c');
  64. printf("d进队\n");
  65. EnQueue(sq,'d');
  66. printf("e进队\n");
  67. EnQueue(sq,'e');
  68. printf("f进队\n");
  69. EnQueue(sq,'f');
  70. printf("队%s\n",(QueueEmpty(sq)==1 ? "空":"不空"));
  71. GetHead(sq,e);
  72. printf("队头元素:%c\n",e);
  73. printf("出队次序:");
  74. while(!QueueEmpty(sq))
  75. {
  76. DeQueue(sq,e);
  77. printf("%c",e);
  78. }
  79. printf("\n");
  80. DestroyQueue(sq);
  81. }

(2)结果演示

三、链队

1、链队定义

采用链式存储的队列称为链队。含有单链表的基本特性。

2、链队四要素

(1)队空条件: st->front ==NULL.

(2)队满:不考虑

(3)进队操作:创建结点p,将其插入到队尾,并由st->rear指向它。

(4)出队操作:删除队头结点。

3、链队基本运算

(1)代码部分

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include<string.h>
  4. typedef ElemType;
  5. //数据结点类型声明
  6. typedef struct QNode
  7. {
  8. ElemType data;
  9. struct QNode *next;
  10. }QType;
  11. //链队的数据类型结点声明
  12. typedef struct qptr
  13. {
  14. QType *front; //队头指针
  15. QType *rear; //队尾指针
  16. }LinkQueue; //链队结点类型
  17. //初始化
  18. void InitQueue(LinkQueue *&st)
  19. {
  20. st=(LinkQueue *)malloc(sizeof(LinkQueue));
  21. st->rear=st->front=NULL; //初始化,队头和队尾指针均为空
  22. }
  23. //销毁
  24. void DestroyQueue(LinkQueue *&st)
  25. {
  26. QType *p1=st->front,*p;
  27. if(p1!=NULL) //队不为空时
  28. {
  29. if(p1==st->rear) //只有一个数据结点的情况
  30. free(p1); //释放p1结点
  31. else //有两个或多个数据结点的情况
  32. {
  33. p=p1->next;
  34. while(p!=NULL)
  35. {
  36. free(p1); //释放p1结点
  37. p1=p;
  38. p=p->next;
  39. }
  40. free(p1);
  41. }
  42. }
  43. free(st); //释放链队结点
  44. }
  45. //进队
  46. int EnQueue(LinkQueue *&st,ElemType x)
  47. {
  48. QType *s;
  49. s=(QType *)malloc(sizeof(QType)); //创建新结点s,
  50. s->data=x;
  51. s->next=NULL;
  52. if(st->front==NULL)
  53. st->rear=st->front=s;
  54. else
  55. {
  56. st->rear->next=s;
  57. st->rear=s;
  58. }
  59. return 1;
  60. }
  61. //出队
  62. int DeQueue(LinkQueue *&st,ElemType &x)
  63. {
  64. QType *p;
  65. if(st->front ==NULL) //空队的情况
  66. return 0;
  67. p=st->front;
  68. x=p->data;
  69. if(st->rear==st->front) //取队头元素
  70. {
  71. st->rear=st->front=NULL;
  72. }else
  73. {
  74. st->front=st->front->next;
  75. }
  76. free(p);
  77. return 1;
  78. }
  79. //取队头元素计算
  80. int GetHead(LinkQueue *st,ElemType &x)
  81. {
  82. if(st->front==NULL)
  83. return 0;
  84. x=st->front->data;
  85. return 1;
  86. }
  87. //判断是否队空
  88. int QueueEmpty(LinkQueue *st)
  89. {
  90. if(st->front==NULL)
  91. return 1;
  92. else
  93. return 0;
  94. }
  95. void main()
  96. {
  97. LinkQueue *st;
  98. ElemType e;
  99. printf("初始化队列\n");
  100. InitQueue(st);
  101. printf("队%s\n",(QueueEmpty(st)==1 ? "空":"不空"));
  102. printf("a进队\n");
  103. EnQueue(st,'a');
  104. printf("b进队\n");
  105. EnQueue(st,'b');
  106. printf("c进队\n");
  107. EnQueue(st,'c');
  108. printf("d进队\n");
  109. EnQueue(st,'d');
  110. printf("e进队\n");
  111. EnQueue(st,'e');
  112. printf("f进队\n");
  113. EnQueue(st,'f');
  114. printf("队%s\n",(QueueEmpty(st)==1 ? "空":"不空"));
  115. GetHead(st,e);
  116. printf("队头元素:%c\n",e);
  117. printf("出队次序:");
  118. while(!QueueEmpty(st))
  119. {
  120. DeQueue(st,e);
  121. printf("%c",e);
  122. }
  123. printf("\n");
  124. DestroyQueue(st);
  125. }

(3)结果演示

 

四、总结

顺序队和链队比较,链表可以实时申请空间,不考虑空间利用问题,因此比顺序队有一定的优势。
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/801610
推荐阅读
相关标签
  

闽ICP备14008679号