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

Program Specification: 1. Read data for names and weights for 15 people from the

ID: 3677091 • Letter: P

Question

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

Explanation / Answer

---------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------------------------------

In other words, this is like two lists, one sorted by name and one sorted by weight, but you're supposed to have two pointers within each node.Here are the classes and the insert() method.

  #include<stdio.h>  #include<conio.h>  #include<iostream>  using namespace std;    int c = 0;    struct node  {          char name[20];          int weight;        node *next;          node *prev;      }*head = NULL, *tail = NULL, *p = NULL, *r = NULL, *np = NULL;     node *start_ptr = NULL;    void create(int x)  {      np = new node;        np->weight = x;      np->next = NULL;      np->prev = NULL;      if (c == 0)      {          tail = np;          head = np;          p = head;          p->next = NULL;          p->prev = NULL;          c++;      }      else      {          p = head;          r = p;      if (np->weight < p->weight)      {          np->next = p;          p->prev = np;          np->prev = NULL;          head = np;          p = head;          do          {              p = p->next;          }          while (p->next != NULL);          tail = p;      }      else if (np->weight > p->weight)      {          while (p != NULL && np->weight > p->weight)          {              r = p;              p = p->next;          if (p == NULL)          {              r->next = np;              np->prev = r;              np->next = NULL;              tail = np;              break;          }          else if (np->weight < p->weight)          {               r->next = np;              np->prev = r;              np->next = p;              p->prev = np;              if (p->next != NULL)              {                  do                  {                      p = p->next;                  }                  while (p->next !=NULL);              }              tail = p;              break;           }         }       }     }  }    //void traverse_tail()  //{  //    node *t = tail;  //    while (t != NULL)  //    {  //        cout<<t->weight<<"	";  //        t = t->prev;  //    }  //    cout<<endl;  //}    void traverse_head()  {      node *t = head;      while (t != NULL)      {          cout<<t->weight<<"	";          t = t->next;      }      cout<<endl;  }      void print_node()    {      node *temp;      temp = start_ptr;      if(temp == NULL) cout << "Empty List!" << endl;      while(temp != NULL)        {          if(temp == NULL) cout << "Empty List!" << endl;                        cout << "Names & weights sorted(ascending) by name. : ";                              cout << "Name   : " << temp->name << endl;          cout << "Weight : " << temp->weight << endl;                      cout << "Names & weights sorted(ascending) by weight. :  ";            cout << endl;            temp = temp->next;         }               }           int main()  {      int i = 0, n, x, ch;      cout<<"Enter the number of people:  ";      cin>>n;        while (i < n)      {          cout<<" Enter Weights:  ";          cin>>x;          create(x);          i++;      }              //print_node();                cout << "Output:  ";     /* cout<<" Traversing Doubly Linked List head first ";*/      traverse_head();      /*cout<<" Traversing Doubly Linked List tail first ";      traverse_tail();*/      getch();            system("pause");          return 0;  }  

---------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------------------------------

In other words, this is like two lists, one sorted by name and one sorted by weight, but you're supposed to have two pointers within each node.Here are the classes and the insert() method.

                                                                                                                   
  1.   class Node {  public:      Node(const string &p_name, unsigned p_weight) :          name(p_name),          weight(p_weight),          nextByWeight(NULL),          nextByName(NULL)      {}        void print(ostream &os) const;      string name;      unsigned weight;       Node *nextByName;          // next node sorted by name      Node *nextByWeight;         // next node sorted by weight  };    class List {  public:  List() :  nameHead(NULL),  weightHead(NULL)      {}      ~List();      void insert(const string &name, unsigned weight);      void printByName(ostream &os) const;      void printByWeight(ostream &os) const;      private:      Node *nameHead;      Node *weightHead;  };    void List::insert(const string &name, unsigned weight)  {      Node *node = new Node(name, weight);      Node **pp, *p;// Insert into name list      for (pp = &nameHead; p = *pp; pp = &(p->nextByName))     {      if (p->name >= name)   {      break;          }      }      node->nextByName = p;      *pp = node;        // Insert into weight list      for (pp = &weightHead; p = *pp; pp = &(p->nextByWeight)) {          if (p->weight >= weight) {              break;          }      }      node->nextByWeight = p;      *pp = node;  }  
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote