无法在c ++中实现链接列表

时间:2011-12-30 17:42:00

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

我正在尝试实现一个简单的单链接整数列表,这些整数将在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");
    }

非常感谢任何指导!

1 个答案:

答案 0 :(得分:2)

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

让他们离开堆:

Node * newNode=new Node(value);

使用时:

Node newNode=Node(value);

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

&newNode

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