program for student database!

This is a working code of student data base, capable of basic functions like Add new record, Delete existing record, Search, Modify, View all, Exit.

#include
#include

struct stud
{
int roll_no;
char name[80], branch[5], hostel[10], dob[15];
int year; // Year of joining

struct stud *next;
};

typedef struct stud student;

void display(student *x)
{
printf("Roll Number: %d\n",x->roll_no);
printf("Name: %s\n",x->name);
printf("Branch: %s\n",x->branch);
printf("Hostel: %s\n",x->hostel);
printf("DOB: %s\n",x->dob);
printf("Year of joining: %d\n",x->year);
}

student *find(student *head, int roll)
{
student *x;

x=head;

while(x!=NULL)
{
if(x->roll_no==roll)
break;
x=x->next;
}

return x;
}

student *add(student *head)
{
student *temp, *x;

temp=(student*)malloc(sizeof(*head));

printf("Add new record-----------------------\n");

if(temp==NULL)
{
printf("Error! Unable to allocate memory space!\n");
return head;
}

printf("Roll number: ");
scanf("%d",&(temp->roll_no));
printf("Name: ");
scanf("%s",temp->name);
printf("Branch: ");
scanf("%s",temp->branch);
printf("Hostel: ");
scanf("%s",temp->hostel);
printf("DOB: ");
scanf("%s",temp->dob);
printf("Year of joining: ");
scanf("%d",&(temp->year));

/*	if(find(head,temp->roll_no)==NULL)
{
temp->next=head;
return temp;
}
else
{
printf("Error! Duplicate record exists!\n");
return head;
}*/

x=head;
if(head==NULL)
{
temp->next=head;
return temp;
}
if(temp->roll_noroll_no)
{
temp->next=head;
printf("Placing current record at head.\n");
return temp;
}
while(x!=NULL)
{
if(x->roll_no==temp->roll_no)
{
printf("Error! Duplicate Record Exists!\n");
return head;
}
if((x->roll_noroll_no)&&(x->next->roll_no>temp->roll_no))
{
temp->next=x->next;
x->next=temp;
return head;
}

x=x->next;
}
}

student *delete(student *head)
{
int seek;
student *x;
student *temp;

printf("Delete student record----------------------\n");

x=head;

printf("Enter the roll number to be deleted: ");
scanf("%d",&seek);

if(head->roll_no==seek)
{
temp=head->next;
free(head);
head=temp;

return head;
}

while(x->next!=NULL)
{
if(x->next->roll_no==seek)
break;
else
x=x->next;
}

if(x->next==NULL)
{
printf("Error! Unable to locate record!\n");
return head;
}

temp=x->next->next;
free(x->next);
x->next=temp;

return head;
}

int modify(student *head)
{
int seek;
student *x;

printf("Modify Student Record---------------------\n");

x=head;

printf("Enter the roll number to be modified: ");
scanf("%d",&seek);

while(x!=NULL)
{
if(x->roll_no==seek)
break;
else
x=x->next;
}

if(x==NULL)
{
printf("Error! Unable to locate record!\n");
return 1;
}

printf("New Roll number: ");
scanf("%d",&(x->roll_no));
printf("New Name: ");
scanf("%s",x->name);
printf("New Branch: ");
scanf("%s",x->branch);
printf("New Hostel: ");
scanf("%s",x->hostel);
printf("New DOB: ");
scanf("%s",x->dob);
printf("New Year of joining: ");
scanf("%d",&(x->year));

return 0;
}

int search(student *head)
{
int seek;
student *x;

printf("Search Student Record---------------------\n");

x=head;

printf("Enter the roll number to be searched: ");
scanf("%d",&seek);

while(x!=NULL)
{
if(x->roll_no==seek)
break;
else
x=x->next;
}

if(x==NULL)
{
printf("Error! Unable to locate record!\n");
return 1;
}

display(x);

return 0;
}

void view(student *head)
{
student *x;

x=head;

printf("View all records----------------------------\n");

while(x!=NULL)
{
display(x);
printf("\n");

x=x->next;
}
}

int main()
{
int choice;
student* head;

printf("\nStudent Database Program---------------------------\n");

head=NULL;

do
{
if(choice!='\n')
printf("\nMain Menu-----------------\n1 Add new entry\n2 Delete entry\n3 Search\n4 Modify\n5 View all records\n6 Exit");

printf("\nEnter your choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1:
head=add(head);
break;
case 2:
head=delete(head);
break;
case 3:
search(head);
break;
case 4:
modify(head);
break;
case 5:
view(head);
break;
case 6:
break;
default:
printf("Error! Unknown command!\n");
break;
}
} while(choice!=6);

return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s