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

The Archive Base Latest Questions

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

#include<stdio.h> struct node { int item; struct node *link }; main() { struct node

  • 0
#include<stdio.h>
struct node
{
    int item;
    struct node *link
};

main()
{
    struct node *start,*list;
    int i;
    start = (struct node *)malloc(sizeof(struct node));
    list = start;
    start->link = NULL;
    for(i=0;i<10;i++)
    {
        list->item = i;
    list->link = (struct node *)malloc(sizeof(struct node));
    }
    list->link = NULL;
    while(start != NULL)
    {
        printf("%d\n",start->item);
    start = start->link;
    }
}

As the title suggests in this i am trying to traverse through a linked list itteratively
the expected output is
0
1
.
.
9
and the observed output is : 9
What is wrong with the code ?

  • 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-18T17:42:11+00:00Added an answer on June 18, 2026 at 5:42 pm

    It is just because of one statement in your code.
    You forgot to point to the next link, when you tried to allocate the new link.
    Because of that you were allocating only at one pointer, thus having memory leak.

    #include<stdio.h>
    struct node
    {
        int item;
        struct node *link
    };
    
    main()
    {
        struct node *start,*list;
        int i;
        start = (struct node *)malloc(sizeof(struct node));
        list = start;
        start->link = NULL;
        for(i=0;i<10;i++)
        {   
            list->item = i;
            list->link = (struct node *)malloc(sizeof(struct node));
            list = list->link;
        }   
        list->link = NULL;
        while(start != NULL)
        {   
            printf("%d\n",start->item);
            start = start->link;
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<stdlib.h> /* Link list node */ struct node { int data; struct node
#include<stdio.h> typedef struct { int *u; struct node next; } *node; int main() {
#include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int data; struct node*link; }; void push(struct node*,int);
This following code works fine: #include <stdio.h> #include <stdlib.h> int main() { struct node{
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; void insert( struct
my test code is below: main1.c: #include <stdio.h> extern struct tt ; int main()
struct node { int data; struct node *next; }; struct node * list,root; list=NULL;
#include <stdio.h> typedef struct node { int i; struct node *next; }node; node getnode(int
I have the following C-code: #include<stdio.h> #include<stdlib.h> typedef struct node { int a; }node;
#include<stdio.h> #include<conio.h> union abc { int a; int x; float g; }; struct pqr

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.