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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:24:18+00:00 2026-05-27T21:24:18+00:00

I was practicing single linked list in c++ (practicing how to find the beginning

  • 0

I was practicing single linked list in c++ (practicing how to find the beginning node of the circular list), but found the use of operator -> very confusing. I’m using Visual studio 2010 C++ Express

This works perfectly: head->append(2)->append(3)->append(4)->append(5)

But this doesn’t work (to create a circular linked list): head->append(2)->append(3)->append(4)->append(5)->append(head->next)

When I jump in this method and debug, it seems head->next is not passed correctly into the method.

But this works:

  1. Node* tail=head->append(2)->append(3)->append(4)->append(5);
    tail->append(head->next);
  2. Or after I change return c->next to return head in the two methods, head->append(2)->append(3)->append(4)->append(5)->append(head->next) also works.

What am I missing here? Thank you!

Details of my code is as follows:

void main(){
    Node* head=new Node(1);
    Node* tail=head->append(2)->append(3)->append(4)->append(5)->append(head->next);
    cin.get();
}

class Node{
public:
    Node* next;
    int data;
    bool marked;

    Node(int d){
        data=d;
        marked=false;
        next=NULL;
    }

    Node* append(int d){
        Node* c=this;
        while(c->next!=NULL){
            c=c->next;
        }
        c->next=new Node(d);
        return c->next;
    }

    Node* append(Node* n){
        Node* c=this;
        while(c->next!=NULL){
            c=c->next;
        }
        c->next=n;
        return c->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-05-27T21:24:19+00:00Added an answer on May 27, 2026 at 9:24 pm

    You are experiencing undefined behavior.

    The problem is that you are expecting head->next to be evaluated at a particular time (right before calling the last append(). But that is not guaranteed.

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

Sidebar

Related Questions

i'm practicing c and looking to use visual studio. but can I write console
So I was practicing some linked list problems and I keep getting mixed up
I am practicing using inner classes but am having difficulty with a homework question:
I just started practicing TDD in my projects. I'm developing a project now using
I'm practicing on Django and using some online tutorial to build a web blog.
I was practicing the dynamic programming problem on SPOJ. But I have no idea
I'm practicing some XSL and using this XML document as a simple example: <?xml
Though there can be many but as i am very new to python so
Been practicing with those system calls, but I stucked into this code: #include <stdio.h>
I am practicing with PHP and AJAX but I have some problems! I'm trying

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.