Question

C++ Program 1. Read data for names and weights for 15 people. 2. Your program will...

C++ Program

1. Read data for names and weights for 15 people.

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include<iostream>
using namespace std;

typedef struct Node{
   string name;
   double weight;
   struct Node* next;
   struct Node* prev;
}Node;

class List{
   private:
       Node*head;
   public:
       List(){
           head = NULL;
       }
       void insertByName(string name , double weight){
           Node* newNode = new Node;
           newNode->name = name;
           newNode->weight = weight;
           newNode->next = NULL;
           newNode->prev = NULL;
  
          Node* current,*prev;
  
       if (head == NULL) {
       head = newNode;
       return;
           }
       if (head->name >= name) {
       newNode->next = head;
       newNode->next->prev = newNode;
       head = newNode;
       return;
       }
  
   current = head;
   while (current != NULL && current->name < name) {
       prev = current;
       current = current->next;
           }
   current = prev;
  
   newNode->next = current->next;
  
   if (current->next != NULL)
   newNode->next->prev = newNode;
  
   current->next = newNode;
   newNode->prev = current;
}
  
void insertByWeight(string name , double weight){
           Node* newNode = new Node;
           newNode->name = name;
           newNode->weight = weight;
           newNode->next = NULL;
           newNode->prev = NULL;
  
          Node* current,*prev;
  
       if (head == NULL) {
       head = newNode;
       return;
           }
       if (head->weight >= weight) {
       newNode->next = head;
       newNode->next->prev = newNode;
       head = newNode;
       return;
       }
  
   current = head;
   while (current != NULL && current->weight < weight){
               prev = current;
   current = current->next; }
current = prev;
   newNode->next = current->next;
  
   if (current->next != NULL)
   newNode->next->prev = newNode;
  
   current->next = newNode;
   newNode->prev = current;
   }
  
void display(){
   Node* current = head;
   while(current!=NULL){
       cout<<current->name<<"-"<<current->weight;
       if(current->next != NULL)cout<<",";
       current = current->next;
       }
   }

};

int main(){
   int n=3;
   string name;
   double weight;
   List list1 , list2;
  
   for(int i=0;i<n;i++){
       cout<<"Enter name : ";
       cin>>name;
cout<<"Enter weight : ";
cin>>weight;
list1.insertByName(name,weight);
list2.insertByWeight(name,weight);
   }
  
   cout<<"\nNames & weights sorted(ascending) by name. :";
   list1.display();
  
   cout<<"\nNames & weights sorted(ascending) by weight. :";
   list2.display();
  
   return 0;
}

//sample output

C:\Users\lshuManish\Documents ordereeddll.exe Enter name : Michael Enter weight : 275 Enter name : Tom Enter weight : 150 Ent

Add a comment
Know the answer?
Add Answer to:
C++ Program 1. Read data for names and weights for 15 people. 2. Your program will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Program 1. Read data for names and weights for 15 people. 2. Your program will...

    C++ Program 1. Read data for names and weights for 15 people. 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...

  • 3. Name Search Modify the Sorted Names program that you wrote for exercises #2 so it...

    3. Name Search Modify the Sorted Names program that you wrote for exercises #2 so it allows you to search the array for a specific name. design a flowchart for it exercises 2 is as follow: Sorted Names Design a program that allows the user to enter 20 names into a String array. Sort the array in ascending (alphabetical) order and display its contents. Pseudocode for number 3 as follow: Initialize i=0 Get the name to search for If i<20...

  • Do WITHOUT #include<sstream> C++ You are to write a program that will read in upto 15...

    Do WITHOUT #include<sstream> C++ You are to write a program that will read in upto 15 names, rearrange each person's name. The list has been collected from different places and the names are not in some standard format. A person's last name may appear last or it may appeaer first. A label has been associated with each name to identify where the last name appears. Either "surname" or "lastname" appears just before a person's last name. The other will be...

  • Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names...

    Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names and phone numbers. ·         a. Define a class contactList that can store a name and up to 3 phone numbers (use an array for the phone numbers). Use constructors to automatically initialize the member variables. b.Add the following operations to your program: i. Add a new contact. Ask the user to enter the name and up to 3 phone numbers. ii. Delete a contact...

  • I just need an algorithm for this please! I have C++ code for it but I dont know how to creat an ...

    I just need an algorithm for this please! I have C++ code for it but I dont know how to creat an algorithm .. CSE 1311-Project 4 Part I: Create and print out the two arrays: (Be sure to do this first) You are allowed to hard code these arrays into your program. You can also put the data into a file and read the information into the program. The data is as follows: 150 250 Anne Bob Ralph 305...

  • Write a program that uses pointer notation to dynamically allocate parallel array(s) large enough to hold...

    Write a program that uses pointer notation to dynamically allocate parallel array(s) large enough to hold a user-defined number of test scores and student names, using parallel arrays. After student names and corresponding scores are entered, the array(s) should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score for the group (do not include the lowest score in the calculation of average grade). The program should display the...

  • Write a program in C++ to: 1. Read up to 10 names and 10 test scores...

    Write a program in C++ to: 1. Read up to 10 names and 10 test scores from the keyboard (provide a method to end the input) (2 points); 2. print out the names and scores in a two column format (2 points); 3. use the function sort (you have to develop one) to sort the list according to test scores(4 points); 4. print out the sorted names and scores in a two column format (2 points)

  • read it carefully C++ Question: Write a program that dynamically allocates an array large enough to...

    read it carefully C++ Question: Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible. Input Validation: Do not...

  • C Programming, part B: (1 point) Write a second program (Program B) that builds a contiguous...

    C Programming, part B: (1 point) Write a second program (Program B) that builds a contiguous list (i.e., array- based), also of 100 elements (cells), each containing a random integer between 0 and 99. You encouraged to re-use as much code from Program A as you wish (this is called code re- sue and it is a good thing). This program must: 1) Build the contiguous list (i.e., the properly populated array) 2) Print out the contents of the list....

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT