【问题标题】:Having trouble implementing a linked list in c++在 C++ 中实现链表时遇到问题
【发布时间】:2011-12-30 17:41:59
【问题描述】:

我正在尝试实现一个简单的整数单链表,在插入 Visual Studio c++ 2010 express 时对其进行排序。

问题是,当我创建一个新节点并在其上调用 .getValue() 函数时,会返回正确的数字,但是当我尝试在列表中已经存在的节点上调用 getValue() 时,它会以某种方式丢失.该节点可能没有正确插入到列表中,但是我找不到为什么会这样。显示一些看起来像参考值或其他东西的其他值,而不是正确的值。

我在调试时向监视窗口添加了电流,但除了要插入的给定值之外,仍然无法看到我的任何变量。我是视觉工作室的新手,所以我不确定我是否遗漏了一些东西。这是我的代码:

#include "Node.h";
#include <iostream>

//namespace Linked{
//The first two constructors would be the first in the linked list.
Node::Node(void){
    value = 0;
    next = 0;
}
Node::Node(int setValue){
    value = setValue;
    next = 0;
}
Node::Node(int setValue,Node *nextNode){
    value = setValue;
    next = nextNode;
}
Node * Node::getNext(){
    return next;
}
void Node::setNext(Node newNext){
    next = &newNext;
}
int Node::getValue(){
    return value;
}
bool Node::isEqual(Node check){
    return value==check.getValue()&&next == check.getNext();
}

/*
int main(){
    int firstInt, secondInt;
    std::cin>>firstInt;
    Node first = Node(firstInt);
    std::cout<<"Enter second int: ";
    std::cin>>secondInt;
    Node second = Node(secondInt, &first);
    std::cout<<"Second: "<<second.getValue()<<"\nFirst: "<<(*second.getNext()).getValue();

    system("pause");
}*/

这里是链表:

    //LinkedList.cpp

    LinkedList::LinkedList(void)
    {
        head = 0;
        size = 0;
    }

    LinkedList::LinkedList(int value)
    {
        head = &Node(value);
        size = 1;
    }

    void LinkedList::insert(int value){
        if(head == 0){

            Node newNode = Node(value);
            head = &newNode;
            std::cout<<"Adding "<<(*head).getValue()<<" as head.\n";
        }else{
            std::cout<<"Adding ";
            Node current = *head;
            int numChecked = 0;
            while(size<=numChecked && (((*current.getNext()).getValue())<value)){
                current = (*(current.getNext()));
                numChecked++;
            }

            if(current.isEqual(*head)&&current.getValue()<value){
                Node newNode = Node(value, &current);
                std::cout<<newNode.getValue()<<" before the head: "<<current.getValue()<<"\n";
            }else{
                Node newNode = Node(value,current.getNext());
                current.setNext(newNode);
                std::cout<<newNode.getValue()<<" after "<<current.getValue()<<"\n";
            }

        }
        size++;
    }
    void LinkedList::remove(int){

    }
    void LinkedList::print(){
        Node current = *head;
        std::cout<<current.getValue()<<" is the head";
        int numPrinted = 0;
        while(numPrinted<(size-1)){
            std::cout<<(current.getValue())<<", ";
            current = (*(current.getNext()));
            numPrinted++;
        }
    }
    int main(){
        int a[5] = {30,20,25,13,2};
        LinkedList myList = LinkedList();
        int i;
        for(i = 0 ; i<5 ; i++){
            myList.insert(a[i]);
        }
        myList.print();
        system("pause");
    }

任何指导将不胜感激!

【问题讨论】:

    标签: visual-studio-2010 visual-c++ linked-list


    【解决方案1】:

    在插入中创建节点时,会将它们从堆栈中分配出去,这意味着它们将在函数返回后丢失。

    通过以下方式将它们从堆中删除:

    Node * newNode=new Node(value);
    

    使用时:

    Node newNode=Node(value);
    

    您正在堆栈上分配该对象,这意味着指针:

    &newNode
    

    仅在该函数返回之前有效。如果您使用堆内存,这不再是问题,但这确实意味着您必须为您的列表实现一个析构函数,该析构函数会遍历并删除每个节点。

    【讨论】:

    • 感谢您的帮助。我进行了建议的更改,现在我收到此错误:“错误 C2100:非法间接”每次我引用 *newNode 时。
    • 您根本不必使用*newNode。这样做会将newNode 指向的节点的内容复制到堆栈上的变量中。您应该使用-&gt;newNode 执行同时取消引用和成员访问,例如newNode-&gt;getValue()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多