# include <stdio.h>
# include <alloc.h>
# include <conio.h>
typedef struct student
{
int roll;
int age;
struct student *link;
}std;
std *head; std *temp; std *temp2; std *add;
int i; int r; int a; int n; int pos; int fRoll;
void append(int r,int a)
{
temp2 = (std *)malloc(sizeof(std));
temp2 -> roll = r;
temp2 -> age = a;
temp = head;
if(temp == NULL)
{
head = temp2;
head -> link = NULL;
}
else
{
while(temp -> link != NULL)
{
temp = temp -> link;
}
temp -> link = temp2;
temp2 -> link = NULL;
}
}
void display(void)
{
temp = head;
while(temp != NULL)
{
printf("\tRoll No. : %d\tAge : %d\n", temp -> roll, temp -> age);
temp = temp -> link;
}
}
void search(int r)
{
temp = head;
while(temp != NULL)
{
if(temp -> roll == r)
{
printf("\n\tRoll Number Exists!\n");
printf("\tRoll No. : %d\tAge : %d\n", temp -> roll, temp -> age);
return;
}
temp = temp -> link;
}
printf("\n\n\tSpecified Roll Number Does Not Exist!");
}
void addAtBegin(int r,int a)
{
temp2 = (std *)malloc(sizeof(std));
temp2 -> roll = r;
temp2 -> age = a;
temp2 -> link = head;
head = temp2;
}
void delRoll(int r)
{
temp = head -> link;
temp2 = head;
if(temp2 -> roll == r)
{
head = temp;
free(temp2);
return;
}
while(temp -> roll != r)
{
temp = temp -> link;
temp2 = temp2 -> link;
}
if(temp == NULL)
{
printf("\n\tRoll Number Does Not Exist");
return;
}
temp = temp -> link;
free(temp2 -> link);
temp2 -> link = temp;
}
void addAtLoc(int pos,int r,int a)
{
add = (std *)malloc(sizeof(std));
add -> roll = r;
add -> age = a;
if(pos == 1)
{
add -> link = head;
head = add;
return;
}
temp = head;
for(i = 0;i < pos - 2;i++)
{
temp = temp -> link;
if(temp == NULL)
{
printf("\n\tLocation Does Not Exist");
return;
}
}
temp2 = temp -> link;
temp -> link = add;
add -> link = temp2;
}
void addAftRoll(int fRoll,int r,int a)
{
temp = head;
while(temp -> roll != fRoll)
{
temp = temp -> link;
if(temp == NULL)
{
printf("\n\tRoll Number Does Not Exist");
return;
}
}
temp2 = temp -> link;
add = (std *)malloc(sizeof(std));
add -> roll = r;
add -> age = a;
temp -> link = add;
add -> link = temp2;
}
int main()
{
do
{
clrscr();
printf("\n\t================");
printf("\n\tSTUDENT DATABASE");
printf("\n\t================\n");
printf("\n\t1 - Apending Information About A Student");
printf("\n\t2 - Displaying Information Of All Students");
printf("\n\t3 - Searching Information About A Student");
printf("\n\t4 - Deleting Information About A Student According To Roll No");
printf("\n\t5 - Adding Information About A Student At Begining");
printf("\n\t6 - Adding Information About A Student At Any Location");
printf("\n\t7 - Adding Information About A Student After A Roll Number");
printf("\n\t8 - Exit");
printf("\n\n\tChoice : ");
fflush(stdin);
scanf("%d",&n);
switch(n)
{
case 1 :
clrscr();
printf("\n\n\tEnter Roll Number : ");
scanf("%d",&r);
printf("\n\n\tEnter Age : ");
scanf("%d",&a);
append(r,a);
getch();
break;
case 2 :
clrscr();
printf("\n\n\tInformation List\n\n");
display();
getch();
break;
case 3 :
clrscr();
printf("\n\n\tEnter The Roll Number You Want To Search : ");
scanf("%d",&r);
search(r);
getch();
break;
case 4 :
clrscr();
printf("\n\n\tEnter The Roll Number You Want To Delete : ");
scanf("%d",&r);
delRoll(r);
getch();
break;
case 5 :
clrscr();
printf("\n\n\tEnter Roll Number : ");
scanf("%d",&r);
printf("\n\n\tEnter Age : ");
scanf("%d",&a);
addAtBegin(r,a);
getch();
break;
case 6 :
clrscr();
printf("\n\n\tEnter Location : ");
scanf("%d",&pos);
printf("\n\n\tEnter Roll Number : ");
scanf("%d",&r);
printf("\n\n\tEnter Age : ");
scanf("%d",&a);
addAtLoc(pos,r,a);
getch();
break;
case 7 :
clrscr();
printf("\n\n\tEnter Roll Number After Which New Data Will Be Put : ");
scanf("%d",&fRoll);
printf("\n\n\tEnter Roll Number : ");
scanf("%d",&r);
printf("\n\n\tEnter Age : ");
scanf("%d",&a);
addAftRoll(fRoll,r,a);
getch();
break;
case 8 :
printf("\n\n\tThank You!");
getch();
break;
default :
printf("\n\n\tThe Specified Option Does Not Exist!");
getch();
break;
}
}while(n!=8);
return 0;
}