在c++中实现链表时遇到麻烦



我正在尝试实现一个简单的整数单链表,这些整数将在Visual Studio c++ 2010 express中插入后进行排序。

问题是,当我创建一个新节点并在其上调用。getValue()函数时,返回正确的数字,然而,当我尝试在列表中已经存在的节点上调用getValue()时,不知何故正在丢失。节点可能无法正确插入到列表中,但是我不知道为什么会这样。其他一些看起来像参考值的值被显示,而不是正确的值。

我在调试时将current添加到watch窗口,但仍然无法看到除要插入的给定值之外的任何变量。我是新来的视觉工作室,所以我不确定我是否错过了一些东西。下面是我的代码:

#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");
    }

任何指导将非常感激!

当您在insert中创建节点时,您将从堆栈中分配它们,这意味着在函数返回后它们将丢失。

将它们从堆中移除:

Node * newNode=new Node(value);

使用

Node newNode=Node(value);

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

&newNode

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

相关内容

  • 没有找到相关文章

最新更新