INSERT AT THE BEGINNING OF LINK LIST

#include<stdio.h>

struct node
{
int data;
struct node* link;
};
struct node* head = NULL;

void insert(int number)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = number ;
temp->link = head;
head = temp;
}
void printthelist()
{
struct node* temp = head ;
while(temp!=NULL)
{
printf("->%d",temp->data);
temp = temp->link;
}
printf("\n");
}
void main()
{
int n,i,num;
printf("enter the number of elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter number\n");
scanf("%d",&num);
insert(num);
printthelist();
}
}

Comments