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 7710543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:09:07+00:00 2026-06-01T01:09:07+00:00

Edit For simplicity: I just want to make the most basic possible cursor that

  • 0

Edit

For simplicity: I just want to make the most basic possible cursor that simply goes trough my list without changing in any way / shape or form my “begin”. I still want begin to be changed when I return something though, just not before. So is it possible to make a pointer to begin, go trough the list without changing anything except at the very end when I want to add a new node ?

Also Can I just go trough the list with a simple pointer that is not a “Node” ?

/Edit

I have a simple (singly) linked list to make as part of my homework. Of-course I also have a lot to do besides that, but after I get the list out of the way everything should be strait forward, but while I have been using C++ for a while (it was borland C++) a lot of what I knew is either half-forgotten or outdated. I have programmed in python for a while but all that means is that I keep getting frustrated with how pointers work in C++.

My problem is when I try to add a new Node to the list, my cursor behaves in an unusual manner, I will explain below:

EDIT: Ok, I changed the:

Node *cursor;
cursor = new Node
cursor = begin;

fiasco, but the result is the same, after the declaration cursor and begin both point at the same memory location (something like: 0x32ce8).

/EDIT

Node *add_node (Node *begin,string type, int sum, int ap_nr) // begin is the first node in the list
{
    // if first node is dummy node

    if (begin->ap_nr == -1)
        {
            begin->type = type;
            begin->ap_nr = ap_nr;
            begin->sum = sum;
            begin->next = 0;
            return begin;
        }

    // else create new node and insert it in sorted position

   else
    {
        // EDIT:
        Node *cursor = begin; // Same problem

        //if node should be inserted before first node (begin)

        if (ap_nr <begin->ap_nr)
        {
            cursor->ap_nr = ap_nr;
            cursor->type = type;
            cursor->sum = sum;
            cursor->next = begin;
            return cursor;
        }

Always when I debug, begin has a similar form: 0x32ce02, when I create my “cursor” it has a vastly different form (longer also), but when I do this : cursor = begin, then cursor becomes something like this 0x32df02.

However the problem is when I get to “if (ap_nr ap_nr)” then for absolutely no feasible reason cursor becomes: 0x32ce02 and “cursor -> next = begin” ensures an infinite loop. And no matter how many nodes I add this always happens so whenever I print the list it’s an infinite stream of the last added Node.

Am I doing something wrong ? is it the declaration or the alocation, creation ? something ?

Also if I have a pointer *begin somewhere in another module, and with this function I return a new begin … that should work, right ?

P.S. I would also appreciate a simple counter solution (another way to do this if mine is just not good)

Also I should point out how I made my list. It is just a simple linking of nodes:

struct Node {
    string type;
    int ap_nr;
    int sum;
    Node *next;
};
  • 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-06-01T01:09:08+00:00Added an answer on June 1, 2026 at 1:09 am

    The pointers cursor and begin point to the same memory location because you explicitly say so: Node* cursor = begin; literally says “create a pointer variable named cursor which points to the same location as begin.” So it is no surprise that it does.

    Edit: Removed advice based on a wrong guess what the code is intended to do and changed it to more applicable advice

    From the comment, I now understand that you want to insert a node at a position so that the field ap_nr is increasing in the resulting list, assuming it is increasing originally (if that still isn’t right, please state clearly what you do want).

    For that case, the initialization of cursor is of course now correct. However it is not correct to modify the object cursor points to: You want to insert a new node before that node. But for that you have to make several changes:

    First, you need another pointer variable, which holds a pointer to a newly created node, like

    Node* new_node = new Node;
    

    And then you have to insert that node into the list. That is, instead of cursor->ap_nr=ap_nr; etc. you have to use new_node->ap_nr=ap_nr; etc. Also, the node following it is of course not the first node of the list (pointed to by first) but the one you just found (pointed to by current).

    However, now you have a problem: You have to insert that new node into the list, which means you have to modify the next pointer of the previous node (but to point not to begin, but to the newly created node!). But you don’t have any more the pointer to the previous node, because your list is singly-linked, that is, you don’t have a pointer from the found element to the previous element. But to insert the element, you have to change the next.

    However what you do have is a pointer to the next node. Therefore a better strategy is to have your cursor point to the previous node, and then consistently use cursor->next instead of cursor (except when moving cursor, of course). That way you can, after you’ve set new_node->next, write cursor->next = new_node;

    Other things which are missing from your code are checks that current is not null (which it will be at the end of the list) and ther code acrtually moving your cursor forward (which belongs into an else part of your inner if).

    Actually I now notice that your blocks are not closed, so the moving-forward code might be there in your actual code.

    At the end, some general advice: You probably would have an easier time writing that code if you modularized the code of your function: Have one function to insert a new node after a given one (changing only the next pointers, and returning a pointer to the newly inserted code), have another function to find the node after which the new node should be inserted, and have your function add_node only call those other functions. That way, in each function you can concentrate on one of the sub-problems.

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

Sidebar

Related Questions

EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
Edit: For simplicity, and in order to try and make this question and the
Edit: From another question I provided an answer that has links to a lot
I really want to create a stunning-looking GUI desktop application that looks like, for
Write a function that counts the number of elements in the list that are
EDIT: For the sake of simplicity: I've got a simple UserJS script (FF/Chrome: Greasemonkey,
I have a list of numbers that i would like to send out onto
You may have noticed that we now show an edit summary on Community Wiki
I wonder what's the most effective way to render ANYTHING that might come from
I'm trying to make a simple forum just to get the hang of the

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.