C++用数组实现一个固定大小的栈/队列

https://blog.csdn.net/u014228447/article/details/80709655


     
     
     
     
  1. #include
  2. #include
  3. using namespace std;
  4. #define NUM 5
  5. //用数组结构实现大小固定的队列和栈
  6. class ArrayIndexOutOfBoundsException: public exception
  7. {
  8. private:
  9. const char*ptr;
  10. public:
  11. ArrayIndexOutOfBoundsException( const char*ptr)
  12. {
  13. this->ptr = ptr;
  14. }
  15. virtual char const *what()
  16. {
  17. cout<< this->ptr<< endl;
  18. return ptr;
  19. }
  20. };
  21. class MyStack
  22. {
  23. private:
  24. int * array;
  25. int idx;
  26. int N;
  27. public:
  28. MyStack( int N);
  29. ~MyStack();
  30. void push(int element);
  31. int pop();
  32. int peek();
  33. void printStack();
  34. };
  35. MyStack::MyStack( int N)
  36. {
  37. this->N = N;
  38. array = new int[N];
  39. idx= 0;
  40. }
  41. MyStack::~MyStack()
  42. {
  43. delete[] this-> array;
  44. }
  45. void MyStack::push(int element)
  46. {
  47. if(idx == N)
  48. throw ArrayIndexOutOfBoundsException( "栈已满");
  49. else
  50. array[idx++] = element;
  51. }
  52. int MyStack::pop()
  53. {
  54. if(idx == 0)
  55. throw ArrayIndexOutOfBoundsException( "栈中无元素");
  56. else
  57. return array[--idx];
  58. }
  59. int MyStack::peek()
  60. {
  61. if(idx == 0)
  62. {
  63. return -1;
  64. }
  65. else
  66. return array[idx -1]; //注意这里是看栈顶元素,但并不把元素去除所以用idx-1而不用--idx;
  67. }
  68. void MyStack::printStack()
  69. {
  70. for( int i= 0;i
  71. {
  72. cout<< array[i]<< " ";
  73. }
  74. cout<< endl;
  75. }
  76. int main()
  77. {
  78. MyStack stack1(NUM);
  79. stack1.push( 1);
  80. stack1.push( 2);
  81. stack1.push( 3);
  82. stack1.push( 4);
  83. stack1.push( 15);
  84. stack1.printStack();
  85. for( int i= 0;i
  86. {
  87. cout<< "pop = "<endl;
  88. cout<< "peek = "<endl;
  89. cout<< endl;
  90. }
  91. stack1.printStack();
  92. return 0;
  93. }

栈的实现相对简单,队列复杂一点。


     
     
     
     
  1. #include
  2. #include
  3. using namespace std;
  4. #define NUM 5
  5. //用数组结构实现大小固定的队列和栈
  6. class ArrayIndexOutOfBoundsException: public exception
  7. {
  8. private:
  9. const char*ptr;
  10. public:
  11. ArrayIndexOutOfBoundsException( const char*ptr)
  12. {
  13. this->ptr = ptr;
  14. }
  15. virtual char const *what()
  16. {
  17. cout<< this->ptr<< endl;
  18. return ptr;
  19. }
  20. };
  21. class MyQuene
  22. {
  23. private:
  24. int * array;
  25. int start;
  26. int end;
  27. int size;
  28. int len;
  29. public:
  30. MyQuene( int N);
  31. ~MyQuene();
  32. void push(int);
  33. int poll();
  34. int peek();
  35. };
  36. MyQuene::MyQuene( int N)
  37. {
  38. size = 0;
  39. start = 0;
  40. end = 0;
  41. len = N;
  42. array = new int[len];
  43. }
  44. MyQuene::~MyQuene()
  45. {
  46. delete[] array;
  47. }
  48. void MyQuene::push(int element)
  49. {
  50. if(end == len)
  51. throw new ArrayIndexOutOfBoundsException( "The qunen is full");
  52. size++;
  53. array[end] = element;
  54. end = end == len -1 ? 0 : end+ 1;
  55. }
  56. int MyQuene::poll()
  57. {
  58. if(size == 0)
  59. throw new ArrayIndexOutOfBoundsException( "The qunen is empty");
  60. size--;
  61. int tmp = start;
  62. start = start == len -1 ? 0 : start + 1;
  63. }
  64. int MyQuene::peek()
  65. {
  66. if(size == 0)
  67. {
  68. cout<< "The qunen is empty"<< endl;
  69. return -1;
  70. }
  71. return array[start];
  72. }
  73. int main()
  74. {
  75. MyQuene myQ(NUM);
  76. myQ.push( 1);
  77. myQ.poll();
  78. myQ.peek();
  79. return 0;
  80. }

你可能感兴趣的:(C/C++)