INSERT A NEW NODE AT BEGINIING IN THE LINKED LIST USING C LANGUAGE
#include<stdio.h>
#include<conio.h>
typedef struct list
{
int data;
struct list *next;
}node;
node *start=NULL;
void creat();
void display();
void insert_beg();
void main()
{
// node *ptr=NULL;
int option;
clrscr();
do
{
printf("\n Main Menu");
printf("\n1:Creat list");
printf("\n2:Display list");
printf("\n3:Insert at begining");
printf("\n0:Exit\n");
printf("\nEnter your option:-");
scanf("%d",&option);
switch(option)
{
case 1:creat();
printf("\nlinked list created");
break;
case 2:display();
break;
case 3:insert_beg();
printf("\nNode inserted at begining:-");
break;
case 0:
exit(0);
}
}
while(option!=0);
getch();
}
void creat()
{
node *ptr,*newnode;
int num;
printf("Enter data,pres 999 to exit:-");
scanf("%d",&num);
while(num!=999)
{
newnode=(node*)malloc(sizeof(node));
newnode->data=num;
if(start==NULL)
{
newnode->next=NULL;
start=newnode;
}
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode;
newnode->next=NULL;
}
printf("Enter another data:-");
scanf("%d",&num);
}
}
void display()
{
node *ptr;
ptr=start;
while(ptr!=NULL)
{
printf("\%d->",ptr->data);
ptr=ptr->next;
}
}
void insert_beg()
{
node *newnode,*ptr;
int num;
newnode=(node*)malloc(sizeof(node));
printf("\nEnter num:-");
scanf("%d",&num);
newnode->data=num;
newnode->next=start;
start=newnode;
}
Post a Comment