2020考研-王道数据结构-栈和队列-队列

た 入场券 2022-01-30 09:23 323阅读 0赞

第一题

题目简述

实现一个循环队列,能充分利用空间内的元素。

代码
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <ctime>
  4. using namespace std;
  5. #define MAXSIZE 10
  6. typedef int Elemtype;
  7. class Queue{
  8. private:
  9. Elemtype data[MAXSIZE];
  10. int tag[2];
  11. public:
  12. // 0域表示front、1域表示back
  13. Queue(){
  14. tag[0] = 0, tag[1] = 0; }
  15. void push(Elemtype e);
  16. int pop();
  17. bool isEmpty(){
  18. return tag[0] == tag[1]; };
  19. };
  20. void Queue::push(Elemtype e){
  21. if ((tag[1] + 1) % MAXSIZE == tag[0]){
  22. cerr << "队列满!" << endl;
  23. return;
  24. }
  25. data[tag[1]] = e;
  26. tag[1] = (tag[1] + 1) % MAXSIZE;
  27. return;
  28. }
  29. int Queue::pop(){
  30. if (tag[0] == tag[1]) {
  31. cerr << "队列空!" << endl;
  32. return -1;
  33. }
  34. int x = data[tag[0]];
  35. tag[0] = (tag[0] + 1) % MAXSIZE;
  36. return x;
  37. }
  38. int main()
  39. {
  40. Queue que;
  41. srand(time(0));
  42. for (int i = 0; i < 12; i++){
  43. int x = rand() % 10;
  44. que.push(x);
  45. cout << x << " ";
  46. }
  47. cout << endl;
  48. while (!que.isEmpty()){
  49. cout << que.pop() << " ";
  50. }
  51. cout << endl;
  52. return 0;
  53. }

第二题

题目简述

Q是一个队列,S是一个空栈,利用S实现将Q中的元素逆置。

代码
  1. #include <iostream>
  2. #include <queue>
  3. #include <stack>
  4. using namespace std;
  5. void reverseQueue(queue<int> &que)
  6. {
  7. stack<int> stk;
  8. while (que.size()) {
  9. stk.push(que.front());
  10. que.pop();
  11. }
  12. while (stk.size()){
  13. que.push(stk.top());
  14. stk.pop();
  15. }
  16. }
  17. int main()
  18. {
  19. queue<int> que;
  20. que.push(1);
  21. que.push(2);
  22. que.push(3);
  23. que.push(5);
  24. que.push(4);
  25. reverseQueue(que);
  26. while (que.size())
  27. {
  28. cout << que.front() << " ";
  29. que.pop();
  30. }
  31. cout << endl;
  32. return 0;
  33. }

第三题

题目简述

利用两个栈,实现一个队列。

代码
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. typedef int Elemtype;
  5. class Queue
  6. {
  7. private:
  8. stack<Elemtype> stk1, stk2;
  9. public:
  10. void EnQueue(Elemtype x);
  11. Elemtype Dequeue();
  12. bool QueueEmpty(){
  13. return stk1.empty() && stk2.empty(); };
  14. };
  15. void Queue::EnQueue(Elemtype e){
  16. // TODO 这个位置应该有判满操作,因为使用的是stl中的nb栈,所以跳过
  17. stk1.push(e);
  18. }
  19. Elemtype Queue::Dequeue(){
  20. if (stk1.empty() && stk2.empty()){
  21. cerr << "队列为空" << endl;
  22. return -1;
  23. }
  24. Elemtype res;
  25. if (!stk2.empty()){
  26. res = stk2.top();
  27. stk2.pop();
  28. }
  29. else{
  30. while (!stk1.empty()){
  31. stk2.push(stk1.top());
  32. stk1.pop();
  33. }
  34. res = stk2.top();
  35. stk2.pop();
  36. }
  37. return res;
  38. }
  39. int main()
  40. {
  41. Queue que;
  42. for (int type, x;;){
  43. cin >> type;
  44. if (type){
  45. cin >> x;
  46. que.EnQueue(x);
  47. }
  48. else{
  49. cout << que.Dequeue() << endl;
  50. }
  51. }
  52. return 0;
  53. }

发表评论

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

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

相关阅读