Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Help Program Specification: 1. Read data for names and weights for 15 people

ID: 3811935 • Letter: C

Question

C++ Help

Program Specification:

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, like in names.txt.
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

Grading:
Well-Formatted Correct Output: 10%
Correct Algorithm/Design: 80%
Good self documenting / commented code &
Readability (This should be easy points) 10%

names.txt

Explanation / Answer

// CPP Program to Implement Doubly Linked List

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>
// Node Declaration

using namespace std;
struct node
{
int weight;
string name;
struct node *next;
struct node *prev;
}*first,*last;

// Class Declaration

class dllsort
{
public :
dllsort()
{
first=NULL;
last=NULL;
}
void addbyname(struct node *);
void addbyweight(struct node *);
void printbyname();
void printbyweight();


};
int main()
{
ifstream inpfile("names.txt");
string line, name;
dllsort obj;
int weigt;
for(int i=0;getline(inpfile,line);i++)
{
if(i%2)
{
weigt=atoi(line.c_str());
struct node *nd;
nd=new(struct node);
nd->name=name;
nd->weight=weigt;
cout<<name;
cout<<weigt;
cout<<nd->name;
cout<<nd->weight;

obj.addbyname(nd);
obj.addbyweight(nd);
}
else
   name=line;

}
obj.printbyname();
obj.printbyweight();

return 0;
}

void dllsort::addbyname(struct node *tmp)
{
struct node * tm ,*tm1;
//tm=first;
if(first==NULL)
{
first=tmp;
first->next=NULL;
first->prev=NULL;
}
else
{
tm=first;
if(strcmp(tm->name, tmp->name))
{
first=tmp;
first->next=tm;
}
else
{
tm1=tm;
tm=tm->next;
for(;tm->next!=NULL;tm=tm->next,tm1=tm1->next)
{
if(strcmp(tm->name, tmp->name))
{
tm1->next=tmp;
tmp->next=tm;
}
}
if(strcmp(tm->name, tmp->name))
{
tm->next=tmp;
tmp->next=NULL;
}

}
}


}

void dllsort:: addbyweight(struct node *tmp)
{
struct node * tm,*tm1;
tm=last;
if(last==NULL)
{
last=tmp;
last->prev=NULL;
last->next=NULL;
}
else
{
if(last->weight>tmp->weight)
{
tmp-prev=last;
last->prev=NULL;

}
else
{
tm=last;
for(tm1=last->prev;tm->prev!=NULL;tm=tm->prev,tm1=tm1->prev)
{

if(tm->weight>tmp->weight)
{
tmp-prev=tm;
tm1->prev=tmpL;

}
}


}
}
}

void dllsort:: printbyname()
{
struct node * tmp;
tmp=first;
if(tmp==NULL)
cout<<" list is empty ";
else
{
cout<<" Names & weights sorted(ascending) by name. : ";
while(tmp->next!=NULL)
{
cout<<tmp->name<<" - ";
cout<<tmp->weight<<", ";
tmp=tmp->next;
}

}
}

void dllsort:: printbyweight()
{
struct node * tmp;
tmp=last;
if(tmp==NULL)
cout<<" list is empty ";
else
{
cout<<" Names & weights sorted(ascending) by name. :";
while(tmp->prev!=NULL)
{
cout<<tmp->name<<" - ";
cout<<tmp->weight<<", ";
tmp=tmp->prev;
}

}
}