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

  • Home
  • SEARCH
  • 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 5848967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:56:02+00:00 2026-05-22T12:56:02+00:00

this is my code. i wanted to print all my list datas. but i

  • 0

this is my code. i wanted to print all my list datas. but i cant cause when i write while(llist->next != NULL) llist->next is NULL but i don’t know why. please help me 🙂

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

struct rame
{
    int data;   
    struct rame *next;  
};
int main()
{ 
    struct rame *llist;
    llist = (rame*)malloc(sizeof(struct rame));
    llist->data = 10;
    llist->next = llist; 
    llist->next->data = 15;
    llist->next->next->data = 20;
    llist->next->next->next->data = 25; 
    llist->next->next->next->next = NULL;
    printf("test\n");
    if(llist->next == NULL)
    printf("%d\n",llist->data);
    else
    while(llist->next != NULL)
    {
         printf("%d\n",llist->data);          
         llist = llist->next;
    } 
 system("pause");
 return 0;   
}  

hey i did everithing but my LOOP doesnt print the last data. help me 🙁

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

struct rame
{
    int data;   
    struct rame *next;  
};
int main()
{ 
    struct rame *llist;
    llist = (rame*)malloc(sizeof(struct rame));
    llist->data = 10;
    llist->next = (rame*)malloc(sizeof(struct rame));
    llist->next->data = 15;
    llist->next->next = (rame*)malloc(sizeof(struct rame));
    llist->next->next->data = 20;
    llist->next->next->next = (rame*)malloc(sizeof(struct rame));
    llist->next->next->next->data = 25; 
    llist->next->next->next->next = (rame*)malloc(sizeof(struct rame));
    llist->next->next->next->next =  NULL;
    printf("test\n");
    while(llist->next != NULL)
    {
         printf("%d\n",llist->data);          
         llist = llist->next;
    } 
 system("pause");
 return 0;   
}          
  • 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-22T12:56:02+00:00Added an answer on May 22, 2026 at 12:56 pm

    In your code

    llist = (rame*)malloc(sizeof(struct rame));
    llist->data = 10;
    

    allocates one memory location to llist , and the data of this location is assigned 10
    Next you do:

    llist->next = llist; 
    llist->next->data = 15;
    

    The first line assigns the next link of the llist to itself, which makes the below state of the list

    +--------+-----+------+
    | llist  |  10 | next |-----+
    +--------+-----+------+     |
        ^                       |
        |                       |
        +-----------------------v
    

    Now executing llist->next points to llist itself and thus llist->next->data simply addresses to the list->data so the value 10 is changed.

    In the other linking you have done, how many number of times ->next->next->....->next you use does not matter as it will point to the same location.

    To test the thing print the address of llist and the address of llist->next. You have address of llist and llist->next identical. This means llist->data , and llist->next->data are the same. and any number of indirection through next field is the same. So after the final assignment the llist->data is 25, other values previously assigned is overwritten by this.

    At the last step you do:
    llist->next->next->next->next = NULL;

    This actually makes the above diagram to:

    +--------+-----+------+
    | llist  |  10 | next |----->NULL
    +--------+-----+------+
    

    This results in if(llist->next == NULL) condition to be true and thus only the first node’s contents in printed, which is the last value you have inserted = 25

    To get the proper effect, you need to allocate a new node for each next link, like for example in the context of your code:

    llist = (rame*)malloc(sizeof(struct rame));
    llist->data = 10;
    
    llist->next = (rame*)malloc(sizeof(struct rame));  // we allocate a new location which 
                                                   // we point to with the initial llist
    llist->next->data = 15;                            // this will now set the data to 15 of
                                                   // the node which we allocated on 
                                                   // the previous step
    

    In this case the diagram becomes

    +--------+-----+------+      +-----------------+----+------+
    | llist  |  10 | next |----->| newly allocated | 15 | next |
    +--------+-----+------+      +-----------------+----+------+
    

    Now you can do linking in a chain fashion by doing llist->next->next = (rame*)malloc(sizeof(struct rame)); and llist->next-next->data = 5486 etc.

    Recommended is instead of writing a chain of nexts you can store the address of the last node temporarily in a temporary variable say temp and access the data element through them, like:

    llist = (rame*)malloc(sizeof(struct rame));
    temp = llist;
    temp->data = 5
    temp->next = (rame*)malloc(sizeof(struct rame));
    temp = temp->next; //now temp contains the address of the newly allocated node above
    temp->data = 10;
    temp->next = (rame*)malloc(sizeof(struct rame));
    temp = temp->next;
    temp->data = 15;
    .
    .
    

    Although actually you should link these with a loop with something like the following structure

    list_head = (rame*)malloc(sizeof(struct rame));
    temp = list->head;
    while (some condition)
    {
      temp->next = (rame*)malloc(sizeof(struct rame));
      temp = temp->next;
      //if this is the last node,we assign null to identify this that there is no more nodes after this.
      temp->next = NULL;
      temp->data = value;
    }
    

    You need to store the list head pointer in some variable, so that with that you can traverse the entire list by following the links. Note that if you loose that head pointer, then you will not be able to get the list.

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

Sidebar

Related Questions

First, sorry this is so long. I probably don't need all the code, but
This code in JS gives me a popup saying i think null is a
This code does not seem to compile, I just need to write something to
This code produces a FileNotFoundException, but ultimately runs without issue: void ReadXml() { XmlSerializer
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We
This code is executed by many way. When it's executed by the form button
This code leads to undefined behavior: void some_func() { goto undefined; { T x

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.