Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么当我尝试将所有字符串转移到自定义队列模板中并尝试打印时,它只打印队列中的最后一个?_C++_Queue - Fatal编程技术网

C++ 为什么当我尝试将所有字符串转移到自定义队列模板中并尝试打印时,它只打印队列中的最后一个?

C++ 为什么当我尝试将所有字符串转移到自定义队列模板中并尝试打印时,它只打印队列中的最后一个?,c++,queue,C++,Queue,说实话,这是我正在努力完成的任务。我们必须做的基本事情是创建一个没有STL的堆栈和队列,然后使用STL创建堆栈和队列。我几乎完成了自定义堆栈的创建,它工作得非常好。然而,对于队列,每当我尝试将字符串移入队列并打印出来时,控制台将只打印最后一个被移入队列的字符串。最重要的是,每当我试图用我拥有的代码解除进入队列的最后一个内容时,我最终会遇到一个读访问冲突,我完全无法解决这个问题 如果你不介意的话,你能看一下我的代码并帮助我理解是什么导致了这个错误,我队列中的最后一个条目是唯一打印出来的吗?提前谢谢

说实话,这是我正在努力完成的任务。我们必须做的基本事情是创建一个没有STL的堆栈和队列,然后使用STL创建堆栈和队列。我几乎完成了自定义堆栈的创建,它工作得非常好。然而,对于队列,每当我尝试将字符串移入队列并打印出来时,控制台将只打印最后一个被移入队列的字符串。最重要的是,每当我试图用我拥有的代码解除进入队列的最后一个内容时,我最终会遇到一个读访问冲突,我完全无法解决这个问题

如果你不介意的话,你能看一下我的代码并帮助我理解是什么导致了这个错误,我队列中的最后一个条目是唯一打印出来的吗?提前谢谢

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

    struct Node {
        //create a node struct
        string data;
        Node *next;
    };

    class Stack {
    public:
        Stack();
        ~Stack();
        void push(string a);
        string pop();
        string toString();
        bool isEmpty();
    private:
        Node * top;
    };

    class Queue {
    public: 
        Queue();
        ~Queue();
        void shift(string a);
        string unshift();
        string toString();
        bool isEmpty();
    private:
        Node * top;
        Node * bottom;
        int count;
    };

    Stack::Stack() {
        //initializes stack to be empty
        top = NULL;
    }
    Queue::Queue() {
        //initializes stack to be empty
        top = NULL;
    }

    Stack::~Stack() {
        //deconstructor to delete all of the dynamic variable 
        if (top == NULL) {
            cout << "Nothing to clean up" << endl;
        }
        else {
            cout << "Should be deleting..." << endl;
        }
    }
    Queue::~Queue() {
        //deconstructor to delete all of the dynamic variable 
        if (bottom == NULL) {
            cout << "Nothing to clean up" << endl;
        }
        else {
            cout << "Should be deleting..." << endl;
        }
    }

    void Stack::push(string a) {
        //Need a new node to store d in
        Node *temp = new Node;
        temp->data = a;
        temp->next = top;//point the new node's next to the old top of the stack

        top = temp;//point top to the new top of the stack
    }
    void Queue::shift(string a) {
        //Need a new node to store d in
        Node *temp = new Node;
        temp->data = a;
        temp->next = NULL;//point the new node's next to the old top of the stack
        if (isEmpty()) {
            top = temp;
        }
        else {
            top->next = temp;
            count++;
        }

        top = temp;//point top to the new top of the stack
    }

    string Stack::pop() {
        if (!isEmpty()) {
            string value = top->data;
            Node *oldtop = top;
            top = oldtop->next;
            delete oldtop;
            return value;
        }
        else {
            cout << "You can't pop from an empty stack!" << endl;
            exit(1);
        }
    }
    string Queue::unshift() {
        if (isEmpty()) {
            cout << "You can't unshift an empty Queue!" << endl;
            exit(1);
        }
        else{
            Node *oldbot = top;
        if (top == bottom) {
            top = NULL;
            bottom = NULL;
        }
        else {
            string value = top->data;
        }
            delete oldbot;
            count--;
        }
    }

    string Stack::toString() {
        string result = "top ->";
        if (isEmpty()) {
            result = result + "NULL";
            return result;
        }
        else {
            Node *current = top;
            while (current != NULL) {
                result = result + current->data + "->";
                current = current->next;
            }
            result = result + "(END)";
            return result;

        }
    }
    string Queue::toString() {
        string result = "top ->";
        if (isEmpty()) {
            result = result + "NULL";
            return result;
        }
        else {
            Node *current =top;
            while (current != NULL) {
                result = result + current->data + "->";
                current = current->next;
            }
            result = result + "(END)";
            return result;

        }
    }

    bool Stack::isEmpty() {
        return(top == NULL);
    }
    bool Queue::isEmpty() {
        return(top == NULL);
    }

int main()
{
    Stack *s = new Stack();
    cout << "Output when empty: " << endl << s->toString() << endl;
    s->push("Cheeseburger");
    s->push("Pizza");
    s->push("Large coffee");
    s->pop();
    cout << "Output when not empty: " << endl << s->toString() << endl;
    delete s;

    cin.get();

    Queue *b = new Queue();
    cout << "Output when empty: " << endl << b->toString() << endl;
    b->shift("Cheeseburger");
    b->shift("Pizza");
    b->shift("Large coffee");
    cout << "Output when not empty: " << endl << b->toString() << endl;
    b->unshift();
    delete b;
    cin.get();

}
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
结构节点{
//创建一个节点结构
字符串数据;
节点*下一步;
};
类堆栈{
公众:
堆栈();
~Stack();
空推(a串);
字符串pop();
字符串toString();
bool是空的();
私人:
节点*顶部;
};
类队列{
公众:
队列();
~Queue();
无效移位(字符串a);
字符串unshift();
字符串toString();
bool是空的();
私人:
节点*顶部;
节点*底部;
整数计数;
};
Stack::Stack(){
//将堆栈初始化为空
top=NULL;
}
队列::队列(){
//将堆栈初始化为空
top=NULL;
}
堆栈::~Stack(){
//解构器删除所有动态变量
if(top==NULL){
下一步;
}
结果=结果+“(结束)”;
返回结果;
}
}
字符串队列::toString(){
字符串结果=“顶部->”;
if(isEmpty()){
结果=结果+“空”;
返回结果;
}
否则{
节点*当前=顶部;
while(当前!=NULL){
结果=结果+当前->数据+“->”;
当前=当前->下一步;
}
结果=结果+“(结束)”;
返回结果;
}
}
bool Stack::isEmpty(){
返回(top==NULL);
}
bool队列::isEmpty(){
返回(top==NULL);
}
int main()
{
Stack*s=新堆栈();
推不动(“大咖啡”);
s->pop();

您必须在Queue::shift method-

top = temp;

bottom
始终为空,因为您从未设置过它。顺便说一句,这是学习使用调试器的完美方式。它允许您在运行时检查每个变量,并逐步检查代码。在
Queue::shift
中,您通过执行
temp->next=NULL;
top=temp;来丢弃所有当前队列内容de>。我建议您使用笔和纸绘制
shift
unshift
中的所有指针和节点的结果。