Matt_Bro
Matt_Bro

Reputation: 14472

Having trouble implementing a linked list in c++

I am trying to implement a simple singly linked list of integers which are to be sorted upon insertion in Visual Studio c++ 2010 express.

The problem is that when I create a new node and call the .getValue() function on it, the correct number is returned, however somehow that is being lost when I try calling getValue() on a node already in the list. The node might not be inserted into the list correctly, however I can't find why that would be the case. Some other value which looks like a reference value or something is displayed instead of the correct value.

I added current to the watch window when debugging but was still unable to see any of my variables other than the give value to be inserted. I am new to visual studio so I'm not sure if I'm missing something there. Here is my code:

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

Here is the linked list:

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

Any guidance would be greatly appreciated!

Upvotes: 1

Views: 760

Answers (1)

When you create nodes in insert, you're allocating them off the stack, which means that they'll be lost after the function returns.

Get them off the heap with:

Node * newNode=new Node(value);

When you use:

Node newNode=Node(value);

You're allocating that object on the stack, which means that pointers:

&newNode

to it are only valid until that function returns. If you use heap memory this is no longer an issue, but it does mean that you have to implement a destructor for your list which goes through and deletes each node.

Upvotes: 2

Related Questions