#include stdafx.h #include iostream using namespace std;vo.docx
1. #include <stdafx.h>
#include <iostream>
using namespace std;
void getdata(int& ident, int& grade_value);
void sortlist(int& ident, int& grade_value);
const int nil = 0;
class node_type // declaration of class//
{
public:
int id;
int grade;
node_type *next;
};
void getdata(int& ident, int& grade_value);
void sortlist(int& ident, int& grade_value);
const int nil = 0;
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;
2. //**********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 ";
while (q != nil)
{
cout << "ID is :" << (*q).id << " ";
cout << "GRADE is :" << (*q).grade << " ";
sum = sum + (*q).grade;
q = (*q).next;
}
average = sum / 10;
cout << " The average of the grades is " << average << "
";
sortlist(ident, grade_value);
}
void getdata(int& ident, int& grade_value)
{
cout << "Enter id and grade ";
cin >> ident;
cout << ident << " ";
cin >> grade_value;
cout << grade_value << " ";
}
void sortlist(int& ident, int& grade_value);
{
if (first == nil)
first = newnode;
else if ((*first).next == nil)
{
3. if ((*newnode).id > (*first).id)
(*first).next == newnode;
else
{
(*newnode).next = first;
first = newnode;
}
}
else if ((newnode).id < (*first).id)
(*newnode).next = first;
first = newnode;
else
q = first;
p = (*q).next;
while ((*p).id < (*newnode).id && ((*p).next != nil))
{
q = p;
p = (*p).next;
}
if ((*p).id >= (*newnode).id)
(*q).next = newnode;
(*newnode).next = p;
}
else
{
(*p).next = newnode;
}
}
4. Using the linked list program given in class , alter the
program to input 10 integers, sorting them
as they are inserted into the list. Print out the sorted
list.
Compute the average of the sorted values and print this
out.
Then delete all values from the list which are LESS than the
average of the entries in the list
Then create a second list of 10 integers, sorting as the
list
is created. Print out this list.
Finally, merge the two lists together (i.e. the ORIGINAL first
and second lists), eliminating any duplicates
Print out the final merged list.
Solution
Here is the modified code:
#include <stdafx.h>
#include <iostream>
using namespace std;
void getdata(int& ident, int& grade_value);
void sortlist(int& ident, int& grade_value);
const int nil = 0;
class node_type // declaration of class//
5. {
public:
int id;
int grade;
node_type *next;
};
void getdata(int& ident, int& grade_value);
void sortlist(int& ident, int& grade_value);
const int nil = 0;
void main()
{
const int NUM_NODES = 10 ; // number of nodes to be
added
int ident, grade_value;
getdata(ident, grade_value);
node_type* first = new node_type { ident, grade_value,
nullptr } ;
node_type* last = first; // last keeps track of the last ode i n
the list.
// right now, there is only one node and last
== first
for( int i = 1 ; i < NUM_NODES ; ++i ) // add
(NUM_NODES-1) more nodes
{
getdata(ident, grade_value);
6. node_type* newnode = new node_type { ident,
grade_value, nullptr } ;
last->next = newnode; // append newnode to the end of the
list:
last = newnode; // and the node just added at the end
becomes the last node
}
double sum = 0 ;
std::cout << "The list contains ";
for( node_type* current = first ; current != nullptr ; current =
current->next )
{
// print out the id and grade in the current node
std::cout << "ID is : " << current->id << " GRADE is :
" << current->grade << ' ' ;
sum += current->grade ; // add the grade in the current
node
}
const double average = sum / NUM_NODES ;
std::cout << " The average of the grades is " << average <<
' ' ;
}
void getdata( int& ident, int& grade_value )
{
std::cout << "Enter id and grade ";
7. std::cin >> ident;
std::cin >> grade_value;
}
void sortlist(int& ident, int& grade_value);
{
if (first == nil)
first = newnode;
else if ((*first).next == nil)
{
if ((*newnode).id > (*first).id)
(*first).next == newnode;
else
{
(*newnode).next = first;
first = newnode;
}
}
else if ((newnode).id < (*first).id)
(*newnode).next = first;
first = newnode;
else
q = first;
p = (*q).next;
while ((*p).id < (*newnode).id && ((*p).next !=
nil))