[ create a new paste ] login | about

Link: http://codepad.org/CPdUvK0x    [ raw code | output | fork ]

C, pasted on Mar 26:
#include<stdio.h>
#include<stdlib.h>

struct node
{
       int x;
       struct node *next;
       };
       
 void add(struct node **root, int x)
 {
      struct node *conductor;
      if(root==NULL)
      {
          (*root)=malloc(sizeof(struct node));
          (*root)->x=x;
          (*root)->next=NULL ;         
                    }
     else
     {
         conductor = *root;
         while(conductor->next!=NULL)
         {
            conductor = conductor -> next;             
                          }
         conductor->next=malloc(sizeof(struct node));
         conductor->next->x=x;
         conductor->next->next=NULL;
         } 
      }      
      void display(struct node *root)
      {
           struct node *conductor;
           conductor=root;
           while(conductor!=NULL)
           {
            printf("%d",conductor->x);
            conductor = conductor ->next;                           
                                 } 
           }
      
     

int main()
{
    struct node *root;
    root=NULL;
    add(&root,5);
    add(&root,4);
    display(root);
    free(root);
    }


Output:
1
Segmentation fault


Create a new paste based on this one


Comments: