#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *link;
};
void append(struct node**,int);
void display(struct node*);
void add_at_beg(struct node**,int );
void main()
{
struct node *head;
int n;
clrscr();
head=NULL;
append(&head,5);
append(&head,6);
append(&head,7);
append(&head,8);
display(head);
printf(" \n Enter the number you want to insert ");
scanf("%d",&n);
add_at_beg(&head,n);
display(head);
getch();
}
void append(struct node **q,int num)
{
struct node *temp,*r;
temp=*q;
if(temp==NULL) /* If list is empty , create first node */
{
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->link=NULL;
*q=temp;
}
else
{
temp=*q;
while(temp->link !=NULL)
temp=temp->link;
r=(struct node*)malloc(sizeof(struct node));
r->data=num;
r->link=NULL;
temp->link=r;
}
}
void display(struct node *start)
{
printf("\n");
printf(" The Linked List is :\n");
/* Traverse the entire linked list */
while(start != NULL)
{
printf(" %d",start->data);
start=start->link;
}
}
void add_at_beg(struct node**q,int no)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=no;
temp->link=*q;
*q=temp;
}
Thanks sir this is helpful for me
ReplyDelete