0

I have written below program which creates a new linked list and prints its elements. The problem I face here is that when i print the values, only the value of last node gets printed and that too several times. Please have a look and tell me what am i doing wrong in this program.

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

struct node
{
       int data;
       struct node *link;
}*head=NULL;

void print()
{
     struct node *temp=NULL;
     temp=head;
     while(temp!=NULL)
     {
                    printf("%d", temp->data);
                      temp=temp->link;
     }
     printf("NULL");
}
void create_list()
{
     int value;char ch;
     struct node *new_node=(struct node*)malloc(sizeof(struct node*));
      struct node *current=NULL;
    printf("before do\n");
     do{


    printf("enter the data \n");
    scanf("%d", &value);
    new_node->data=value;
    new_node->link=NULL;
     if(head==NULL)
     {
                   head=new_node;
                   current=new_node;
     }
     else
     {

      current->link=new_node;
      current=new_node;
      }                
     printf("do you want to create another  node \n");
     ch=getche();
     }while(ch=='Y');

}





int main()
{
    create_list();
    print();
   getchar();
     return 0;
}

Input and output:

enter the data
2
do you want to create another  node 
Y
enter the data
3
do you want to create another  node 
Y
enter the data
4
do you want to create another  node 
Y
enter the data
5
do you want to create another  node 
N

55555555555555555555
55555555555555555555
55555555555555555555
55555555555555555555
55555555555555555555
2
  • The 90's called, they want their conio.h back. Commented Apr 28, 2014 at 9:52
  • 3
    This is wrong: malloc(sizeof(struct node*)); Use malloc(sizeof(struct node));. And don't cast return value of malloc in C. What you are getting as output is not the last value you input, it is just the garbage. Commented Apr 28, 2014 at 9:53

2 Answers 2

1

You are creating only one node:

struct node *new_node=(struct node*)malloc(sizeof(struct node*));

Above line is before do..while loop. It should be inside the loop.

Sign up to request clarification or add additional context in comments.

3 Comments

BTW, do you think, this is right: malloc(sizeof(struct node*));
You are correct. It should be malloc(sizeof(struct node)). But that's not the reason for the main issue.
Well, I'll say that is one of the reason.
1

The problem is that you are adding value to the same node. And by your program there will be only one node, linked to the same node. The while(temp!=NULL) Fails due to the same reason. temp->link points to the same temp. That you are getting the same output many (infinite ) times.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.