/* link list */
# include
# include
struct node
{ int code;
char name[15];
struct node *next;
}*start,*ptr,*temp;
main()
{ int ch;
while(1)
{ clrscr();
printf("\n\t\tMAIN MENU");
printf("\n \t1. Create a link list");
printf("\n \t2. Display the list");
printf("\n \t3. Add a node in the beginning");
printf("\n \t4. Append a node");
printf("\n \t5. Insert a node");
printf("\n \t6. Delete a node");
printf("\n \t7. Sort the list");
printf("\n \t8. Exit the program");
printf("\n\tEnter your choice(1 to 8):");
scanf("%d",&ch);
switch (ch)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
addbeg();
break;
case 4:
addend();
break;
case 5:
insnode();
break;
case 6:
delnode();
break;
case 7:
sort();
break;
case 8:
return;
default:
printf("\nWrong choice
entered,Try again");
}
getch();
}
}
create()
{
char ans='y';
start=ptr=(struct node *) malloc(sizeof(struct
node));
// printf("SIZE OF NODE=%d",sizeof(struct node
)); //c
printf("\tstart=%u",start); //c
printf("\tptr=%u",ptr); //c
while (ans=='y' || ans=='Y')
{ printf("\nEnter code and name:");
scanf("%d%s",&ptr->code,ptr->name);
printf("\nWant to add another
node(Y/N):");
ans=getche();
if(ans=='y' || ans=='Y')
{
ptr->next=(struct node *)
malloc(sizeof(struct node));
printf("\tptr
next=%d",ptr->next); //c
ptr=ptr->next;
printf("\tptr =%u",ptr); //c
}
else
ptr->next=NULL;
}
return;
} //*******END****
display()
{
ptr=start;
printf("start in display=%u",ptr); //c
while(ptr!=NULL)
{
printf("\nCode=%d \t
Name=%s",ptr->code,ptr->name);
ptr=ptr->next;
printf("ptr =%u",ptr); //c
}
return;
}
addbeg()
{
char ans='y';
while(ans=='y' || ans=='Y')
{
ptr=(struct node *)
malloc(sizeof(struct node));
printf("\tptr=%u",ptr); //c
printf("\nEnter code and name:");
scanf("%d%s",&ptr->code,ptr->name);
ptr->next=start;
printf("ptr next=%d",ptr->next); //c
start=ptr;
printf("Start=%u",start); //c
printf("\nWant to add another(y/n)");
ans=getche();
}
return;
}
addend()
{ char ans='y';
ptr=start;
printf("ptr=%u",ptr); //c
while(ptr->next!=NULL)
ptr=ptr->next;
while(ans=='y'|| ans=='Y')
{
temp=(struct node *)
malloc(sizeof(struct node));
printf("temp=%u",temp); //c
printf("\nEnter code & name");
scanf("%d%s",&temp->code,temp->name);
temp->next=NULL;
ptr->next=temp;
printf("ptr next=%d",ptr->next);
ptr=ptr->next;
printf("ptr=%u",ptr);
printf("\nwant to add (y/n)");
ans=getche();
}
return;
}
insnode()
{ int cd;
ptr=start;
if(ptr!=NULL)
{
printf("\nEnter code before which to insert:");
scanf("%d",&cd);
}
if(ptr==NULL)
{
start=(struct node *)
malloc(sizeof(struct node));
printf("\nEnter code & name");
scanf("%d%s",&start->code,start->name);
start->next=NULL;
}
else if(ptr->code==cd)
{ ptr=(struct node *)
malloc(sizeof(struct node));
printf("ptr=%u",ptr); //c
printf("in 1"); //c
printf("\nEnter code & name:");
scanf("%d%s",&ptr->code,ptr->name);
ptr->next=start;
printf("ptr next=%d",ptr->next); //c
start=ptr;
printf("start =%u",ptr); //c
}
else
{
while(ptr->code!=cd && ptr!=NULL)
{
printf(" in 2 ");
temp=ptr;
printf("temp=%u",ptr);
//c
ptr=ptr->next;
printf("ptr
next=%d",ptr->next); //c
}
temp->next=(struct node *)
malloc(sizeof(struct node));
printf("temp next=%d",temp->next);
//c
printf("\nEnter code & name");
scanf("%d%s",&temp->next->code,temp->next->name);
temp->next->next=ptr;
printf("\ntemp next
next=%d",ptr);//c
}
return;
}
delnode()
{
int cd;
ptr=start;
if(ptr==NULL)
printf("\nLink list is empty");
else
{ printf("\nEnter code to be deleted");
scanf("%d",&cd);
if(start->code==cd)
{ ptr=start;
start=start->next;
free(ptr);
printf("\nNode successfully deleted");
}
else
{
while(ptr->code!=cd && ptr!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
if(ptr==NULL)
printf("\nNode not found");
else
{
temp->next=ptr->next;
free(ptr);
printf("\nNode deleted
successfully");
}
}
}
return;
}
sort()
{
struct node *ptr1;
char *temp1[15];
ptr=start;
ptr1=start;
if(ptr==NULL)
printf("\n Link list is empty");
else
while(ptr->next!=NULL)
{
ptr1=ptr->next;
while(ptr1!=NULL)
{
if(ptr->code > ptr1->code)
{
temp=ptr->code;
ptr->code=ptr1->code;
ptr1->code=temp;
strcpy(temp1,ptr->name);
strcpy(ptr->name,ptr1->name);
strcpy(ptr1->name,temp1);
}
ptr1=ptr1->next;
}
ptr=ptr->next;
}
return;
}
# include
# include
struct node
{ int code;
char name[15];
struct node *next;
}*start,*ptr,*temp;
main()
{ int ch;
while(1)
{ clrscr();
printf("\n\t\tMAIN MENU");
printf("\n \t1. Create a link list");
printf("\n \t2. Display the list");
printf("\n \t3. Add a node in the beginning");
printf("\n \t4. Append a node");
printf("\n \t5. Insert a node");
printf("\n \t6. Delete a node");
printf("\n \t7. Sort the list");
printf("\n \t8. Exit the program");
printf("\n\tEnter your choice(1 to 8):");
scanf("%d",&ch);
switch (ch)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
addbeg();
break;
case 4:
addend();
break;
case 5:
insnode();
break;
case 6:
delnode();
break;
case 7:
sort();
break;
case 8:
return;
default:
printf("\nWrong choice
entered,Try again");
}
getch();
}
}
create()
{
char ans='y';
start=ptr=(struct node *) malloc(sizeof(struct
node));
// printf("SIZE OF NODE=%d",sizeof(struct node
)); //c
printf("\tstart=%u",start); //c
printf("\tptr=%u",ptr); //c
while (ans=='y' || ans=='Y')
{ printf("\nEnter code and name:");
scanf("%d%s",&ptr->code,ptr->name);
printf("\nWant to add another
node(Y/N):");
ans=getche();
if(ans=='y' || ans=='Y')
{
ptr->next=(struct node *)
malloc(sizeof(struct node));
printf("\tptr
next=%d",ptr->next); //c
ptr=ptr->next;
printf("\tptr =%u",ptr); //c
}
else
ptr->next=NULL;
}
return;
} //*******END****
display()
{
ptr=start;
printf("start in display=%u",ptr); //c
while(ptr!=NULL)
{
printf("\nCode=%d \t
Name=%s",ptr->code,ptr->name);
ptr=ptr->next;
printf("ptr =%u",ptr); //c
}
return;
}
addbeg()
{
char ans='y';
while(ans=='y' || ans=='Y')
{
ptr=(struct node *)
malloc(sizeof(struct node));
printf("\tptr=%u",ptr); //c
printf("\nEnter code and name:");
scanf("%d%s",&ptr->code,ptr->name);
ptr->next=start;
printf("ptr next=%d",ptr->next); //c
start=ptr;
printf("Start=%u",start); //c
printf("\nWant to add another(y/n)");
ans=getche();
}
return;
}
addend()
{ char ans='y';
ptr=start;
printf("ptr=%u",ptr); //c
while(ptr->next!=NULL)
ptr=ptr->next;
while(ans=='y'|| ans=='Y')
{
temp=(struct node *)
malloc(sizeof(struct node));
printf("temp=%u",temp); //c
printf("\nEnter code & name");
scanf("%d%s",&temp->code,temp->name);
temp->next=NULL;
ptr->next=temp;
printf("ptr next=%d",ptr->next);
ptr=ptr->next;
printf("ptr=%u",ptr);
printf("\nwant to add (y/n)");
ans=getche();
}
return;
}
insnode()
{ int cd;
ptr=start;
if(ptr!=NULL)
{
printf("\nEnter code before which to insert:");
scanf("%d",&cd);
}
if(ptr==NULL)
{
start=(struct node *)
malloc(sizeof(struct node));
printf("\nEnter code & name");
scanf("%d%s",&start->code,start->name);
start->next=NULL;
}
else if(ptr->code==cd)
{ ptr=(struct node *)
malloc(sizeof(struct node));
printf("ptr=%u",ptr); //c
printf("in 1"); //c
printf("\nEnter code & name:");
scanf("%d%s",&ptr->code,ptr->name);
ptr->next=start;
printf("ptr next=%d",ptr->next); //c
start=ptr;
printf("start =%u",ptr); //c
}
else
{
while(ptr->code!=cd && ptr!=NULL)
{
printf(" in 2 ");
temp=ptr;
printf("temp=%u",ptr);
//c
ptr=ptr->next;
printf("ptr
next=%d",ptr->next); //c
}
temp->next=(struct node *)
malloc(sizeof(struct node));
printf("temp next=%d",temp->next);
//c
printf("\nEnter code & name");
scanf("%d%s",&temp->next->code,temp->next->name);
temp->next->next=ptr;
printf("\ntemp next
next=%d",ptr);//c
}
return;
}
delnode()
{
int cd;
ptr=start;
if(ptr==NULL)
printf("\nLink list is empty");
else
{ printf("\nEnter code to be deleted");
scanf("%d",&cd);
if(start->code==cd)
{ ptr=start;
start=start->next;
free(ptr);
printf("\nNode successfully deleted");
}
else
{
while(ptr->code!=cd && ptr!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
if(ptr==NULL)
printf("\nNode not found");
else
{
temp->next=ptr->next;
free(ptr);
printf("\nNode deleted
successfully");
}
}
}
return;
}
sort()
{
struct node *ptr1;
char *temp1[15];
ptr=start;
ptr1=start;
if(ptr==NULL)
printf("\n Link list is empty");
else
while(ptr->next!=NULL)
{
ptr1=ptr->next;
while(ptr1!=NULL)
{
if(ptr->code > ptr1->code)
{
temp=ptr->code;
ptr->code=ptr1->code;
ptr1->code=temp;
strcpy(temp1,ptr->name);
strcpy(ptr->name,ptr1->name);
strcpy(ptr1->name,temp1);
}
ptr1=ptr1->next;
}
ptr=ptr->next;
}
return;
}
No comments:
Post a Comment