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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:30:00+00:00 2026-06-15T07:30:00+00:00

#include<stdio.h> #include<malloc.h> typedef struct node_t { int i; struct node_t* link; } node; node*

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

typedef struct node_t {
    int i;
    struct node_t* link;
} node;

node* head = NULL;

int main() {
    int i = 10;
    node* temp = NULL;
    head = (node *)malloc(sizeof(node));
    temp = (node *)malloc(sizeof(node));
    if(temp == NULL) {
        printf("\n malloc for temp node failed! \n");
    }
    else {
        /* linked list logic to add the elements in the beginning */
        while(i<=10) {
            temp->i = i;
            temp->link = NULL;
            if(head == NULL) {
                head = temp;
            }
            else {
                temp->link = head;
                head = temp;
            }
            i++;
        }
    }
    for(temp = head; temp->link != NULL; temp = temp->link) {
        printf("\n The data is:%d \n",temp->i);
    }
    free(temp);
    free(head);
    return 0;
}

I’m trying a simple linked list program. I’m not getting the output.

  • 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-15T07:30:02+00:00Added an answer on June 15, 2026 at 7:30 am

    1) You have to allocate node (tmp) each time you are assigning value to tmp. and not allocate only one time the tmp. See the following fixed code to see how to do it

    2) the following for loop is wrong:

    for(temp = head; temp->link != NULL; temp = temp->link) {
    

    This for loop is fixed in the following code

    3) for the free you have to browse the whole linked list and then free each node. see the following fixed code.

    #include<stdio.h>
    #include<malloc.h>
    #include<stdlib.h>
    
    typedef struct node_t{
        int i;
         struct node_t* link;
    }node;
    
    node* head = NULL;
    
    int main(){
         int i = 1;
         node* temp = NULL;
    
         /* linked list logic to add the elements in the beginning */
         while(i<=10){
             temp = (node *)malloc(sizeof(node));
             if(temp == NULL){
                 printf("\n malloc for temp node failed! \n");
                 exit(1);
             }
             temp->i = i;
             temp->link = NULL;
             if(head == NULL){
                 head = temp;
             }
             else{
                 temp->link = head;
                 head = temp;
             }
             i++;
         }
    
         for(temp = head; temp != NULL; temp = temp->link){
             printf("\n The data is:%d \n",temp->i);
         }
    
         while (head!=NULL)
         {
            temp = head->link;
            free(head);
            head = temp;
         }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

1 #include<stdio.h> 2 #include<malloc.h> 3 4 typedef struct node_t{ 5 int i; 6 struct
I have the following C-code: #include<stdio.h> #include<stdlib.h> typedef struct node { int a; }node;
#include <stdio.h> typedef struct node { int i; struct node *next; }node; node getnode(int
#include <stdio.h> void wrapperPrint(char* s) { printf(s); return; } int main() { wrapperPrint(Hello world\n);
#include<stdio.h> #define N (sizeof(array) / sizeof(array[0])) main() { int array[5]={1,2,3,4,5}; int d; for(d=-1;d <=
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; void insert( struct
Here's my code: #include <stdio.h> typedef struct node_struct { int data; struct node_struct *next;
#include <stdio.h> #include <stdlib.h> typedef struct { char *Name; int grade; int cost; }Hotel;
#include <stdio.h> #include <pthread.h> #include <time.h> #include <stdlib.h> typedef struct pr_struct{ int owner; int
#include <stdio.h> #include <stdlib.h> typedef int element; struct cell { element e; struct cell

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.