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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:26:42+00:00 2026-06-18T14:26:42+00:00

I am a new learner and trying to build linked list. I am able

  • 0

I am a new learner and trying to build linked list. I am able to do so but I am trying to keep the pointer of root or first node so after building linked list I can read the list or execute the pattern matching but I am not able to do it successfully. Can you please help here?

#include <stdio.h>
#include <stdlib.h>

struct node {    
    int x;
    struct node *next;
};

int main(){

    int d;
    struct node *linked;
    struct node *head;

    linked = malloc (sizeof(struct node));

    head = linked;   <<<< As this is pointer, in while loop whenever malloc executes it changes the value of head as well.
    printf ("Location of head %p \n", head->next);

    d = 1;
    while (d>0){

        printf ("Enter the value of X: ");
        scanf ("%d", &linked->x);

        linked->next = malloc (sizeof(struct node));
        printf ("Location of linked %p \n", linked->next);

        printf ("Location of head %p \n", head->next);

        printf ("To enter further value enter non zero: ");
        scanf ("%d", &d);

        if (d==0)
            linked->next = NULL;
    }

    //linked->next = NULL;

    printf("Value of Next is %p\n", linked->next);        
    printf ("Location of head %p \n", head->next);        
}

Output:

MacBook-Air:cprog jimdev$ ./a.out

Location of head 0x7fff90ab952c <<<< this value should not be change
but here in sub sequent output it is.

Enter the value of X: 0

Location of linked 0x7ff0624039c0

Location of head 0x7ff0624039c0 <<<< different value then previous

To enter further value enter non zero: 3

Enter the value of X: 3

Location of linked 0x7ff0624039d0

Location of head 0x7ff0624039d0 <<<< different value then previous

To enter further value enter non zero: 0

Value of Next is 0x0

Location of head 0x0

I just tried this new one it does the printf of linked list elements as well, let me know if you guys think of any improvement. I know recursion is quick and neat way to achieve it but I wanted to try out something with while loop.

include

include

struct node {

    int x;
    struct node *next;
    };

int main () {

    int d, hold, i;
    struct node *list;
    struct node *head;
    struct node *current;



    list = (node *)malloc(sizeof(struct node));
    head = list;
    printf ("Location of list is %p \n", head);
    d = 1;

while (d>0){

    printf ("Enter the value of X: ");
    scanf ("%d", &list->x);
    printf ("Location of list is %p\n", list);
    current = (node *)malloc (sizeof (struct node));
    list->next = current;
    list = current;

    printf ("Location of head is %p\n", head);


    printf ("Enter zero to terminate the loop: ");
    scanf ("%d", &d);

    }
    list->next = NULL;
    printf ("Value of last next is %d\n", list->next);

    current = head;

    i = 1;
    while (current->next != 0){
          printf ("Location of list is %p \n", current);
          printf ("Value of linked list %d elements %d \n", i, current->x);
          current = current->next;
          i++;
          }

    scanf ("%d", &hold);

}

  • 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-18T14:26:43+00:00Added an answer on June 18, 2026 at 2:26 pm

    2 things:

    1. you are outputting head->next instead of just head

    2. you are not updating linked to linked->next

    The head node of a linked list is the first element of a list. head->next, when the linked list is well-formed, is the second element. Either way it shouldn’t be changing as the list is made after it is created.

    If you are not updating linked to be linked->next you are just throwing away the memory you just allocated in linked->next and not creating a list.

    This works:

    int d;
    struct node *linked;
    struct node *head;
    
    linked = malloc(sizeof(struct node));
    
    head = linked;
    printf ("Location of head %p \n", head);//changed head->next to head
    
    d = 1;
    while (d>0){
    
        printf ("Enter the value of X: ");
        scanf ("%d", &linked->x);
    
        linked->next = malloc (sizeof(struct node));
        printf ("Location of linked %p \n", linked->next);
    
        printf ("Location of head %p \n", head);// changed head->next to head
    
        printf ("To enter further value enter non zero: ");
        scanf ("%d", &d);
    
        linked = linked->next; //this was added
        if (d==0)
            linked->next = NULL;
    }
    

    Your code, since it wasn’t updating linked, kept head and linked equal. Your list looked like this:

    Node {x:next}

               -> {x2:NULL}
               -> {x3:NULL}
    {x1:node2} -> {x4:NULL}
        ^^            ^^
    (head and linked)   you're outputing this node both times  
    

    Since you don’t have any pointers to node2 or node3 you lose these nodes (a memory leak).

    You had it almost right. This is what a list generated with this code looks like:

    {x1:node2} -> {x2:node3} -> {x3:node4} -> {x4:node5}
       ^^           ^^                           ^^
      head        head->next                   linked
    

    update:

    I changed the diagram for your code to better reflect what is happening.

    The ptr->varname operator looks in location pointed to by ptr for the value of varname. So if the pointers are the same value then you are looking in the same space for the variables. In your case, head=linked means that if you set linked->next = ptr then head->next = ptr, and since linked is never updated to be linked->next (and therefore head->next) then you are just assigning and reassigning linked->next

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

Sidebar

Related Questions

I am self-learner and I am currently building (trying to build) a simple form
I am pretty new to MySQL and databases but I'm trying to learn :)
as a learner, and a new one to wordpress, i am trying to learn
I am an new learner of scala, and i am trying to run this
I am a new learner to php. I am trying to retrieve embeded information
I know php well. But I am a very new learner of php framework
I am new learner to jquery/js. I am having difficulties making a scroll tab
First, I'm not a python programmer. I'm an old C dog that's learned new
I am new to PL/SQL, I'm trying to execute the commands that I learned
I am trying to check for new orders in Magento and if they exist,

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.