Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7001767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:50:41+00:00 2026-05-27T20:50:41+00:00

I am trying to implement a simple singly linked list of integers which are

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:50:42+00:00Added an answer on May 27, 2026 at 8:50 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to create a simple single linked list in python. (i know there
I'm trying to implement a simple horizontal navigation menu that just shows a single
I am trying to implement a simple request to Wikipedia's API using AJAX (XMLHttpRequest).
I am trying to implement a simple authorization strategy for my Wicket application. I
I am now trying to implement a simple HTML webpage scraper using Java.Now I
I've been trying to implement a simple component-based game object architecture using Objective-C, much
I'm new to Flex SDK and trying to implement a simple project using Doug
My first try of MVC. Am trying to implement a simple example. Inspiration from
I'm still learning about Objective-C memory management. I'm trying to implement several simple classes
We're trying to determine how to implement a simple plugin framework for a service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.