【问题标题】:C++: First character in queue is wrongC++:队列中的第一个字符是错误的
【发布时间】:2013-11-18 14:26:22
【问题描述】:

我目前正在为学校做一个作业,说我应该创建一个队列。它似乎正在工作。唯一的问题是我的队列开头有一个意外的字符。我使用 CQueue 类从队列中推送和弹出值。我必须使用这个类而不是 std::queue 或 deque 之类的东西。

class CQueue
{
private:
char *bottom_;
char *top_;
int size_;
public:
CQueue(int n = 20){
    bottom_ = new char[n];
    top_ = bottom_;
    size_ = n;
}

void push(char c){
    *top_ = c;
    top_++;
}

int num_items() {
    return (top_ - bottom_ );
}

char pop(){
    bottom_++;
    return *bottom_;
}

void print(){
    cout << "Queue currently holds " << num_items() << " items: " ;
    for (char *element=top_; element > bottom_; element--) {
        cout << " " << *element;
    }
    cout << "\n";
}

这是我的主要方法:

int main(){


CQueue q(10);

q.push('s');q.push('t');q.push('a');q.push('c');q.push('k');
q.print();
cout << "Popped value is: " << q.pop() << "\n";
q.print();
q.push('!');
q.push('?');
cout << "Popped value is: " << q.pop() << "\n";
q.print();

while (!q.empty()) q.pop();
if (q.num_items() != 0) {
    cout << "Error: Stack is corrupt!\n";
}
q.print();
cout << "End of program reached\n"<< endl;
return 0;

当我运行此代码时,队列被填满,但 *bottom_ 被替换为 '=' 符号。这是我的输出:

Queue currently holds 5 items:  ═ k c a t
Popped value is: t
Queue currently holds 4 items:  ═ k c a
Popped value is: a
Queue currently holds 5 items:  ═ ? ! k c
Queue currently holds 0 items:
End of program reached

我一直在努力解决这个问题,所以我希望你能对这个问题有所了解!

【问题讨论】:

    标签: c++ pointers queue output


    【解决方案1】:

    当您的push() 被定义时,*top_ 不在队列中。它是队列末尾之后的一个元素。因此,您应该定义您的 print() 以从 top_ - 1 迭代。

    正如@stellarossa 提到的,您应该在递增之前返回bottom_ 指向的字符。也就是说,

    char pop() { return *(bottom_++); }
    

    【讨论】:

      【解决方案2】:
      char pop(){
          bottom_++;
          return *bottom_;
      }
      

      您正在递增指针,然后返回值。应该是相反的。

      【讨论】:

        【解决方案3】:

        你用的是数组还是链表?

        保持简单并使用带有count 变量的数组。

        #include <iostream>
        using namespace std;
        class CQueue
        {
        private:
        char * q;
        int size_;
        int count;
        public:
        CQueue(int n = 20){
            q = new char[n];
            size_ = n;
            count = 0;
        }
        
        void push(char c){
            assert(count != size);
            q[count] = c;
            count++;
        }
        
        int num_items() {
            return count;
        }
        
        char pop() {
            assert(count != 0);
            char ret = q[count-1];
            count--;
            return ret; 
        }
        
        void print(){
            cout << "Queue currently holds " << num_items() << " items: " ;
            for (int i = 0; i < count; i++) {
                cout << " " << q[i];
            }
            cout << "\n";
        }
        

        【讨论】:

          【解决方案4】:

          我的朋友至少有两个错误。

          1) print() 方法开始打印 *top,它是最后一个成员之后的 1。应该是:

          for (char *element=top_-1; element >= bottom_; element--) {
              cout << " " << *element;
          }
          

          2) pop() 方法错误: 应该是:

          char pop(){
              return (top_ > bottom_) ? *top_-- : 0;
          }
          

          【讨论】:

          • 你对第一个是完全正确的!非常感谢。但我决定使用@stellarossa 的 pop 方法解决方案。
          猜你喜欢
          • 2020-09-08
          • 1970-01-01
          • 1970-01-01
          • 2016-07-19
          • 1970-01-01
          • 2011-10-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多