1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

mark out correction for the question(insertion at tail in linked list)

Discussion in 'C' started by sumedh yadav, Nov 3, 2012.

  1. sumedh yadav

    sumedh yadav New Member

    Joined:
    Aug 18, 2012
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    OUTPUT segmentation fault

    C code
    Code:
    #include<stdio.h>
    #define OK 1
    #define ERROR 0
    typedef struct node{
      int data;
      struct node *next;
    }node,*list;
    int insertionattail(list l,int data)  {
      list t,temp;
      temp=l;
      t=(list)malloc(sizeof(node));
      if(t==NULL)return ERROR;
      t->data=data;
      while(l->next)  {
        l->next=l->next->next;
      }
      t->next=NULL;
      l->next=t;
      l=temp;
      return OK;
    }
    int main()  {
      node n;
      list l;
      l=&n;
      n.next=NULL;
      insertionattail(l,5);
      insertionattail(l,7);
      printf("data1=%d,data2=%d",l->next->next->data,l->next->next->next->data);
      return 0;
    }
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
  3. DRK

    DRK New Member

    Joined:
    Apr 13, 2012
    Messages:
    44
    Likes Received:
    3
    Trophy Points:
    0
    This is what you get when you explicitly cast pointer returned by malloc. To call this function an appropriate header needs to be included - <stdlib.h>. Without the explicit cast the complier would display a warning.
     

Share This Page