#GOBJ606H. GESP 6级客观题|栈、队列与循环队列|课后作业

GESP 6级客观题|栈、队列与循环队列|课后作业

GESP 6级客观题|栈、队列与循环队列|课后作业

考试频率:高频。本卷共 6 题。

  1. 以下代码实现了循环队列的哪种操作?
class CircularQueue
{
    int *arr;
    int front, rear, size;

public:
    CircularQueue(int k)
    {
        size = k;
        arr = new int[k];
        front = rear = -1;
    }
    bool enQueue(int value)
    {
        if (isFull())
            return false;
        if (isEmpty())
            front = 0;
        rear = (rear + 1) % size;
        arr[rear] = value;
        return true;
    }
};

{{ select(1) }}

  • 入队
  • 出队
  • 查看队首元素
  • 判断队列是否为空
  1. 循环队列常用于实现数据缓冲。假设一个循环队列容量为 5 (即最多存放 4 个元素,留一个位置区分空与满),依次进行操作:入队数据1,2,3,出队1个数据,再入队数据4和5,此时队首到队尾的元素顺序是( )。

    {{ select(2) }}

  • [2, 3, 4, 5]
  • [1, 2, 3, 4]
  • [3, 4, 5, 2]
  • [2, 3, 5, 4]
  1. 在 C++ STL 中,栈(std::stack)的 pop 操作返回栈顶元素并移除它。

    {{ select(3) }}

  • 正确
  • 错误
  1. 循环队列通过模运算循环使用空间。

    {{ select(4) }}

  • 正确
  • 错误
  1. 采⽤如下代码实现检查输⼊的字符串括号是否匹配,横线上应填⼊的代码为( )。
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool is_valid(string s) {
    stack<char> st;
    char top;
    for (char& ch : s) {
        if (ch == '(' || ch == '{' || ch == '[') {
            st.push(ch);    // 左括号入栈
        }
        else
        {
            if (st.empty())
                return false;
            ———————————————————————— // 在此处填入代码
            if ((ch == ')' && top != '(') ||
                (ch == '}' && top != '{') ||
                (ch == ']' && top != '[')) {
                return false;
            }
        }
    }
    return st.empty();      // 栈为空则说明所有括号匹配成功
}

{{ select(5) }}

  • top = st.top(); st.pop();
  • st.pop(); top = st.top();
  • st.pop(); top = st.front();
  • top = st.front(); st.pop();
  1. 下⾯代码判断队列的第⼀个元素是否等于 ,并删除该元素,横向上应填写( )。
#include <iostream>
#include <queue>
using namespace std;
bool is_front_equal(std::queue<int>& q, int a) {
    bool is_equal = false;
    if (!q.empty()) {
        ———————————————————————— // 在此处填入代码
    }
    return is_equal;
}

{{ select(6) }}

  • is_equal = (q.front() == a);
  • is_equal = (q.front() == a); q.pop();
  • q.pop(); is_equal = (q.front() == a);
  • q.pop(); is_equal = (q.top() == a);
蜀ICP备2025119001号-1