队列的实现与基本操作

古城微笑少年丶 2022-06-03 00:17 275阅读 0赞

*

1.链队列

*
存储结构:

  1. typedef struct LQNode
  2. {
  3. DataType info;
  4. struct LQNode *next;
  5. }LQNode;
  6. typedef struct
  7. {
  8. struct LQNode *front;
  9. struct LQNode *rear;
  10. }LinkQueue;

基本操作的实现:

  1. //创建空队列
  2. LinkQueue *LQueueCreateEmpty()
  3. {
  4. LinkQueue *plq=(LinkQueue*)malloc(sizeof(LinkQueue));
  5. LQNode *p=(LQNode*)malloc(sizeof(LQNode));
  6. p->next=NULL;
  7. plq->front=plq->rear=p;
  8. return plq;
  9. }
  10. //判断队列是否空
  11. int LQueueIsEmpty(LinkQueue *plq)
  12. {
  13. if(plq->front==plq->rear)
  14. {
  15. return TRUE;
  16. }
  17. else
  18. return FALSE;
  19. }
  20. //入队
  21. void LQueueEnqueue(LinkQueue *plq,DataType x)
  22. {
  23. LQNode *p=(LQNode*)malloc(sizeof(LQNode));
  24. if(p==NULL)
  25. printf("内存分配失败\n");
  26. else
  27. {
  28. p->info=x;
  29. p->next=NULL;
  30. plq->rear->next=p;
  31. plq->rear=p;
  32. }
  33. }
  34. //出队
  35. int LQueueDeQueue(LinkQueue *plq,DataType *x)
  36. {
  37. LQNode *p;
  38. if(plq->front==plq->rear)
  39. {
  40. printf("队列空\n");
  41. return ERROR;
  42. }
  43. else
  44. {
  45. p=plq->front->next;
  46. *x=p->info;
  47. plq->front->next=p->next;
  48. free(p);
  49. return OK;
  50. }
  51. }
  52. //返回队首元素
  53. DataType LQueueFront(LinkQueue *plq)
  54. {
  55. if(plq->front==plq->rear)
  56. {
  57. printf("队列空\n");
  58. exit(0);
  59. }
  60. return (plq->front->next->info);
  61. }

*

2.循环队列

*

存储结构:

  1. typedef struct
  2. {
  3. int front,rear;
  4. DataType data[MAXSIZE];
  5. }SeqQueue;

基本操作的实现:

  1. //创建
  2. SeqQueue *SQueueCreate()
  3. {
  4. SeqQueue *sq=(SeqQueue*)malloc(sizeof(SeqQueue));
  5. if(sq==NULL)
  6. printf("溢出\n");
  7. else
  8. sq->front=sq->rear=0;
  9. return sq;
  10. }
  11. //判空
  12. int SQueueIsEmpty(SeqQueue *sq)
  13. {
  14. if(sq->front==sq->rear)
  15. return TRUE;
  16. else
  17. return FALSE;
  18. }
  19. //入队
  20. void SQueueEnQueue(SeqQueue *sq,DataType x)
  21. {
  22. if((sq->rear+1)%MAXSIZE==sq->front)
  23. printf("队列满");
  24. else
  25. {
  26. sq->rear=(sq->rear+1)%MAXSIZE;
  27. sq->data[sq->rear]=x;
  28. }
  29. }
  30. //出队
  31. int SQueueDeQueue(SeqQueue *sq,DataType *e)
  32. {
  33. if(sq->front==sq->rear)
  34. {
  35. printf("队空\n");
  36. return ERROR;
  37. }
  38. else
  39. {
  40. sq->front=(sq->front+1)%MAXSIZE;
  41. *e=sq->data[sq->front];
  42. return OK;
  43. }
  44. }
  45. //取队首元素
  46. DataType SQueueFront(SeqQueue *sq)
  47. {
  48. if(sq->front==sq->rear)
  49. {
  50. printf("队空下溢\n");
  51. return ERROR;
  52. }
  53. else
  54. return (sq->data[(sq->front+1)%MAXSIZE]);
  55. }
  56. //输出循环队列
  57. void SQueuePrint(SeqQueue *sq)
  58. {
  59. int i=(sq->front+1)%MAXSIZE;
  60. while(i!=sq->rear+1)
  61. {
  62. printf("\t%d",sq->data[i]);
  63. i=(i+1)%MAXSIZE;
  64. }
  65. }

发表评论

表情:
评论列表 (有 0 条评论,275人围观)

还没有评论,来说两句吧...

相关阅读

    相关 c++实现基本操作

    队列是一种常见的数据结构,它的特点是先进先出,也就是说,先加入队列的元素先被取出,而后加入的元素则在其后面等待。 在C++中,队列可以通过STL库中的queue类来实现。下

    相关 基本操作

    目录 一、什么是队列? 二、用数组实现队列 代码实现: 数组实现的缺点: 三、用链表实现队列 代码实现: -------------------- 一、什么

    相关 循环基本操作

    循环队列设置不同队空与队满条件的解决方案 为了解决顺序队列假溢出的问题,我们采用的方案是少用一个元素空间,此时的指针状态是队尾指针加1才与队头指针重合,于是此时队满的判定