当前位置:   article > 正文

C语言链表实现双端队列_双端队列c语言 链式

双端队列c语言 链式

 

6-1 Deque (25 point(s))

A "deque" is a data structure consisting of a list of items, on which the following operations are possible:

  • Push(X,D): Insert item X on the front end of deque D.
  • Pop(D): Remove the front item from deque D and return it.
  • Inject(X,D): Insert item X on the rear end of deque D.
  • Eject(D): Remove the rear item from deque D and return it. Write routines to support the deque that take O(1) time per operation.

Format of functions:

  1. Deque CreateDeque();
  2. int Push( ElementType X, Deque D );
  3. ElementType Pop( Deque D );
  4. int Inject( ElementType X, Deque D );
  5. ElementType Eject( Deque D );

where Deque is defined as the following:

  1. typedef struct Node *PtrToNode;
  2. struct Node {
  3. ElementType Element;
  4. PtrToNode Next, Last;
  5. };
  6. typedef struct DequeRecord *Deque;
  7. struct DequeRecord {
  8. PtrToNode Front, Rear;
  9. };

Here the deque is implemented by a doubly linked list with a header. Front and Rear point to the two ends of the deque respectively. Front always points to the header. The deque is empty when Front and Rear both point to the same dummy header. Note: Push and Inject are supposed to return 1 if the operations can be done successfully, or 0 if fail. If the deque is empty, Pop and Eject must return ERROR which is defined by the judge program.

Sample program of judge:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define ElementType int
  4. #define ERROR 1e5
  5. typedef enum { push, pop, inject, eject, end } Operation;
  6. typedef struct Node *PtrToNode;
  7. struct Node {
  8. ElementType Element;
  9. PtrToNode Next, Last;
  10. };
  11. typedef struct DequeRecord *Deque;
  12. struct DequeRecord {
  13. PtrToNode Front, Rear;
  14. };
  15. Deque CreateDeque();
  16. int Push( ElementType X, Deque D );
  17. ElementType Pop( Deque D );
  18. int Inject( ElementType X, Deque D );
  19. ElementType Eject( Deque D );
  20. Operation GetOp(); /* details omitted */
  21. void PrintDeque( Deque D ); /* details omitted */
  22. int main()
  23. {
  24. ElementType X;
  25. Deque D;
  26. int done = 0;
  27. D = CreateDeque();
  28. while (!done) {
  29. switch(GetOp()) {
  30. case push:
  31. scanf("%d", &X);
  32. if (!Push(X, D)) printf("Memory is Full!\n");
  33. break;
  34. case pop:
  35. X = Pop(D);
  36. if ( X==ERROR ) printf("Deque is Empty!\n");
  37. break;
  38. case inject:
  39. scanf("%d", &X);
  40. if (!Inject(X, D)) printf("Memory is Full!\n");
  41. break;
  42. case eject:
  43. X = Eject(D);
  44. if ( X==ERROR ) printf("Deque is Empty!\n");
  45. break;
  46. case end:
  47. PrintDeque(D);
  48. done = 1;
  49. break;
  50. }
  51. }
  52. return 0;
  53. }
  54. /* Your function will be put here */

Sample Input:

  1. Pop
  2. Inject 1
  3. Pop
  4. Eject
  5. Push 1
  6. Push 2
  7. Eject
  8. Inject 3
  9. End

Sample Output:

  1. Deque is Empty!
  2. Deque is Empty!
  3. Inside Deque: 2 3

 

 首先是初始化函数,返回一个空双端队列

注意,这个双端队列带有一个头节点

Push操作将数据加入到双端队列头部

Pop操作弹出双端队列头部元素

Inject操作在尾部插入一个元素

Eject操作弹出尾部元素

双向链表操作要仔细细致。

  1. /* Your function will be put here */
  2. Deque CreateDeque()
  3. {
  4. Deque deque = (Deque)malloc(sizeof(struct DequeRecord));
  5. PtrToNode temp = (PtrToNode)malloc(sizeof(struct Node));
  6. temp->Last = NULL;
  7. temp->Next = NULL;
  8. deque->Front = temp;
  9. deque->Rear = temp;
  10. return deque;
  11. }
  12. int Push(ElementType X, Deque D)
  13. {
  14. PtrToNode temp;
  15. temp = (PtrToNode)malloc(sizeof(struct Node));
  16. if (!temp)
  17. return 0;
  18. temp->Element = X;
  19. // 空队列情况
  20. if (D->Front == D->Rear)
  21. {
  22. D->Front->Next = temp;
  23. temp->Last = D->Front;
  24. temp->Next = NULL;
  25. D->Rear = temp;
  26. return 1;
  27. }
  28. // 一般情况
  29. temp->Next = D->Front->Next;
  30. temp->Last = D->Front;
  31. D->Front->Next->Last = temp;
  32. D->Front->Next = temp;
  33. return 1;
  34. }
  35. ElementType Pop(Deque D)
  36. {
  37. PtrToNode temp;
  38. int X;
  39. // 空队列
  40. if (D->Front == D->Rear)
  41. return ERROR;
  42. temp = D->Front->Next;
  43. X = temp->Element;
  44. // 如果只有一个节点的情况
  45. if (D->Front->Next == D->Rear)
  46. {
  47. D->Rear = D->Front;
  48. D->Rear->Next = NULL;
  49. free(temp);
  50. return X;
  51. }
  52. // 一般情况
  53. temp->Next->Last = D->Front;
  54. D->Front->Next = temp->Next;
  55. free(temp);
  56. return X;
  57. }
  58. // 末尾添加一个元素
  59. int Inject(ElementType X, Deque D)
  60. {
  61. PtrToNode temp;
  62. temp = (PtrToNode)malloc(sizeof(struct Node));
  63. if (!temp)
  64. return 0;
  65. temp->Element = X;
  66. // 空队列
  67. if (D->Front == D->Rear)
  68. {
  69. D->Front->Next = temp;
  70. temp->Last = D->Front;
  71. D->Rear = temp;
  72. return 1;
  73. }
  74. D->Rear->Next = temp;
  75. temp->Next = NULL;
  76. temp->Last = D->Rear;
  77. D->Rear = temp;
  78. return 1;
  79. }
  80. // 从末尾移除一个元素
  81. ElementType Eject(Deque D)
  82. {
  83. PtrToNode temp;
  84. int X;
  85. if (D->Front == D->Rear)
  86. return ERROR;
  87. temp = D->Rear;
  88. X = temp->Element;
  89. D->Rear = temp->Last;
  90. D->Rear->Next = NULL;
  91. free(temp);
  92. return X;
  93. }

 

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

闽ICP备14008679号