I am Learning Link List. Today wrote a simple program, but not working. Program is like this
Expand|Select|Wrap|Line Numbers
- #include<stdio.h>
- #include<stdlib.h>
- #include<alloca.h>
- struct node{
- int data;
- struct node *link;
- };
- void addafter(struct node **q,int value);
- void display(struct node *q);
- void main()
- {
- struct node *p;
- p=NULL;
- addafter(&p,11);
- addafter(&p,12);
- addafter(&p,13);
- addafter(&p,14);
- display(p);
- }
- void addafter(struct node **q,int value)
- {
- struct node *temp,*r ;
- if(*q==NULL){
- temp=(struct node *)malloc(sizeof (struct node));
- temp->data=value;
- temp->link=NULL;
- *q=temp;
- free(temp);
- }
- else{
- temp=*q;
- while(temp->link!=NULL)temp=temp->link;
- r=(struct node *)malloc(sizeof(struct node));
- r->data=value;
- r->link=NULL;
- temp->link=r;
- *q=temp;
- }
- }
- void display(struct node *q)
- {
- while(q!=NULL)
- {
- printf("%d\n",q->data);
- q=q->link;
- }
- }
I am trying to execute this code in LINUX. Please anybody can help.