Hi !
This is a program I wrote which compiles fine but gives me and error on running it .. I need some help on finding what I am doing wrong in this ????
#include<stdio.h>
#include<stdlib.h>
Code:
/* ********************************************************** */
/* This program creates a linked list */
/* ********************************************************** */
typedef struct node* link;
struct node
{
int item;
link next;
};
void makelist(link x, int y);
void printlist(link x);
int main()
{
link x;
makelist(x, 5);
printlist(x);
return 0;
}
void makelist(link x, int num)
{
int i;
if(0 == num)
{
x = NULL;
return;
}
link a = malloc(sizeof *a);
x = a;
a->item = 1;
if(1 == num)
{
a->next = NULL;
return;
}
for(i = 2; i <= num; i++)
{
a->next = malloc(sizeof *a);
a = a->next;
a->item = i;
if(i == num)
{
a->next = NULL;
break;
}
}
}
void printlist(link x)
{
link b = x;
while(b != NULL)
{
printf("%d, ", b->item);
b = b->next;
}
}