Advertisement
loki10i

Untitled

Mar 29th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #define SIZE 5
  4. using namespace std;
  5. class myq
  6. {
  7. int *arr;
  8. int top;
  9. int capacity;
  10. public:
  11. myq(int size = SIZE);
  12. ~ myq();
  13. void push(int);
  14. void pop();
  15. int front();
  16. int back();
  17. int size();
  18. bool isempty();
  19. bool isfull();
  20. };
  21. myq::myq(int size)
  22. {
  23. arr = new int[size];
  24. top = -1;
  25. capacity = size;
  26. }
  27. myq::~myq()
  28. {
  29. delete arr;
  30. }
  31. void myq::push(int x)
  32. {
  33. if (isfull())
  34. {
  35. int *A = new int[capacity + 1];
  36. for (int i = 0; i < capacity; i++)
  37. A[i] = arr[i];
  38. A[capacity] = x;
  39. capacity++;
  40. delete arr;
  41. arr = new int[capacity];
  42. for (int i = 0; i < capacity; i++)
  43. arr[i] = A[i];
  44. delete A;
  45. top++;
  46. cout << "Inserting " << x << endl;
  47. }
  48. else
  49. {
  50. cout << "Inserting " << x << endl;
  51. arr[++top] = x;
  52. }
  53. }
  54. bool myq::isfull()
  55. {
  56. return top == capacity - 1;
  57. }
  58. bool myq::isempty()
  59. {
  60. return top == - 1;
  61. }
  62. void myq::pop()
  63. {
  64. if (isempty())
  65. cout << "Underflow" << endl;
  66. else
  67. {
  68. cout << "Removing " << arr[0] << endl;
  69. for (int i = 1; i <= top; i++)
  70. arr[i - 1] = arr[i];
  71. top--;
  72. }
  73. }
  74. int myq::front()
  75. {
  76. if (!isempty())
  77. return arr[0];
  78. else {
  79. cout << "Underflow" << endl; exit(EXIT_FAILURE);
  80. }
  81. }
  82. int myq::back()
  83. {
  84. if (!isempty())
  85. return arr[top];
  86. else {
  87. cout << "Underflow" << endl; exit(EXIT_FAILURE);
  88. }
  89. }
  90. int myq::size()
  91. {
  92. return top + 1;
  93. }
  94.  
  95. int main()
  96. {
  97. myq Q = myq();
  98. Q.push(666); Q.push(112); Q.push(911); Q.push(1); Q.push(2); Q.push(3);
  99. cout <<"first "<< Q.front()<<" last " << Q.back()<<endl;
  100.  
  101. for (int i = 0; i < Q.size();)
  102. {
  103. cout << Q.front() << "\n";
  104. Q.pop();
  105. }
  106. return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement