Write a menu based C program for performing operation such as INSERT, DELETE, SEARCH, MODIFY, DISPLAY, EXIT etc on linear Linked list.
#include <stdio.h> //HEADER FILES
#include <string.h>
struct student //STRUCTURE DEFINITION
{
long int rollno;
float cpi;
char name[40],branch[10];
struct student *next; //POINTER FOR LINKED LIST
}stud1,stud2; //STRUCTURE VARIABLES
struct student *temp,*previous,*current; //STRUCTURE POINTERS
struct student *top=NULL; //POINTER FOR FIRST ELEMENT
int menu(void); //FUNCTION TO DISPLAY MAIN MENU
int submenu(int); //FUNCTION TO DISPLAY SUBMENU
void target(int,int); //FUNCTION TO GET THE TARGET NODE
void found(void); //FUNCTION TO DISPLAY THE CONTENTS OF A NODE
int getit(int,int *); //FUNCTION TO READ FROM LINKED LIST
void add(void); //FUNCTION TO ADD IN LINKED LIST
void delete(void); //FUNCTION TO DELETE A NODE
void modify(void); //FUNCTION TO MODIFY A NODE
void search(void); //FUNCTON TO SEARCH FOR A NODE
void display(void); //FUNCTION TO DISPLAY ALL NODES
main() //main() STARTS HERE
{
int i,j,k,choice;
do
{
choice=menu(); //DISPLAY MAIN MENU
switch(choice) //GET CHOICE AND PERFORM.....
{ //.....CORRESPONDING OPERATION
case 1 : add();
break;
case 2 : delete();
break;
case 3 : modify();
break;
case 4 : search();
break;
case 5 : display();
break;
case 6 : printf("nt************ PROGRAM OVER ************nn");
temp=top;
while(temp!=NULL) //BEFORE EXITING FREE ALL THE ALLOCATED SPACE
{
temp=temp->next;
free(top);
top=temp;
}
break;
}
}while(choice!=6);
}
int menu() //FUNCTION TO DISPLAY MAIN MENU AND GET CHOICE
{
int choice;
while(1)
{
printf("nnn STUDENTS' RECORD ");
printf("n MAIN MENU");
printf("n ADD RECORD :: 1");
printf("n DELETE RECORD :: 2");
printf("n MODIFY RECORD :: 3");
printf("n SEARCH RECORD :: 4");
printf("n DISPLAY ALL RECORDS :: 5");
printf("n EXIT :: 6");
printf("n ENTER YOUR CHOICE :: ");
scanf("%d",&choice);
if((choice>=1)&&(choice<=6))
return(choice);
}
}
int submenu(int type) //FUNCTION TO DISPLAY SUBMENU AND GET CHOICE
{
char ask[][10]={"DELETE","MODIFY","SEARCH"};
int choice;
do
{
printf("ntt %s MENU",ask[type-1]);
printf("nt %s BY ROLL NO. :: 1",ask[type-1]);
printf("nt %s BY NAME :: 2",ask[type-1]);
printf("nt RETURN TO MAIN MENU :: 3");
printf("nt ENTER YOUR CHOICE :: ");
scanf("%d",&choice);
}while((choice!=1)&&(choice!=2)&&(choice!=3));
return choice;
}
void target(int choice,int type) //FUNCTION TO GET TARGET NODE
{
char ch,opt[][10]={"ROLL NO","NAME"};
char ask[][10]={"DELETED","MODIFIED","SEARCHED"};
int j;
printf("n ENTER THE %s OF STUDENT WHOSE RECORD IS TO BE %s :: ",opt[choice-1],ask[type-1]);
if(choice==1)
scanf("%ld",&stud1.rollno);
else
{
ch=getchar();
j=0;
ch=getchar();
while(ch!='n')
{
stud1.name[j++]=ch;
ch=getchar();
}
stud1.name[j]='';
}
}
void found(void) //FUNCTION TO DISPLAY THE CONTENTS OF NODE POINTED BY temp
{
printf("n ROLL NO. :: %ld",temp->rollno);
printf("n NAME :: %s",temp->name);
printf("n BRANCH :: %s",temp->branch);
printf("n C.P.I. :: %3.2fn",temp->cpi);
}
int getit(int choice,int *flag) //FUNCTION TO COMPARE EACH NODE WITH TARGET NODE
{
int find=0,j;
char ch;
while(temp!=NULL)
{
if((choice==1)&&(stud1.rollno==temp->rollno))
find=1;
else if((choice==2)&&(!strcmp(stud1.name,temp->name)))
find=1;
else
{
previous=temp;
temp=temp->next;
continue;
}
if(find)
{
*flag=1;
printf("n RECORD FOUND::");
found();
ch=getchar();
return(find);
}
}
return (find);
}
void add(void) //FUNCTION TO ADD A RECORD IN LIST
{
int i,j,k;
char cho,ch;
k=1;
do
{
// printf("n%d",sizeof(struct student));
// printf("n%d",sizeof(long int));
// printf("n%d",sizeof(float));
// printf("n%d",sizeof(char [40]));
// printf("%d",sizeof(struct student *));
temp=(struct student *)(malloc(sizeof(struct student))); //DYNAMIC MEMORY ALLOCATION
printf("nn ENTER ROLL NO. OF STUDENT :: ");
scanf("%ld",&temp->rollno);
ch=getchar();
printf("n ENTER STUDENT'S NAME :: ");
j=0;
ch=getchar();
while(ch!='n')
{
temp->name[j++]=ch;
ch=getchar();
}
temp->name[j]='';
printf("n ENTER STUDENT'S BRANCH :: ");
scanf("%s",temp->branch);
printf("n ENTER STUDENT'S C.P.I. :: ");
scanf("%f",&temp->cpi);
ch=getchar();
printf("n DO U WANT TO SAVE THIS RECORD (Y/N)??");
cho=getchar();
if((cho=='n')||(cho=='N'))
{
free(temp); //IF NOT TO SAVE THEN FREE THE ALLOCATED MEMORY
goto label;
}
temp->next=top;
top=temp;
printf("n ONE RECORD SUCCESSFULLY ADDED");
label:
ch=getchar();
printf("n WANT TO ENTER MORE RECORDS (Y/N) ::");
cho=getchar();
}while((cho=='y')||(cho=='Y'));
}
void delete(void) //FUNCTION TO DELETE A RECORD FROM LIST
{
int i,j,flag,find,choice,num,rec;
char cho,ch;
do
{
if(top==NULL)
{
printf("n NO RECORDS IN LIST!!!!!");
printf("n RETURNING TO MAIN MENU");
break;
}
choice=submenu(1);
if(choice==3)
return;
target(choice,1);
flag=rec=0;
temp=top;
previous=NULL;
while(temp!=NULL)
{
cho='n';
find=getit(choice,&flag);
if(find)
{
printf("n DO U WANT TO DELETE THIS RECORD (Y/N)??");
cho=getchar();
if((cho=='n')||(cho=='N'))
{
previous=temp;
temp=temp->next;
continue;
}
else
{
if(top==temp)
{
current=top;
top=top->next;
temp=top;
free(current);
rec++;
}
else
{
current=temp;
previous->next=temp->next;
temp=temp->next;
free(current);
}
}
}
}
if(!flag)
printf("n RECORD NOT FOUND");
if(rec)
printf("n %d RECORD SUCCESSFULLY DELETED",rec);
ch=getchar();
printf("n WANT TO DELETE SOME OTHER RECORDS (Y/N) ::");
cho=getchar();
}while((cho=='y')||(cho=='Y'));
}
void modify(void) //FUNCTION TO MODIFY A RECORD
{
int i,j,flag,find,choice,num,rec;
char cho,ch;
do
{
if(top==NULL)
{
printf("n NO RECORDS IN LIST!!!!!");
printf("n RETURNING TO MAIN MENU");
break;
}
choice=submenu(2);
if(choice==3)
return;
target(choice,2);
flag=rec=0;
temp=top;
previous=NULL;
while(temp!=NULL)
{
cho='n';
find=getit(choice,&flag);
if(find)
{
printf("n DO U WANT TO MODIFY THIS RECORD (Y/N)??");
cho=getchar();
if((cho=='n')||(cho=='N'))
{
temp=temp->next;
continue;
}
if((cho=='y')||(cho=='Y'))
{
printf("n ENTER THE NEW ROLL NO. STUDENT :: ");
scanf("%ld",&temp->rollno);
ch=getchar();
printf("n ENTER STUDENT'S NAME :: ");
j=0;
ch=getchar();
while(ch!='n')
{
temp->name[j++]=ch;
ch=getchar();
}
stud2.name[j]='';
printf("n ENTER STUDENT'S BRANCH :: ");
scanf("%s",temp->branch);
printf("n ENTER STUDENT'S C.P.I. :: ");
scanf("%f",&temp->cpi);
temp=temp->next;
rec++;
}
}
}
if(!flag)
printf("n RECORD NOT FOUND");
if(rec)
printf("n %d RECORD SUCCESSFULLY MODIFIED",rec);
ch=getchar();
printf("n WANT TO MODIFY SOME OTHER RECORDS (Y/N) ::");
cho=getchar();
}while((cho=='y')||(cho=='Y'));
}
void search(void) //FUNCTION TO SEARCH A RECORD IN LIST
{
int i,j,flag,find,choice,num,rec;
char cho,ch;
do
{
if(top==NULL)
{
printf("n NO RECORDS IN LIST!!!!!");
printf("n RETURNING TO MAIN MENU");
break;
}
choice=submenu(3);
if(choice==3)
return;
target(choice,3);
flag=rec=0;
temp=top;
previous=NULL;
while(temp!=NULL)
{
find=getit(choice,&flag);
if(find)
{
temp=temp->next;
rec++;
}
}
if(!flag)
printf("n RECORD NOT FOUND");
if(rec)
printf("n %d RECORDS FOUND",rec);
ch=getchar();
printf("n WANT TO SEARCH SOME OTHER RECORDS (Y/N) ::");
cho=getchar();
}while((cho=='y')||(cho=='Y'));
}
void display(void) //FUNCTION TO DISPLAY ALL RECORDS OF LIST
{
int i,j;
char cho;
i=1;
if(top==NULL)
{
printf("n NO RECORDS IN LIST!!!!!");
printf("n RETURNING TO MAIN MENU");
return;
}
temp=top;
while(temp!=NULL)
{
printf("n SR. NO. %d",i);
found();
temp=temp->next;
if(!((i++)%5))
{
cho=getchar();
printf("n PRESS ENTER TO CONTINUE");
cho=getchar();
}
}
printf("n TOTAL NO. OF RECORDS = %d",i-1);
}


Recent Comments