You are on page 1of 7

#include<stdio.

h>
#include<string.h>
#include<stdlib.h>

struct student{
char name[20];
int roll;
char tel[15];
char add[50];
struct student *next;
};

struct student *SReg=NULL;

int add(struct student *s){


if(SReg==NULL){SReg=s; return 1;}

struct student *temp=SReg;


while(temp->next!=NULL){
if(temp->roll==s->roll)return 0;
temp=temp->next;
}
temp->next=s;
return 1;
}

struct student* get_st(int roll){


if(SReg==NULL)return NULL;
struct student *temp=SReg;
while(temp!=NULL){
if(temp->roll==roll)return temp;
temp = temp->next;
}
return NULL;
}

int Delete(int roll){


if(SReg==NULL)return 0;
if(SReg->roll==roll){
struct student *temp=SReg;
SReg=SReg->next;
free(temp);
return 1;
}
struct student *temp=SReg;
while(temp->next!=NULL){
if(temp->next->roll==roll)break;
temp=temp->next;
}
if(temp->next==NULL)return 0;
struct student *a= temp->next;
temp->next=temp->next->next;
free(a);
return 1;
}

int modify(struct student *s){


struct student *temp=SReg;
while(temp!=NULL){
if(temp->roll==s->roll)break;
temp=temp->next;
}
if(temp==NULL)return 0;
strcpy(temp->name,s->name);
temp->roll=s->roll;
strcpy(temp->tel,s->tel);
strcpy(temp->add,s->add);
return 1;
}

int get_count(){
int count=0;
struct student * temp=SReg;
while(temp!=NULL){
count++;
temp=temp->next;
}
return count;
}

void sort_student(){
if(SReg==NULL)return;
if(SReg->next==NULL)return ;
int count =0;
struct student *temp= SReg;
while(temp!=NULL){
count++; temp= temp->next;
}
for(int i=0;i<count;i++){
struct student* temp1=SReg;
struct student* temp2= SReg->next;
if(strcmp(temp1->name,temp2->name)>0){
struct student *a= temp2;
temp1->next=temp2->next;
temp2->next= temp1;
SReg = temp2;
temp1= SReg->next;
temp2= SReg->next->next;
while(temp2!=NULL){
if(strcmp(temp1->name,temp2->name)>0){
temp1->next=temp2->next;
temp2->next= temp1;
struct student *a;
a = temp1;
temp1= temp2;
temp2=a;
temp1= temp1->next;
temp2= temp->next;
continue;
}
temp1= temp1->next;
temp2= temp2->next;
}
}
}
}

int main(){
int counter=0 ;
do{
printf("Enter 1 for Insert a New Student
details\n");
printf("Enter 2 for get the student details with
given Roll\n");
printf("Enter 3 for Delete the student's details
with given Roll\n");
printf("Enter 4 for Modify the data of the student
of the given Roll\n");
printf("Enter 5 to store the Student Register in
the given file\n");
printf("Enter 6 to get the count of the
student\n");
printf("Enter 0 for exit from the Student
table\n");
scanf("%d",&counter);
switch(counter){
case 1:{
struct student *s= (struct
student*)malloc(sizeof(struct student));
printf("Enter the Name of the student\n");
gets(s->name);
printf("Enter the roll of the student\n");
scanf("%d",&(s->roll));
printf("Enter the telephone no.\n");
scanf("%s",(s->tel));
printf("Enter the address\n");
gets(s->add);
s->next = NULL;
int x = add(s);
if(x==1)printf("Details added
successfully\n");
else printf("Student data is already
exists\n");
break;
}
case 2: {
printf("enter the Roll\n");
int z;
scanf("%d",&z);
struct student* st= get_st(z);
if(st==NULL)printf("No Data exists\n");
else {
printf("Name : %s\n",st->name);
printf("Roll : %d\n",st->roll);
printf("Tel : %s\n",st->tel);
printf("add :");
puts(st->add);printf("\n");
}
break;
}
case 3:{
printf("Enter the Roll\n");
int z; scanf("%d",&z);
int x = Delete(z);
if(x==1)printf("Delete operation is
completed\n");
else printf("No record for this Roll
exits\n");
}
case 4:{
struct student *s= (struct
student*)malloc(sizeof(struct student));
printf("Enter the Name of the student\n");
gets(s->name);
printf("Enter the roll of the student\n");
scanf("%d",&(s->roll));
printf("Enter the telephone no.\n");
scanf("%s",(s->tel));
printf("Enter the address\n");
gets(s->add);
s->next = NULL;
int b = modify(s);
if(b==1)printf("updated Successfully\n");
else printf("No Roll No matched with Given
data\n");
}
case 6:{
int z = get_count();
printf("No. of the student is %d\n",z);
}
}
}while(counter!=0);
return 0;
}

You might also like