0% found this document useful (0 votes)
31 views

#Include #Include Using Namespace Struct

This C++ program uses a linked list to store student data including name, ID, score, and grade. It allows the user to add new students to the sorted linked list, search for a student by name, update a student's score, delete a student, and print out the full list with student details and total length. The list is sorted based on student score and each student's grade is calculated and stored based on their score.

Uploaded by

Niraj Gohil
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

#Include #Include Using Namespace Struct

This C++ program uses a linked list to store student data including name, ID, score, and grade. It allows the user to add new students to the sorted linked list, search for a student by name, update a student's score, delete a student, and print out the full list with student details and total length. The list is sorted based on student score and each student's grade is calculated and stored based on their score.

Uploaded by

Niraj Gohil
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like