Our teacher never actually teaches us the code making it hard for me to understand how to do the assignment. He only teaches pseudocode and this the first time I actually seen code for link list in c++. So any help would be greatly appreciated.
This is the assignment I have to do.
1. Using the linked list program given in class , alter the
program to input twenty five FLOATS , sorting them
as they are inserted into the list. Print out
the sorted list.
2. Create a linked lists with two info fields, a last name
and a G.P.A. (Ex. Smith, 3.98) Sort the list as it is
created, by G.P.A. (use 1.) Print out list.
Then, delete all entries whose G.P.A. is
less than 2.5. Print out the new list
Finally, create a new list out of this modified
list , this time sorting alphabetically
This is the code we are given by our teacher.
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
|
// This program introduces simple linked lists using pointers //
// It creates a list of nodes having an id and a grade field and computes the //
// average of the grades. It also prints out the nodes in the order in which they //
// are added to the list..It does NOT sort the list..that is YOUR job!!! //
// D. Pinto FALL 2015 CS 501_112
#include <iostream>
using namespace std;
void getdata(int& ident, int& grade_value);
const int nil = 0;
class node_type // declaration of class//
{
public:
int id;
int grade;
node_type *next;
};
void main()
{
node_type *first, *p, *q, *newnode;
int i, ident, grade_value;
float sum, average;
sum = 0;
first = new node_type;
p = first;
getdata(ident, grade_value);
(*first).id = ident;
(*first).grade = grade_value;
(*first).next = nil;
for (i = 2; i <= 10; ++i)
{
getdata(ident, grade_value);
newnode = new node_type;
(*newnode).id = ident;
(*newnode).grade = grade_value;
(*newnode).next = nil;
//**********Links previous node to newnode******//
(*p).next = newnode;
p = newnode;
}
//**********Traverses list and prints out nodes in chronological order ******//
q = first;
cout << "The list contains \n";
while (q != nil)
{
cout << "ID is :" << (*q).id << "\n";
cout << "GRADE is :" << (*q).grade << "\n";
sum = sum + (*q).grade;
q = (*q).next;
}
average = sum / 10;
cout << "\nThe average of the grades is " << average << "\n";
}
void getdata(int& ident, int& grade_value)
{
cout << "Enter id and grade \n";
cin >> ident;
cout << ident << "\n";
cin >> grade_value;
cout << grade_value << "\n";
}
|