程序中的逻辑错误

时间:2015-03-12 15:59:47

标签: c++

我尝试在各个阶段使用cout来计算,但徒劳无功。在刷新反向列表中的前两个值后,程序崩溃。它打印一个垃圾值作为第三个值,在它打印最后两个值之前,它会崩溃。

#include <iostream>

using namespace std;

class LLStack {
    public:
        struct Node {
            int data;
            Node* next;
            Node(int n) {
                data = n;
                next = 0;
            }
            Node(int n, Node* node) {
                data = n;
                next = node;
            }
        };
        LLStack();
        LLStack(const LLStack&);
        LLStack& operator = (const LLStack&);
        ~LLStack();
        void push(int);
        int pop();
        int top();
        bool isEmpty();
        void flush();

    private:
        Node* head;

};

LLStack::LLStack() {
    head = 0;
}

LLStack::LLStack(const LLStack& s) {
    head = new Node(NULL);
    head->data = s.head->data;
    if (s.head->next != NULL) {
        head->next = new Node(*(s.head->next));
    }
    else {
        head->next = new Node(NULL);
    }

}

LLStack::~LLStack() {
    this->flush();
}

LLStack& LLStack::operator = (const LLStack& s) {
    this->head = new Node(NULL);
    this->head->data = s.head->data;
    if (s.head->next != NULL) {
        this->head->next = new Node(*(s.head->next));
    }
    else {
        this->head->next = new Node(NULL);
    }

    return *this;
}

void LLStack::push(int x) {
    if (head == 0) head = new Node(x);
    else {
        Node* temp = new Node(x, head);
        head = temp;
    }
}

int LLStack::pop() {
    if (head == 0) {
        cout << "\n\nNo elements to pop\n\n";
        return -1;
    }
    else {
        Node* temp = head;
        int n = temp->data;
        head = temp->next;
        delete temp;
        return n;
    }
}

int LLStack::top() {
    if (head == 0) {
        cout << "\n\nNo elements in the stack\n\n";
        return -1;
    }
    else {
        return head->data;
    }
}

bool LLStack::isEmpty() {
    return (head == 0);
}

void LLStack::flush() {

    if (head == 0) {
        cout << "\n\nNo elements in the Stack to flush\n\n";
        return;
    }
    cout << "\n\nFlushing the Stack:  ";
    Node* temp = 0;
    while (head != 0) {
        temp = head;
        cout << temp->data << "  ";
        head = head->next;
        delete temp;
    }
    cout << endl << endl;
}

void reverseStack(LLStack& s1) {
    LLStack s2;
    while (!s1.isEmpty()) {
        s2.push(s1.pop());
    }
    s1 = s2;
}

int main() {

    LLStack s;
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    s.push(5);
    reverseStack(s);
    cout << "\n\nFlushing s:\n";
    s.flush();
    return 0;
}

我尝试在各个阶段使用cout来计算,但徒劳无功。在刷新反向列表中的前两个值后,程序崩溃。它打印一个垃圾值作为第三个值,在它打印最后两个值之前,它会崩溃。

#include <iostream>

using namespace std;

class LLStack {
    public:
        struct Node {
            int data;
            Node* next;
            Node(int n) {
                data = n;
                next = 0;
            }
            Node(int n, Node* node) {
                data = n;
                next = node;
            }
        };
        LLStack();
        LLStack(const LLStack&);
        LLStack& operator = (const LLStack&);
        ~LLStack();
        void push(int);
        int pop();
        int top();
        bool isEmpty();
        void flush();

    private:
        Node* head;

};

LLStack::LLStack() {
    head = 0;
}

LLStack::LLStack(const LLStack& s) {
    head = new Node(NULL);
    head->data = s.head->data;
    if (s.head->next != NULL) {
        head->next = new Node(*(s.head->next));
    }
    else {
        head->next = new Node(NULL);
    }

}

LLStack::~LLStack() {
    this->flush();
}

LLStack& LLStack::operator = (const LLStack& s) {
    this->head = new Node(NULL);
    this->head->data = s.head->data;
    if (s.head->next != NULL) {
        this->head->next = new Node(*(s.head->next));
    }
    else {
        this->head->next = new Node(NULL);
    }

    return *this;
}

void LLStack::push(int x) {
    if (head == 0) head = new Node(x);
    else {
        Node* temp = new Node(x, head);
        head = temp;
    }
}

int LLStack::pop() {
    if (head == 0) {
        cout << "\n\nNo elements to pop\n\n";
        return -1;
    }
    else {
        Node* temp = head;
        int n = temp->data;
        head = temp->next;
        delete temp;
        return n;
    }
}

int LLStack::top() {
    if (head == 0) {
        cout << "\n\nNo elements in the stack\n\n";
        return -1;
    }
    else {
        return head->data;
    }
}

bool LLStack::isEmpty() {
    return (head == 0);
}

void LLStack::flush() {

    if (head == 0) {
        cout << "\n\nNo elements in the Stack to flush\n\n";
        return;
    }
    cout << "\n\nFlushing the Stack:  ";
    Node* temp = 0;
    while (head != 0) {
        temp = head;
        cout << temp->data << "  ";
        head = head->next;
        delete temp;
    }
    cout << endl << endl;
}

void reverseStack(LLStack& s1) {
    LLStack s2;
    while (!s1.isEmpty()) {
        s2.push(s1.pop());
    }
    s1 = s2;
}

int main() {

    LLStack s;
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    s.push(5);
    reverseStack(s);
    cout << "\n\nFlushing s:\n";
    s.flush();
    return 0;
}

请运行它,你就会明白自己。

1 个答案:

答案 0 :(得分:0)

问题在于:

void reverseStack(LLStack& s1) {
    LLStack s2;
    while (!s1.isEmpty()) {
        s2.push(s1.pop());
    }
    s1 = s2;
}

在这里,您将创建本地堆栈&#34; s2&#34;。当s2超出范围时,它会调用析构函数并刷新整个堆栈。更好的是你应该从析构函数中删除flush调用或创建全局&#34; s2&#34;。最好的选择是实现其他逻辑来反转堆栈。