Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is simple code of linked list . In the main function i am trying to print 2 and 3 but the code prints only last value i.e 3. I am not getting what is wrong with the code . Here is the code
#include<stdio.h>

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

  struct list* insert(struct list* node,int data)
  {
    struct list* newnode=malloc(sizeof(struct list));
    if(!newnode){
      newnode=data;
    }
    newnode->data=data;
    newnode->next=NULL;
  }

printlist(struct list* node)
{
  struct list *newnode;
  if(!newnode)
    {
      printf("empty list\n");
    }
  while(newnode!=NULL)
    {
      printf("%d\n",newnode->data);
      newnode=newnode->next;
    }
}


int main()
{
   int i,n;
  struct list* temp;
  temp=malloc(sizeof(struct list));
  for(i=0;i<9;i++){
    scanf("%d",&n);
    insert(temp,n);
  }
   
 printlist(temp);

}
Posted
Updated 18-Aug-13 22:59pm
v2

There are many errors in your code.
Below a working implementation. However, as it stands, it is still rather ugly: you have to complete (for instance you need to eventually release the memory) and improve it.

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

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

struct list* insert(struct list* node,int data)
{
  struct list* newnode=malloc(sizeof(struct list));
    assert(newnode);
  newnode->data=data;
  newnode->next=NULL;
    if( node )
    {
        while ( node->next )
            node = node->next;
        node->next = newnode;
    }
    return newnode;
}

void printlist(struct list* node)
{
  if(!node)
  {
    printf("empty list\n");
  }
  while(node!=NULL)
  {
    printf("%d\n",node->data);
    node=node->next;
  }
}


int main()
{
  int i,n;

  struct list * first;

    scanf("%d",&n);

    first= insert(NULL,n);

  for(i=1;i<9;i++){
    scanf("%d",&n);
    insert(first,n);
  }

 printlist(first);

}
 
Share this answer
 
v3
Comments
pasztorpisti 19-Aug-13 5:30am    
+5 for your efforts. Next time don't forget to recommend learning the usage of a debugger... :-)
CPallini 19-Aug-13 5:33am    
Thank you. I could recommend learning the usage of a compiler, at first.
pasztorpisti 19-Aug-13 5:36am    
:-) :-) :-)
Look at your code: if nothing else, have a close look at printlist:
C++
printlist(struct list* node)
{
  struct list *newnode;
  if(!newnode)
    {
      printf("empty list\n");
    }
  while(newnode!=NULL)
    {
      printf("%d\n",newnode->data);
      newnode=newnode->next;
    }
}
Where in there are you using the "node" you pass in? So how would you expect it to print the values it contains?
 
Share this answer
 
Comments
Member 10200096 20-Aug-13 0:08am    
printlist will take the argument which is kind of struct list * and after that i am initialising a newnode of that kind . I mean i have done the same in insert() function too. And even if i use node ,the code gives output 0
OriginalGriff 20-Aug-13 3:15am    
Yes, you hand a argument to both functions, and yes, that argument is a "kind of struct list *" - but you don't actually use it inside either function.
It's a bit like a car: you have specified your car as the one to drive, but then gone up to the nearest car in the car park and tried to drive it! If you pass an argument, then you have to explicitly use the name of the parameter in order to do anything with it - you don't, you create a new one and use that, then it is discarded at the end of the function!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900