顺序队列表示与操作实现

准备条件:

  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "math.h"
  4. #include "time.h"
  5. #define OK 1
  6. #define ERROR 0
  7. #define TRUE 1
  8. #define FALSE 0
  9. #define MAXSIZE 20 /* 存储空间初始分配量 */
  10. typedef int Status;
  11. typedef int QElemType; /* QElemType类型根据实际情况而定,这里假设为int */
  12. /* 循环队列的顺序存储结构 */
  13. typedef struct
  14. {
  15. QElemType data[MAXSIZE];
  16. int front; /* 头指针 */
  17. int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
  18. }SqQueue;

调用方式:

  1. int main(int argc, const char * argv[]) {
  2. // insert code here...
  3. printf("001--顺序队列表示与操作实现\n");
  4. Status j;
  5. int i=0,l;
  6. QElemType d;
  7. SqQueue Q;
  8. InitQueue(&Q);
  9. printf("初始化队列后,队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
  10. printf("入队:\n");
  11. while (i < 10) {
  12. EnQueue(&Q, i);
  13. i++;
  14. }
  15. QueueTraverse(Q);
  16. printf("队列长度为: %d\n",QueueLength(Q));
  17. printf("现在队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
  18. printf("出队:\n");
  19. //出队
  20. DeQueue(&Q, &d);
  21. printf("出队的元素:%d\n",d);
  22. QueueTraverse(Q);
  23. //获取队头
  24. j=GetHead(Q,&d);
  25. if(j)
  26. printf("现在队头元素为: %d\n",d);
  27. ClearQueue(&Q);
  28. printf("清空队列后, 队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
  29. return 0;
  30. }

1. 初始化一个空队列Q

  1. Status InitQueue(SqQueue *Q){
  2. Q->front = 0;
  3. Q->rear = 0;
  4. return OK;
  5. }

2. 将队列清空

  1. Status ClearQueue(SqQueue *Q){
  2. Q->front = Q->rear = 0;
  3. return OK;
  4. }

3. 若队列Q为空队列,则返回TRUR,否则返回FALSE;

  1. //3 若队列Q为空队列,则返回TRUR,否则返回FALSE;
  2. Status QueueEmpty(SqQueue Q){
  3. //队空标记
  4. if (Q.front == Q.rear)
  5. return TRUE;
  6. else
  7. return FALSE;
  8. }

4. 返回Q的元素个数,也就是队列的当前长度

  1. //6.4 返回Q的元素个数,也就是队列的当前长度
  2. int QueueLength(SqQueue Q){
  3. return (Q.rear - Q.front + MAXSIZE)%MAXSIZE;
  4. }

5. 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR;

  1. Status GetHead(SqQueue Q,QElemType *e){
  2. //队列已空
  3. if (Q.front == Q.rear)
  4. return ERROR;
  5. *e = Q.data[Q.front];
  6. return OK;
  7. }

6. 若队列未满,则插入元素e为新队尾元素

  1. Status EnQueue(SqQueue *Q,QElemType e){
  2. //队列已满
  3. if((Q->rear+1)%MAXSIZE == Q->front)
  4. return ERROR;
  5. //将元素e赋值给队尾
  6. Q->data[Q->rear] = e;
  7. //rear指针向后移动一位,若到最后则转到数组头部;
  8. Q->rear = (Q->rear+1)%MAXSIZE;
  9. return OK;
  10. }

7. 若队列不空,则删除Q中队头的元素,用e返回值

  1. Status DeQueue(SqQueue *Q,QElemType *e){
  2. //判断队列是否为空
  3. if (Q->front == Q->rear) {
  4. return ERROR;
  5. }
  6. //将队头元素赋值给e
  7. *e = Q->data[Q->front];
  8. //front 指针向后移动一位,若到最后则转到数组头部
  9. Q->front = (Q->front+1)%MAXSIZE;
  10. return OK;
  11. }

8. 从队头到队尾依次对队列的每个元素数组

  1. Status QueueTraverse(SqQueue Q){
  2. int i;
  3. i = Q.front;
  4. while ((i+Q.front) != Q.rear) {
  5. printf("%d ",Q.data[i]);
  6. i = (i+1)%MAXSIZE;
  7. }
  8. printf("\n");
  9. return OK;
  10. }

链队列表示与操作实现

准备条件

  1. #define OK 1
  2. #define ERROR 0
  3. #define TRUE 1
  4. #define FALSE 0
  5. #define MAXSIZE 20 /* 存储空间初始分配量 */
  6. typedef int Status;
  7. typedef int QElemType; /* QElemType类型根据实际情况而定,这里假设为int */
  8. typedef struct QNode /* 结点结构 */
  9. {
  10. QElemType data;
  11. struct QNode *next;
  12. }QNode,*QueuePtr;
  13. typedef struct /* 队列的链表结构 */
  14. {
  15. QueuePtr front,rear; /* 队头、队尾指针 */
  16. }LinkQueue;

调用示例:

  1. int main(int argc, const char * argv[]) {
  2. // insert code here...
  3. printf("链队列的表示与操作!\n");
  4. Status iStatus;
  5. QElemType d;
  6. LinkQueue q;
  7. //1.初始化队列q
  8. iStatus = InitQueue(&q);
  9. //2. 判断是否创建成
  10. if (iStatus) {
  11. printf("成功地构造了一个空队列\n");
  12. }
  13. //3.判断队列是否为空
  14. printf("是否为空队列?%d (1:是 0:否)\n",QueueEmpty(q));
  15. //4.获取队列的长度
  16. printf("队列的长度为%d\n",QueueLength(q));
  17. //5.插入元素到队列中
  18. EnQueue(&q, -3);
  19. EnQueue(&q, 6);
  20. EnQueue(&q, 12);
  21. printf("队列的长度为%d\n",QueueLength(q));
  22. printf("是否为空队列?%d (1:是 0:否)\n",QueueEmpty(q));
  23. //6.遍历队列
  24. printf("队列中的元素如下:\n");
  25. QueueTraverse(q);
  26. //7.获取队列头元素
  27. iStatus = GetHead(q, &d);
  28. if (iStatus == OK) {
  29. printf("队头元素是:%d\n",d);
  30. }
  31. //8.删除队头元素
  32. iStatus =DeQueue(&q, &d);
  33. if (iStatus == OK) {
  34. printf("删除了的队头元素为:%d\n",d);
  35. }
  36. //9.获取队头元素
  37. iStatus = GetHead(q, &d);
  38. if (iStatus == OK) {
  39. printf("新的队头元素为:%d\n",d);
  40. }
  41. //10.清空队列
  42. ClearQueue(&q);
  43. //11.销毁队列
  44. DestoryQueue(&q);
  45. return 0;
  46. }

1. 初始化队列

  1. Status InitQueue(LinkQueue *Q){
  2. //1. 头/尾指针都指向新生成的结点
  3. Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));
  4. //2.判断是否创建新结点成功与否
  5. if (!Q->front) {
  6. return ERROR;
  7. }
  8. //3.头结点的指针域置空
  9. Q->front->next = NULL;
  10. return OK;
  11. }

2. 销毁队列Q

  1. Status DestoryQueue(LinkQueue *Q){
  2. //遍历整个队列,销毁队列的每个结点
  3. while (Q->front) {
  4. Q->rear = Q->front->next;
  5. free(Q->front);
  6. Q->front = Q->rear;
  7. }
  8. return OK;
  9. }

3. 将队列Q置空

  1. Status ClearQueue(LinkQueue *Q){
  2. QueuePtr p,q;
  3. Q->rear = Q->front;
  4. p = Q->front->next;
  5. Q->front->next = NULL;
  6. while (p) {
  7. q = p;
  8. p = p->next;
  9. free(q);
  10. }
  11. return OK;
  12. }

4. 判断队列Q是否为空

  1. Status QueueEmpty(LinkQueue Q){
  2. if (Q.front == Q.rear)
  3. return TRUE;
  4. else
  5. return FALSE;
  6. }

5. 获取队列长度

  1. int QueueLength(LinkQueue Q){
  2. int i= 0;
  3. QueuePtr p;
  4. p = Q.front;
  5. while (Q.rear != p) {
  6. i++;
  7. p = p->next;
  8. }
  9. return i;
  10. }

6. 插入元素e为队列Q的新元素

  1. Status EnQueue(LinkQueue *Q,QElemType e){
  2. //为入队元素分配结点空间,用指针s指向;
  3. QueuePtr s = (QueuePtr)malloc(sizeof(QNode));
  4. //判断是否分配成功
  5. if (!s) {
  6. return ERROR;
  7. }
  8. //将新结点s指定数据域.
  9. s->data = e;
  10. s->next = NULL;
  11. //将新结点插入到队尾
  12. Q->rear->next = s;
  13. //修改队尾指针
  14. Q->rear = s;
  15. return OK;
  16. }

7. 出队列

  1. Status DeQueue(LinkQueue *Q,QElemType *e){
  2. QueuePtr p;
  3. //判断队列是否为空;
  4. if (Q->front == Q->rear) {
  5. return ERROR;
  6. }
  7. //将要删除的队头结点暂时存储在p
  8. p = Q->front->next;
  9. //将要删除的队头结点的值赋值给e
  10. *e = p->data;
  11. //将原队列头结点的后继p->next 赋值给头结点后继
  12. Q->front->next = p ->next;
  13. //若队头就是队尾,则删除后将rear指向头结点.
  14. if(Q->rear == p) Q->rear = Q->front;
  15. free(p);
  16. return OK;
  17. }

8. 获取队头元素

  1. Status GetHead(LinkQueue Q,QElemType *e){
  2. //队列非空
  3. if (Q.front != Q.rear) {
  4. //返回队头元素的值,队头指针不变
  5. *e = Q.front->next->data;
  6. return TRUE;
  7. }
  8. return FALSE;
  9. }

9. 遍历队列

  1. Status QueueTraverse(LinkQueue Q){
  2. QueuePtr p;
  3. p = Q.front->next;
  4. while (p) {
  5. printf("%d ",p->data);
  6. p = p->next;
  7. }
  8. printf("\n");
  9. return OK;
  10. }