Write menu driven program for the followings:
a) Linear linked list : Insertion, Deletion, Search, Display.
b) Linear ordered linked list : Insertion, Deletion, Search, Display.
c) Linear circular linked list : Insertion, Deletion, Search, Display.
d) Linear doubly linked list : Insertion, Deletion, Search, Display.
e) Linear doubly circular linked list : Insertion, Deletion, Search, Display.
Note :
1) Insertion and Deletion ( At first node, At last node, At inbetween nodes)
2) Each node in linked list contains Student's Roll_No, Name, CGPA.
//single circular linked list
//assignment 4
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
char name[20];
int roll_no;
float cgpa;
struct node *link;
};
struct node *head=NULL;
void insert();
void delete();
void search();
void display();
int main()
{
int choice;
struct node *head=NULL;
while(1){
printf("Enter your choice:\n");
printf("1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();break;
case 2:
delete();break;
case 3:
search();break;
case 4:
display();break;
case 5:
return 0;
default :
printf("Try valid input\n");
}
}
}
void insert()
{
int choice;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node)); // allocating memory in heap
struct node *trav;
trav=head;
int n,count=0;
printf("\nEnetr your choice :\n");
printf("1.Insert at beginning \n2.Insert at end \n3.Insert at nth position\n");
scanf("%d",&choice);
//getting the data
printf("Enter name :\n");
scanf("%s",temp->name);
printf("Enter roll number :\n");
scanf("%d",&(temp->roll_no));
printf("Enter CGPA :\n");
scanf("%f",&(temp->cgpa));
if(head==NULL)
{
temp->link=temp;
head=temp;
}
else{
switch(choice)
{
case 1:
while((trav->link)!=head)
{
trav=trav->link;
}
trav->link=temp;
temp->link=head;
head=temp;break;
case 2:
while(trav->link!=head)
{
trav=trav->link;
}
trav->link=temp;
temp->link=head;break;
case 3:
printf("Enter position :\n");
scanf("%d",&n);
while(count<n-2)
{
trav=trav->link;
count++;
}
temp->link=trav->link;
trav->link=temp;break;
}
}
}
void delete()
{
int choice;
printf("Enter your choice :\n");
printf("1.Delete 1st node\n2.Delete last node\n3.Delete nth node\n");
scanf("%d",&choice);
int n,count=0;
struct node *temp,*temp1;
struct node *last;
temp=head;
switch(choice)
{
case 1:
if (head==NULL)
{
printf("List is empty\n");
}
else
{
temp1=head;
while(temp1->link!=head)
{
temp1=temp1->link;
}
temp1->link=head->link;
head=head->link;
} break;
case 2:
if (head==NULL)
{
printf("List is empty\n");
}
else
{
while((temp->link->link)!=head)
{
temp=temp->link;
}
temp->link=head;
}break;
case 3:
printf("Enter n :\n");
scanf("%d",&n);
while(count<n-2)
{
temp=temp->link;
count++;
}
temp1=temp->link;
temp->link=temp1->link;
free(temp1);break;
}
}
void search()
{
struct node *temp;
temp=head;
char name[20];
printf("Search : Name ?\n");
scanf("%s",name);
do
{
if(strcmp(name,temp->name)==0)
{
printf("Here is the required data :\n");
printf("Name : %s\n",temp->name);
printf("Roll Number : %d\n",temp->roll_no);
printf("CGPA : %.2f\n\n",temp->cgpa);
return;
}
temp=temp->link;
}while(temp!=head);
printf("(X) Sorry , NO MATCHES FOUND !\n");
}
void display()
{
struct node *temp;
temp=head;
int count=1;
printf("\n...................................\n");
do
{
printf("Student %d : \n",count);
printf("Name : %s\n",temp->name);
printf("Roll Number : %d\n",temp->roll_no);
printf("CGPA : %.2f\n\n",temp->cgpa);
temp=temp->link;
count++;
}while(temp!=head);
printf("...................................\n");
}
Thanking you !
case 3:
ReplyDeleteprintf("Enter n :\n");
scanf("%d",&n);
while(countlink;
count++;
}
temp1=temp->link;
temp->link=temp1->link;
free(temp1);break;
}
help to understand that code please...
case 3:
Deleteprintf("Enter n :\n");
scanf("%d",&n);
/* here I used "Count" as a delimiter. i.e. "Count" let me know that I have reached (n-1)th node.*/
while(countlink;
count++;
}
/* here, as our "temp" is (n-1)th node, temp1 = temp->link will assign temp1 as (n)th node.
so, temp1 is the required nth node, which we wants to delete.*/
temp1=temp->link;
/* we are setting link of (n-1)th node to link of nth node. This will drop "nth" node, obviously.*/
temp->link=temp1->link;
/* freeing the memory of deleted nth node*/
free(temp1);break;