当前位置:   article > 正文

顺序队列的代码实现_顺序队列的实现代码

顺序队列的实现代码

        队列的操作与栈的操作类似,不同的是,删除是在表的头部(队头)进行的。队列也有两种存储表示,顺序表示和链式表示。

        和顺序栈相类似,在队列的顺序存储结构中,除了用一组地址连续的存储单元依次存储从队头到队尾的元素外,尚需附设两个整形变量front和rear分别指示队头元素及队尾元素的位置。附设整型变量size用来表示队列的最大存储元素个数。

        以下就是顺序队列的不同代码实现:

        C语言(SequentialQueue):

  1. #ifndef SEQUENTIALQUEUE_H_INCLUDED
  2. #define SEQUENTIALQUEUE_H_INCLUDED
  3. #define TRUE 1
  4. #define FALSE -1
  5. typedef int ElemType;
  6. typedef struct Node
  7. {
  8. volatile ElemType *base;
  9. volatile int front;
  10. volatile int rear;
  11. volatile int size;
  12. }SqQueue;
  13. int InitQueue(SqQueue *S,int size)
  14. {
  15. S->base=(SqQueue*)malloc(sizeof(SqQueue)*size);
  16. if(S->base!=NULL)
  17. {
  18. S->front=0;
  19. S->rear=0;
  20. S->size=size;
  21. return TRUE;
  22. }
  23. else
  24. {
  25. return FALSE;
  26. }
  27. }
  28. void DestroyQueue(SqQueue *S)
  29. {
  30. free(S->base);
  31. S->base=NULL;
  32. S->front=NULL;
  33. S->rear=NULL;
  34. S->size=NULL;
  35. }
  36. void ClearQueue(SqQueue *S)
  37. {
  38. for(int i=0;i<S->size;i++)
  39. {
  40. *(S->base+i)=NULL;
  41. }
  42. S->front=0;
  43. S->rear=0;
  44. }
  45. int QueueEmpty(SqQueue *S)
  46. {
  47. if(S->front==S->rear)
  48. {
  49. return TRUE;
  50. }
  51. else
  52. {
  53. return FALSE;
  54. }
  55. }
  56. int QueueLength(SqQueue *S)
  57. {
  58. return S->rear-S->front;
  59. }
  60. ElemType GetHead(SqQueue *S)
  61. {
  62. if(S->front==S->rear)
  63. {
  64. return FALSE;
  65. }
  66. else
  67. {
  68. return S->base[S->front];
  69. }
  70. }
  71. int EnQueue(SqQueue *S,ElemType elem)
  72. {
  73. if(S->rear-S->front==S->size)
  74. {
  75. return FALSE;
  76. }
  77. else
  78. {
  79. S->base[S->rear]=elem;
  80. S->rear++;
  81. return TRUE;
  82. }
  83. }
  84. ElemType DeQueue(SqQueue *S)
  85. {
  86. if(S->front==S->rear)
  87. {
  88. return FALSE;
  89. }
  90. else
  91. {
  92. ElemType tmp=S->base[S->front];
  93. S->front++;
  94. return tmp;
  95. }
  96. }
  97. void QueueTraverse(SqQueue *S)
  98. {
  99. int i=S->front;
  100. while(i<S->rear)
  101. {
  102. printf("%d\t",*(S->base+i));
  103. i++;
  104. }
  105. printf("\n");
  106. }
  107. #endif // SEQUENTIALQUEUE_H_INCLUDED

        Java语言:

  1. package DataStructure.LinearList;
  2. class SequentialList1
  3. {
  4. protected Object []elem;
  5. protected int front;
  6. protected int rear;
  7. public SequentialList1(int maxsize)
  8. {
  9. this.elem=new Object[maxsize];
  10. }
  11. public SequentialList1() {}
  12. }
  13. public class SequentialQueue extends SequentialList1{
  14. private SequentialList1 Queue;
  15. public void InitQueue(int size)
  16. {
  17. this.Queue=new SequentialList1(size);
  18. this.Queue.front=0;
  19. this.Queue.rear=0;
  20. }
  21. public Boolean DestoryQueue()
  22. {
  23. this.ClearQueue();
  24. this.Queue.elem=null;
  25. this.Queue=null;
  26. return true;
  27. }
  28. public Boolean ClearQueue()
  29. {
  30. int i=this.QueueLength();
  31. while (i<this.Queue.rear)
  32. {
  33. this.DeQueue();
  34. i++;
  35. }
  36. this.Queue.rear=this.Queue.front=0;
  37. return true;
  38. }
  39. public Boolean QueueEmpty()
  40. {
  41. if (this.Queue.rear==this.Queue.front)
  42. {
  43. return true;
  44. }
  45. else
  46. {
  47. return false;
  48. }
  49. }
  50. public int QueueLength()
  51. {
  52. return this.Queue.rear-this.Queue.front;
  53. }
  54. public Object GetHead()
  55. {
  56. if (this.Queue.rear==this.Queue.front)
  57. {
  58. return -1;
  59. }
  60. else
  61. {
  62. return this.Queue.elem[this.Queue.front];
  63. }
  64. }
  65. public Boolean EnQueue(Object elem)
  66. {
  67. if (this.Queue.rear==this.Queue.elem.length)
  68. {
  69. return false;
  70. }
  71. else
  72. {
  73. this.Queue.elem[this.Queue.rear]=elem;
  74. this.Queue.rear++;
  75. return true;
  76. }
  77. }
  78. public Object DeQueue()
  79. {
  80. if (this.Queue.front==this.Queue.rear)
  81. {
  82. return -1;
  83. }
  84. Object Get=this.Queue.elem[this.Queue.front];
  85. this.Queue.elem[this.Queue.front]=null;
  86. this.Queue.front++;
  87. return Get;
  88. }
  89. public void QueueTraverse()
  90. {
  91. int i=this.Queue.front;
  92. while (i<this.Queue.rear)
  93. {
  94. System.out.print(this.Queue.elem[i]+"\t");
  95. i++;
  96. }
  97. System.out.println("");
  98. }
  99. }

 

 

 

 

 

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

闽ICP备14008679号