In: Computer Science
Program Specification: (Visual Studio C++)
1. Read data for names and weights for 15 people from the console
where there is a name on a line followed by a weight on the next
line.
2. Your program will build a list for the data maintained in
ascending order based on both name and weight via a doubly linked
list.
3. This dll will use one pointer to keep weights in sorted order,
and use the other link to keep names on sorted order.
4. You need to build the list as you go maintaining this ordering,
so at any time a print method was called it would print the related
field in order. (This means nodes are added to the list in sorted
order, elements are not added to the list followed by a sort called
on the list.)
For example after 3 elements are added for (Name –
Weight):
Michael – 275, Tom – 150, Abe – 200.
Output:
Names & weights sorted(ascending) by name. : Abe – 200, Michael
– 275, Tom - 150
Names & weights sorted(ascending) by weight. : Tom – 150, Abe –
200, Michael - 275
Jim
150
Tom
212
Michael
174
Abe
199
Richard
200
April
117
Claire
124
Bobby
109
Bob
156
Kevin
145
Jason
182
Brian
150
Chris
175
Steven
164
Annabelle
99
#include<iostream>
using namespace std;
struct data{
string name; // name to
be stored
double weight; //
weight to be stored
struct data *nw; // pointer to next node
with greater weight
struct data *nn; // pointer to next node
with next name in ascending order
};
void print(struct data *t1,struct data *t2)
{
//struct data *t1 = head_nm;
//struct data *t2 = head_wt;
cout<<"Names & weights sorted(ascending) by
name. :";
while(t1!=NULL)
{
cout<<t1->name<<" -
"<<t1->weight<<",";
t1=t1->nn;
}
cout<<endl;
cout<<"Names & weights sorted(ascending) by
weight. :";
while(t2!=NULL)
{
cout<<t2->name<<" -
"<<t2->weight<<",";
t2=t2->nw;
}
cout<<endl;
}
int main()
{
int flag = 1;
struct data *head_wt = NULL; // head node
for weights
struct data *head_nm = NULL; // head
node for names
string inp_n;
double inp_wt;
while(flag!=3)
{
cout<<"Please enter the
choices from below : \n";
cout<<"Type 1 : for entering
data\n";
cout<<"Type 2 : for
print\n";
cout<<"Type 3 : for
exit\n";
cin>>flag;
if(flag == 2 || flag == 3)
{
print(head_nm,head_wt);
continue;
}
cin>>inp_n;
cin>>inp_wt;
struct data *new_node = new
data;
new_node->name = inp_n;
new_node->weight = inp_wt;
new_node->nn = NULL;
new_node->nw = NULL;
struct data *temp = head_nm;
//for entering names
if(head_nm == NULL)
{
head_nm =
new_node;
}
else if( head_nm->name >
new_node->name)
{
new_node->nn
= head_nm;
head_nm =
new_node;
}
else
{
int flag =
0;
while(temp->nn!=NULL)
{
if(temp->nn != NULL &&
temp->nn->name > new_node->name)
{
struct data *temp2 =
temp->nn;
new_node->nn =
temp2;
temp->nn = new_node;
flag = 1;
break;
}
temp = temp->nn;
}
if(flag ==
0)
{
temp->nn = new_node;
}
}
// for weight addition
temp = head_wt;
if(head_wt == NULL)
{
head_wt =
new_node;
}
else if( head_wt->weight >
new_node->weight)
{
new_node->nw
= head_wt;
head_wt =
new_node;
}
else
{
int flag =
0;
while(temp->nw!=NULL)
{
if(temp->nw != NULL &&
temp->nw->weight > new_node->weight)
{
struct data *temp2 =
temp->nw;
new_node->nw =
temp2;
temp->nw = new_node;
flag = 1;
break;
}
temp = temp->nw;
}
if(flag ==
0)
{
temp->nw = new_node;
}
}
}
return 0;
}
Output :
Names & weights sorted(ascending) by name. : Abe – 200,
Michael – 275, Tom - 150,
Names & weights sorted(ascending) by weight. : Tom – 150, Abe –
200, Michael - 275,