【问题标题】:Stack Linked List pop() and isEmpty() not working, no idea where the bug isStack Linked List pop() 和 isEmpty() 不起作用,不知道错误在哪里
【发布时间】:2020-08-13 20:20:11
【问题描述】:

我正在处理一个项目,但无法克服几个错误。这是我的代码。

main()

#include <iostream>
#include <cstddef>
#include "src\DynStack.cpp"
#include "include\DynStack.h"
#include "src\DynQue.cpp"
#include "DynQue.h"
#include <fstream>

using namespace std;

int main ()
{
    ifstream inputFile;
    ofstream outputFile;

    DynStack<char>cstack;

    inputFile.open("input.txt");

    if (!inputFile)
    {
        cout << "Error. No input file found." << endl;
    }
    else
    {
        char x;

        while (!inputFile.eof())
        {
            inputFile.get(x);

            cstack.push(x);

            cout << x;
        }

        inputFile.close();


        outputFile.open("output_reverse.txt");

        char y;

        int count = 1;

        while (cstack.isEmpty() == NULL)
        {
            cstack.pop(y);

            cout << y;

            count++;

        }

        outputFile.close();
    }



    cout << "Program completed." << endl;

    return 0;
}

DynStack.cpp

#include "DynStack.h"
#include <cstddef>
#include <iostream>

using namespace std;

template<typename T>
DynStack<T>::DynStack()
{
    top = NULL;
}

template<typename T>
void DynStack<T>::push(T val)
{
    StackNode *nodePtr; // ptr to traverse thru the stack
    StackNode *previousNode; // ptr to connect higher node to node below it

    StackNode *newNode;
    newNode = new StackNode; // makes a new StackNode
    newNode->value = val;
    newNode->next = NULL;

    if (top == NULL) // If the stack is empty
    {
        top = newNode; // Make newNode the first node;
    }
    else
    {
        nodePtr = top; // make our ptr = top

        newNode->next = nodePtr; // make the our new top node, newNode, point to the node below it

        top = newNode; // newNode is our new top of the stack

        delete nodePtr;
    }
}

template <typename T>
void DynStack<T>::pop(T& val)
{
    StackNode *nodePtr; // makes a nodePtr to traverse the stack

    nodePtr = top; // set nodePtr to point to top of stack

    if (top == NULL) // If stack is empty
    {
        cout << "Error. Stack is empty." << endl; // Provide error message.
        return;
    }
    else if (top->next == NULL) // If there is only one item in stack
    {
        val = top->value; // Make return value whatever the previous top was

        delete top; // Delete top

        top = NULL; // make top equal to null
    }
    else // If there is more than one item in stack
    {
            val = top->value; // Return value whatever previous top was

            nodePtr = nodePtr->next; // nodePtr point to node below top node

            delete top; // delete top node

            top = nodePtr; // make nodePtr our new top

            delete nodePtr; // delete the nodePtr
        }
}

template <typename T>
bool DynStack<T>::isEmpty()
{
    if (top == NULL) // If top node is null return true
    {
        return true;
    }
    if (top != NULL) // If top node is not null return false
    {
        return false;
    }
}



template <typename T>
DynStack<T>::~DynStack()
{
    //dtor
}

DynStack.h

#ifndef DYNSTACK_H
#define DYNSTACK_H

template <typename T>
class DynStack
{
    private:
        struct StackNode
        {
            T value;
            StackNode *next;
        };



    public:
        DynStack();
        ~DynStack();
        void push(T);
        void pop(T&);
        bool isEmpty();

        StackNode *top;
};

#endif // DYNSTACK_H

这个程序的想法是我们输入一个输入文件“input.txt”说“这是原始文件”。并将其反向输出到另一个文本文件。所以“.elif lanigiro eht si sihT”。

我的问题是:

首先,当我运行这段代码时,它会打印出 while(!inputFile.eof()) 就好了。当它运行第二个 while 循环时,while(cstack.isEmpty() == NULL),它会无限打印。我认为我的 pop() 或 isEmpty() 函数一定有问题,但我只是看不出我缺少什么。也许我在 pop() 中将 top 设置为 NULL 错误,但我看不出要更改什么。可能 isEmpty() 有问题,但是看起来很简单我不知道要添加什么。

更重要的是,当我尝试运行它时,第二个 while 循环的输出似乎每次都不同。有时它是“.e.e.e.”重复。有时它的“.el.el.el”重复。有时它是“.elif.elif.elif”重复。 “.elif.elif.elif”重复是我得到的最接近正确输出的结果,所以输出或保存空格可能有问题??

我不知道。如果有人能看出问题所在并向我解释,我将非常感激你。谢谢大家。

【问题讨论】:

  • isEmpty 是 bool 为什么要和 null 比较?
  • 这段代码有很多问题。打开编译器警告,然后退出调试器。

标签: c++ function class linked-list stack


【解决方案1】:

您的push 可能有错误:

template<typename T>
void DynStack<T>::push(T val)
{
    StackNode *nodePtr; // ptr to traverse thru the stack
    StackNode *previousNode; // ptr to connect higher node to node below it

    StackNode *newNode;
    newNode = new StackNode; // makes a new StackNode
    newNode->value = val;
    newNode->next = NULL;

    if (top == NULL) // If the stack is empty
    {
        top = newNode; // Make newNode the first node;
    }
    else
    {
        nodePtr = top; // make our ptr = top

        newNode->next = nodePtr; // make the our new top node, newNode, point to the node below it

        top = newNode; // newNode is our new top of the stack

        delete nodePtr;
    }
}

您正在删除nodePtr,它目前持有top,a 的值,因此您正在删除所有列表

【讨论】:

  • 哇,谢谢@Berto99!我可能会通过反复试验发现这一点,但谁知道这需要多长时间。既然你已经说过了,我可以概念化为什么会发生这种情况......我还在学习指针是如何工作的,它们对我来说有点挑战。
  • @cstrike2 不要担心我们所有人都曾经在我们的生活中去过那里
猜你喜欢
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
  • 2015-04-04
  • 2015-07-13
相关资源
最近更新 更多