# include <iostream.h>
# include <stdlib.h>
using namespace std;
struct student
{
char student_name[30];
char ID_NO[20];
struct student* previous;
struct student* next;
};
void Menu();
void Show_List(struct student*);
struct student* Add_Student(char [], char [], struct student*);
struct student* Erase_Student(char [], struct student*);
int main(void)
{
char student_name[30];
char ID_NO[20];
struct student* first = NULL;
struct student* node0 = NULL;
int x = 0;
int opt;
Menu();
for(;
{
cout<<"\n1- Enter student information:";
cout<<"\n2- Search Students by ID:";
cout<<"\n3- Search Students by name:";
cout<<"\n4- Delete Students information:";
cout<<"\n5- Print all students:";
cout<<"\n6- Quit:\n";
cin>>opt;
switch(opt)
{
case 1:
cout<<"\nStudent ID:";
cin>>ID_NO;
cout<<"\nStudent Name:";
cin>>student_name;
node0 = Add_Student(student_name, ID_NO , first);
first = node0;
break;
case 5:
Show_List(first);
break;
default:
cout<<"\nRecord not found\n";
Menu();
break;
case 6:
return 0;
case 3:
cout<<"Enter the name:";
cin>>student_name;
Show_List(first);
case 2:
cout<<"Enter the ID:";
cin>>ID_NO;
Show_List(first);
}
}
cin>>x;
}
void Menu()
{
}
void Show_List(struct student* firstNode)
{
struct student* firstNodeCopy = firstNode;
int number = 0;
if(firstNode == NULL)
cout<<"\nThe list is empty.\n";
cout<<"Students ID:"<<"\t\t Students Name";
cout<<"\n-------------"<<"\t\t --------------";
while(firstNodeCopy)
{
printf(" \t\t", ++number, firstNodeCopy->student_name,"\t");
printf("\n%s \t\t\t%s\n", firstNodeCopy->ID_NO);
firstNodeCopy = firstNodeCopy->next;
}
}
struct student* Add_Student(char name_1[], char ID[], struct student* firstNode)
{
struct student* start = firstNode;
struct student* last = NULL;
struct student* addNode = (struct student*) malloc(sizeof(struct student));
if(firstNode == NULL)
{
firstNode = (struct student*) malloc(sizeof(struct student));
strcpy(firstNode->student_name, name_1);
strcpy(firstNode->ID_NO, ID);
firstNode->next = NULL;
firstNode->previous = NULL;
return firstNode;
}
else
{
strcpy(addNode->student_name, name_1);
strcpy(addNode->ID_NO, ID);
while(start)
{
if(strcmp(addNode->student_name, start->student_name) > 0)
{
if(start->next == NULL)
{
start->next = addNode;
addNode->previous = start;
addNode->next = NULL;
return firstNode;
}
else
{
last = start;
start = start->next;
}
}
if(strcmp(addNode->student_name, start->student_name) < 0)
{
if(last == NULL)
{
addNode->next = start;
start->previous = addNode;
return addNode;
}
else
{
addNode->next = start;
addNode->previous = last;
last->next = addNode;
start->previous = addNode;
return firstNode;
}
}
if(strcmp(addNode->student_name, start->student_name) == 0)
{
addNode->next = start;
start->previous = addNode;
return addNode;
}
else
{
addNode->next = start;
addNode->previous = last;
last->next = addNode;
start->previous = addNode;
return firstNode;
}
}
if(start->next == NULL)
{
start->next = addNode;
addNode->previous = start;
addNode->next = NULL;
return firstNode;
}
else
{
last = start;
start = start->next;
}
}
}
struct student* Erase_Student(char ID[], struct student* firstNode)
{
struct student* start = firstNode;
struct student* last = NULL;
if(start == NULL)
{
cout<<"\nThe list is empty.\n";
return NULL;
}
else
{
while(start)
{
if(strcmp(ID, start->ID_NO) == 0)
{
if(last == NULL)
{
start = start->next;
return start;
}
else
{
last->next = start->next;
return firstNode;
}
}
else
{
last = start;
start = start->next;
}
}
cout<<"\nYou entered a WRONG personal ID number to erase!!! Please try again.\n";
return firstNode;
}
}
-- Join us at facebook: https://www.facebook.com/VU.Study.Corner
Group Link: http://groups.google.com/group/VU-Study-Corner?hl=en
Group Rules: http://groups.google.com/group/VU-Study-Corner/web/group-rules
Unsubscribe: VU-Study-Corner+unsubscribe@googlegroups.com
Adult contents, Spamming, Immoral & Rudish talk, Cell number, Websites & Groups links specially in paper days are strictly prohibited and banned in group.
No comments:
Post a Comment