Sunteți pe pagina 1din 4

#include <iostream>

#include <string>
using namespace std;

struct studentInfo
{
string name;
int score;
int id;
string grade;
studentInfo * link;
};/
void main()
{
studentInfo *first, *current, *newNode, *last, *trailcurrent;
string name, A, B,C, grade;
int size, score, num;
bool found;
cout<<"enter student ID? -1 to exit"<<endl;
cin>>size;
first = NULL;

while(size != -1)
{
newNode = new studentInfo;
newNode->id = size;

cout<<"Name:"<<endl;
cin>>name;
cout<<"Score:"<<endl;
cin>>score;
newNode->name = name;
newNode->score = score;
if(newNode->score <= 70)
{
newNode->grade = 'C';
}
else if(newNode->score <= 80)
{
newNode->grade = 'B';
}
else
{
newNode->grade = 'A';
}

newNode->link = NULL;

if(first == NULL) //building sorted list


{
first = newNode;
last = newNode;
}
else
{ current = first;
found = false;
while(current != NULL && !found)
{
if( current->score >= score)
found = true;
else
{
trailcurrent = current;
current = current->link;
}
}
if (current == first)
{
newNode->link = first;
first = newNode;
}
else
{
trailcurrent-> link = newNode;
newNode->link = current;
}

}
cout<<"Enter next student id or -1";
cin>>size;

}//end of while

current = first; //search node


while(current != NULL)
{
cout<<current->id<<" : "<<current->name<<" "<<current-
>score<<" "<<current->grade<<endl;
current = current->link;
}
cout<<"Enter name to search"<<endl;
cin>>name;
if( first == NULL)
cout<<"List is empty"<<endl;
else
{
current = first;
found = false;
while( !found && current != NULL)
{
if (current->name == name)
{
found = true;
cout<<"enter marks to increse or
decrease"<<endl;
cin>>num;
current->score = current->score + num;
cout<<current->name<<"'s new score
is :"<<current->score;
}
else
{
current = current->link;
}
if ((current == NULL) && (!found))
{
cout<<"Name is not in the list"<<endl;
}
}//end of while
}//e o while

//delete node
cout<<"enter name to delete;"<<endl;
cin>>name;
if ( first == NULL)
cout<<"List is empty"<<endl;
else if ( first->name == name)
{
current = first;
first = first->link;
if( first == NULL)
last = NULL;
delete current;
}
else
{
found = false;
trailcurrent = first;
current = first->link;
while((!found) && (current != NULL))
{
if (current->name != name)
{
trailcurrent = current;
current = current->link;
}
else
found = true;
}
if (found)
{
trailcurrent->link = current->link;
if(last == current)
last = trailcurrent;
delete current;
}
else
cout<<"Name is not in the list"<<endl;
}
int length = 0;
//print and length
current = first;
while(current != NULL)
{
if(current->score <= 70)
{
current->grade = 'C';
}
else if(current->score <= 80)
{
current->grade = 'B';
}
else
{
current->grade = 'A';
}
length++;
cout<<current->id<<" : "<<current->name<<"
"<<current->score<<" "<<current->grade<<endl;
current = current->link;
}
cout<<"Total length of list is : "<<length;
}//eomain

S-ar putea să vă placă și