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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:50:41+00:00 2026-06-06T05:50:41+00:00

#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; void insert( struct

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

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

void insert( struct node *q,int num)
{
    struct node *temp;

    if( q == NULL)
    {
        q = (struct node*)malloc(sizeof(struct node));
        q->data = num;
        q->next = NULL;
    }
    else
    {
        temp = q;
        while( temp != NULL)
        {
            temp = temp->next;
        }
        temp = (struct node*)malloc(sizeof(struct node));
        temp->data = num;
        temp->next = NULL;
    }
}

void display(struct node *q)
{
    struct node *temp;
    temp = q;
    while(temp != NULL)
    {
        printf("%d",temp->data);
        temp = temp->next;
    }
}



int main()
{
    struct node *a;
    a = NULL;
    insert( a,13);
    insert( a,13);
    display(a);
    return 0;
}

In the insert function q is a pointer to struct node which is initialized to NULL.

Here I am seeing 1st if q is NULL or not. If it is null then I am allocating heap memory, data and next pointer, in this way q is now a pointer which is dereferencing to 1st data. If q is not NULL, then I take a temp pointer which points to a struct node which is being pointed by q, so till temp becomes NULL temp goes to temp->next, then it allocates heap memory, puts data and next pointer to NULL.

But it is showing nothing for my display function please correct me on this, and on how stack and heap memory is used in linked list.

  • 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-06T05:50:42+00:00Added an answer on June 6, 2026 at 5:50 am

    Recall that in C arguments are pass-by-value, including pointer arguments.

    When q == NULL, you’re allocating memory and assigning that memory to q, but this won’t change q outside your function: only the copy of q inside your function will be changed.

    In order to change what the argument q points to, and have those changes reflected outside your function, you’ll need to pass a pointer to a pointer, e.g.:

    void insert(struct node **q, int num)
    

    And change how you’re using q, e.g.

    if (*q == NULL)
        *q = (struct node *) malloc(sizeof(struct node));
    

    Furthermore, in your else case, you should loop until temp->next == NULL, then add your new node with:

    temp->next = (struct node*) malloc(sizeof(struct node));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

struct node { int data; struct node *next; }; struct node * list,root; list=NULL;
I have the following C-code: #include<stdio.h> #include<stdlib.h> typedef struct node { int a; }node;
#include <stdio.h> #include <stdlib.h> typedef struct nod{ int data; struct nod *left,*right; }NOD; NOD
#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]);
#include<stdio.h> #include<signal.h> #include<stdlib.h> void handler(int signo) { printf(First statement); system(date); exit(EXIT_SUCCESS); } int main()
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
#include <stdio.h> #include <stdlib.h> typedef struct { char *Name; int grade; int cost; }Hotel;
Here is a program it is working #include <stdio.h> #include <stdlib.h> struct node {
#include <stdio.h> #include <pthread.h> #include <time.h> #include <stdlib.h> typedef struct pr_struct{ int owner; int
#include <stdlib.h> #include <stdio.h> struct foo{ int id; char *bar; char *baz[6]; }; int

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.