All codes!
#include <iostream>
#include <cstring>
using namespace std;
struct student
{
int stID;
char stName[20];
int stG1;
int stG2;
int stG3;
student *next;
student *prev;
};
void sortList(student *ptr,int tp)
{
student *p;
if(tp == 0)
{
student *t;
for(p = ptr->next; p->next != NULL; p=p->next)
{
for(t = p->next;t != NULL; t=t->next)
{
if(p->stID > t->stID)
{
int stID,stG1,stG2,stG3;
char stName[20];
stID = p->stID;
p->stID = t->stID;
t->stID = stID;
stG1 = p->stG1;
p->stG1 = t->stG1;
t->stG1 = stG1;
stG2 = p->stG2;
p->stG2 = t->stG2;
t->stG2 = stG2;
stG3 = p->stG3;
p->stG3 = t->stG3;
t->stG3 = stG3;
strcpy(stName,t->stName);
strcpy(t->stName,p->stName);
strcpy(p->stName,stName);
}
}
}
}
if(tp = 1)
{
student *t;
for(p = ptr->next; p->next != NULL; p=p->next)
{
for(t = p->next;t != NULL; t=t->next)
{
if(strcmp(p->stName,t->stName) !=0)
{
int stID,stG1,stG2,stG3;
char stName[20];
stID = p->stID;
p->stID = t->stID;
t->stID = stID;
stG1 = p->stG1;
p->stG1 = t->stG1;
t->stG1 = stG1;
stG2 = p->stG2;
p->stG2 = t->stG2;
t->stG2 = stG2;
stG3 = p->stG3;
p->stG3 = t->stG3;
t->stG3 = stG3;
strcpy(stName,t->stName);
strcpy(t->stName,p->stName);
strcpy(p->stName,stName);
}
}
}
}
if(tp = 2)
{
student *t;
for(p = ptr->next; p->next != NULL; p=p->next)
{
for(t = p->next;t != NULL; t=t->next)
{
int a1 = (p->stG1+p->stG2+p->stG3)/3;
int a2 = (t->stG1+t->stG2+t->stG3)/3;
if(a1 > a2)
{
int stID,stG1,stG2,stG3;
char stName[20];
stID = p->stID;
p->stID = t->stID;
t->stID = stID;
stG1 = p->stG1;
p->stG1 = t->stG1;
t->stG1 = stG1;
stG2 = p->stG2;
p->stG2 = t->stG2;
t->stG2 = stG2;
stG3 = p->stG3;
p->stG3 = t->stG3;
t->stG3 = stG3;
strcpy(stName,t->stName);
strcpy(t->stName,p->stName);
strcpy(p->stName,stName);
}
}
}
}
}
void addStudent(student *ptr)
{
student *p;
student *t;
char stName[20];int stID,stG1,stG2,stG3;
cout<<"Enter ID:"<<endl;
cin>>stID;
cout<<"Enter Name:"<<endl;
cin>>stName;
cout<<"Enter grades:"<<endl;
cin>>stG1>>stG2>>stG3;
p = new student;
strcpy(p->stName,stName);
p->stID = stID;
p->stG1 = stG1;
p->stG2 = stG2;
p->stG3 = stG3;
p->next = NULL;
t = ptr;
while(t->next != NULL)
{
t = t->next;
}
p->prev = t;
t->next = p;
sortList(ptr,0);
}
void removeStudent(student *ptr,int stID)
{
if(ptr == NULL)
{
cout<<"Linked list is empty.";
return;
}else{
student *p;
student *t;
t = ptr;
while(t != NULL)
{
if(t->stID = stID)
{
if(t == ptr)
{
ptr=t->next;
(t->next)->prev=NULL;
delete t;
}else{
if(t->next == NULL)
{
p=t->prev;
p->next=NULL;
delete t;
}else{
p=t->next;
p->next=t->next;
p=t->next;
p->prev=t->prev;
delete t;
}
}
}
cout<<"Target deleted successfully.";
return;
t=t->next;
}
}
}
void printList(student *ptr)
{
student *p = ptr->next;
while(p != NULL)
{
cout<<"StID:"<<p->stID<<" Name:"<<p->stName<<" G1:"<<p->stG1<<" G2:"<<p->stG2<<" G3:"<<p->stG3<<" Avg:"<<(p->stG1+p->stG2+p->stG3)/3<<endl;
p = p->next;
}
}
int main()
{
student *ptr;
ptr->next = NULL;
char c;
for(;;)
{
cout<<"Double Linked list"<<endl;
cout<<"1:Add new node."<<endl;
cout<<"2:Remove one student."<<endl;
cout<<"3:List in name order."<<endl;
cout<<"4:List in avg order."<<endl;
cout<<"5:Exit."<<endl;
cin>>c;
switch©
{
case '1':
addStudent(ptr);
break;
case '2':
cout<<"Enter ID:"<<endl;
int id;
cin>>id;
removeStudent(ptr,id);
break;
case '3':
sortList(ptr,1);
printList(ptr);
break;
case '4':
sortList(ptr,2);
printList(ptr);
break;
case '5':
return 0;
}
}
return 0;
}