Thread: explain me the error in this linked list creation program

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    76

    explain me the error in this linked list creation program

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct node{
    int data;
    struct node *next;
    };
    void ins(struct node *temp,struct node *head)
    {
      temp->next=head;
    head=temp;                #error caused by this line
    }
    void main()
    {
    struct node *head,*temp,*p;
    char c;
    int i;
    printf("Do you want to enter data? Y/N ");
    scanf("%c",&c);
    fflush(stdin);
    if(c=='y'||c=='Y')
    {
    head=(struct node *)malloc(sizeof(struct node));
    printf("enter your data: ");
    scanf("%d",&i);
    fflush(stdin);
    head->data=I;
    head->next=NULL;
    printf("Do you want to enter more data? Y/N ");
    scanf("%c",&c);
    fflush(stdin);
    }
    while(c=='y'||c=='Y')
    {
    temp=(struct node *)malloc(sizeof(struct node));
    printf("enter your data: ");
    scanf("%d",&i);
    fflush(stdin);
    temp->data=i;
    temp->next=NULL;
    ins(temp,head);
    printf("Do you want to enter more data? Y/N ");
    scanf("%c",&c);
    fflush(stdin);
    }
    p=head;
    while(p!=NULL)
    {
    printf("%d ",p->data);
    p=p->next;
    }
    }
    If I cut "head=temp" line from ins function definition and paste it after ins function call, this program works smoothly but why it is not working good in this above condition? please explain!!

  2. #2

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    Well the part which is causing error is different!

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Re-post the code with proper indentation.

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    any one else who can reply and help?

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Learn the difference between pass by pointer/address and pass by value.
    (Note: The purist sometimes says C is only pass by value.)

    This knowledge is often needed in writing link lists in C.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Learn that error/warning messages are useful!
    Post them when requesting help!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by gaurav# View Post
    any one else who can reply and help?
    Ignoring advice makes me less likely to help you. Posting crappy looking code might make others less likely to help you.

    I'll give you some help this time, but if you continue to make it difficult for us to help you (by not posting neat-looking code), then don't count on continued help from me.

    For starters, you don't initialize any of your pointers. So if the user does not enter 'y' or 'Y', you attempt to access random memory in your last "while()" loop.

    Your "ins()" function is all wrong. First of all, you're assigning "head" to "temp->next", which means it's no longer the head. Then you try assign temp back to head (which won't work anyway; to change the value of *head, you need to pass a pointer to *head to the function). But this is still not the right approach.

    Try this. In your "ins()" function, declare a node pointer. Assign the value of head to this pointer. Use this node pointer to traverse the list until you get to the last valid node. Then assign "temp" to "node->next".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Program Error
    By greendragons in forum C Programming
    Replies: 2
    Last Post: 12-11-2011, 10:12 AM
  2. linked list creation
    By sed_y in forum C++ Programming
    Replies: 4
    Last Post: 07-24-2011, 10:36 PM
  3. Linked List, node creation question
    By Alec0905 in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2010, 09:45 AM
  4. Can someone explain Linked List to me?
    By Frost Drake in forum C++ Programming
    Replies: 22
    Last Post: 01-03-2006, 08:32 PM
  5. Linked List creation
    By thedoofus in forum C Programming
    Replies: 2
    Last Post: 05-01-2005, 11:51 AM