#include
#include
#include
#include
struct student
{
char roll[6];
char name[10];
float mark;
struct student *next;
}*head,*tail;
void showinfo();
void insert();
void display();
void pop();
void main()
{
int x=0;
char ch;
clrscr();
printf("\n1. Insert a new student.");
printf("\n2. Display all students information.");
printf("\n3. Remove an existing student.");
printf("\n4. Show student information.");
printf("\n5. Exit\n");
while(x==0)
{
printf("Please enter what you want to do(1,2,3,4,5);");
ch=getch();
switch(ch)
{
case'1':
insert();
break;
case'2':
display();
break;
case'3':
pop();
break;
case'4':
showinfo();
break;
case'5':
x=1;
break;
}
}
getch();
}
void insert()
{
struct student *sptr,*aux,*prev;
sptr=(struct student*)malloc(sizeof(struct student));
int a,count=1;
float mr;
printf("\n Enter information to be inserted:\n\n Student roll:");
scanf("%s",sptr->roll);
printf("\n Student Name:");
scanf("%s",sptr->name);
printf("\n Student gpa:");
scanf("%f",&mr);
sptr->mark=mr;
sptr->next=NULL;
aux=head;
if(!head)
{
head=sptr;
tail=sptr;
}
else
{
printf("\nEnter the position:");
scanf("%d",&a);
while(aux){
if(count==a)
break;
else
prev=aux;
count++;
aux=aux->next;
}
if(prev->next==NULL&&a!=1){
tail->next=sptr;
tail=sptr;
tail->next=NULL;
}
else{
prev->next=sptr;
prev=sptr;
prev->next=aux;
if(a==1)
head=prev;
}
}
}
void display()
{
int c=0;
struct student *sptr;
sptr=head;
if(!sptr)
printf("\n\n\nEmpty list-----------Press any key");
printf("\n\n");
if(sptr)
printf("\nContents of the list are: ");
while(sptr)
{
c++;
printf("\n%d. %s",c,sptr->roll);
sptr=sptr->next;
}
printf("\n\n\nPress any key to continue-----------");
}
void pop()
{
struct student *sptr,*prev;
char roll[10];
printf("\nDo you want to remove the roll");
scanf("%s",roll);
sptr=head;
while(sptr)
{
if(!strcmp(sptr->roll,roll))
break;
else
prev=sptr;
sptr=sptr->next;
}
if(sptr)
{
if(sptr==head)
head=sptr->next;
if(sptr==tail)
{
prev->next=NULL;
tail=prev;
}
prev->next=sptr->next;
printf("\n The student with roll:%s... is removed",roll);
printf("\n\n\nPress any key to continue.");
free(sptr);
}
else
printf("\nThe student with roll:%s... is not found.",roll);
}
void showinfo()
{
struct student *sptr,*prev;
char roll[6];
printf("\nInseart the roll which you want to show info:");
scanf("%s",roll);
sptr=head;
while(sptr)
{
if(!strcmp(sptr->roll,roll))
break;
sptr=sptr->next;
}
if(sptr)
{
printf("\nThe information about student %s is: \n",roll);
printf("\nroll= %s name= %s mark=%0.2f",sptr->roll,sptr->name,sptr->mark);
printf("\n\n\nPress any key to continue.");
free(sptr);
}
else
printf("\nThe student with roll:%s... is not found.",roll);
}
0 comments:
Post a Comment