Name and Grade Sorted Linked List

So far, I have the program to read in the max number of points, students name, and total grade. I have inserted the names and grades into a link list. The names and grades are sorted in named order currently. Now, I need to have a link print the grades in sorted order. Also, I need a 3rd link in the same link list to of all students who have a grade below 200.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>

using namespace std;

ifstream inFile; //declare file stream
ofstream outputFile;

//define struct
class LList
{
     public:
     string Name;
     int Number;

     LList *next;
};

typedef LList *Node;
Node ListHead;

//define a newNode
LList *newNameNode()
{
    LList * temp;
    temp = new LList();
    temp -> Name = 'a';
    temp -> next = NULL;
    return temp;
}

LList *newGradeNode()
{
    LList * temp;
    temp = new LList();
    temp -> Number = 0;
    temp -> next = NULL;
    return temp;
}

// print list function
void printList(int max)
{
    Node c;
    c = ListHead; //assign c to head
    int num;
    int answer;

    while ( c )
    {
        outputFile << left << c -> Name   << setw(18);
        outputFile << left << c -> Number * 100 / max << setw(15);
        outputFile << left << c -> Number << setw(15);
        outputFile << endl;
        c = c -> next;
    }
}

//insert a node to link list
 void insert(string v, int n1)
{
    Node c, p, temp;

    c = ListHead;
    p = NULL;

    string s; //declare string variables
    int g;
    s = v;
    g = n1;

    //place items in ordered link list on item Name
    while ( c != NULL && c -> Name <= s)
    {
        p = c;
        c = c -> next;
    }

    if(p == NULL)
    {
            temp = newNameNode();
            temp -> Name = s;
            temp -> Number = g;
            temp -> next = c;
            ListHead = temp;
    }
    else if(s != p->Name)
    {
            temp = newNameNode();
            temp -> Name = s;
            temp -> Number = g;
            p -> next = temp;
            temp -> next = c;
    }
}

int main()
{
    //open input and output files
    inFile.open("LinkLnamesAndGrades.txt");
    outputFile.open("answer.txt");

    int n1;
    string v;
    int max;

    inFile >> max;

    while(inFile >> v )
    {
        inFile >> n1;
        insert(v, n1);
    }
    printList(max); //call print list function

    //close the files
    inFile.close();
    outputFile.close();

    return 0;
}
Last edited on
Input:

300
Taylor 287
Thomas 197
Brown 250
Lee 273
Reynolds 282
Lion 192
Mack 265
Lewis 176


Sorted in Named Order:
Brown	       83                250            
Lee            91                273            
Lewis          58                176            
Lion           64                192            
Mack           88                265            
Reynolds       94                282            
Taylor         95                287            
Thomas         65                197  
Topic archived. No new replies allowed.