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

Please comment on this C++ existing program to help me what\'s going on, eg. suc

ID: 3591883 • Letter: P

Question

Please comment on this C++ existing program to help me what's going on, eg. such as //Create a node struct, //Create an add method for DLL, so on.

The descriptions of the program are below the code.

#include<iostream>
#include<fstream>
#include<string.h>

using namespace std;
int x = 0, n = 0;

// Create a node struct
struct node
{
node *next, *prev;
int weight;
char name[25];
}*head = NULL, *tail = NULL, *p = NULL, *pl = NULL, *r = NULL, *newp = NULL;

void create(char names[], int num)
{
newp = new node;
newp->weight = num;
strcpy_s(newp->name, names);
newp->next = NULL;
newp->prev = NULL;
if (x == 0)
{
head = newp;
p = head;
p->next = NULL;
x++;
}
else
{
p = head;
r = p;
if (newp->weight < p->weight)
{
newp->next = 0;
head = newp;
p = head;
do
{
p = p->next;
} while (p->next != NULL);
}
else if (newp->weight > p->weight)
{
while (p != NULL && newp->weight > p->weight)
{
r = p;
p = p->next;
if (p == NULL)
{
r->next = newp;
newp->next = NULL;
tail = newp;
break;
}
else if (newp->weight < p->weight)
{
r->next = newp;
newp->next = p;
if (p->next != NULL)
{
do
{
p = p->next;

} while (p->next != NULL);
}
break;
}
}
}
}
if (n == 0)
{
tail = newp;
pl = tail;
pl->prev = NULL;
n++;
}
else
{
pl = tail;
r = pl;
if (strcmp(newp->name, pl->name) < 0)
{
newp->prev = pl;
tail = newp;
pl = tail;
do
{
pl = pl->prev;
} while (pl->prev != NULL);
}
else if (strcmp(newp->name, pl->name) > 0)
{
while (pl != NULL &&strcmp(newp->name, pl->name) > 0)
{
r = pl;
pl = pl->prev;
if (pl == NULL)
{
r->prev = newp;
newp->prev = NULL;
head = newp;
break;
}
else if (strcmp(newp->name, pl->name) < 0)
{
r->prev = newp;
newp->prev = pl;
if (pl->prev != NULL)
{
do
{
pl = pl->prev;
} while (pl->prev != NULL);
}
break;
}
}
}
}
}
void printfrom_tail()
{
node *t = tail;
while (t != NULL)
{
cout << t->name << " ";
cout << t->weight << " ";
t = t->next;
}
cout << endl;

}

void printfrom_head()
{
node *t = head;
while (t != NULL)
{
cout << t->name << " ";
cout << t->weight << " ";
t = t->next;
}
cout << endl;
}
int main()
{

int i = 0, n, x, ch;
ifstream file, filel;
char name[25];
int wgt, count = 0;

//Read the file

filel.open("name.txt");
while (!filel.eof())
{
filel >> name;
filel >> wgt;
count = count + 1;
cout << " " << name << " " << wgt;
create(name, wgt);
}
filel.close();
cout << " Number of nodes = " << count;
cout << " Names&weights sorted(ascending) by name.: ";
printfrom_tail();
cout << " Names&weights sorted(ascending) by weight. : ";
printfrom_head();
return 0;

}

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.

Explanation / Answer

Here is your code with proper comments

/*******************************/

/*
* DoublyLL.cpp
*
* Created on: 14-Oct-2017
*      Author: kuhu
*/

#include<iostream>
#include<fstream>
#include<string.h>

using namespace std;   // to use cout/cin
int x = 0, n = 0;   // global variables

// Create a node struct to hold data and giving link to next and previous node
struct node
{
node *next, *prev;   // pointer to next and previous nodes
int weight;
char name[25];
}*head = NULL, *tail = NULL, *p = NULL, *pl = NULL, *r = NULL, *newp = NULL; //create global pointers


// create a node in doubly linked list
void create(char names[], int num)
{
   newp = new node;   // allocate memory dynamically for a node
   // assign values for node elements
   newp->weight = num;
   strcpy(newp->name, names);
   newp->next = NULL;
   newp->prev = NULL;
   //check if head is not created
   if (x == 0)
   {
       head = newp; //create head with the newly created node
       p = head;
       p->next = NULL;
       x++;
   }
   else
   {
       p = head;
       r = p;
       //check if new->weight is less than head->weight add the new node in the head position
       if (newp->weight < p->weight)
       {
           newp->next = 0;
           head = newp;
           p = head;
           do
           {
           p = p->next;
           } while (p->next != NULL);
       }
       //check if new->weight is greater than head->weight
       else if (newp->weight > p->weight)
       {
           //traverse through the link till newp->weight is greater than any node weight
           while (p != NULL && newp->weight > p->weight)
           {
               r = p;
               p = p->next;
               //if the link ends add the new node at the end
               if (p == NULL)
               {
                   r->next = newp;
                   newp->next = NULL;
                   tail = newp;
                   break;
               }
               //if new node weight find a node which weight is more than new node weight
               //add new node there
               else if (newp->weight < p->weight)
               {
                   r->next = newp;
                   newp->next = p;
                   if (p->next != NULL) //check the next node not null
                   {
                       do
                       {
                       p = p->next; //traverse through the link

                       } while (p->next != NULL);
                   }
                   break;
               }
           }
       }
   }
   //check if tail is not created
   if (n == 0)
   {
       tail = newp; //make tail as the new node
       pl = tail;
       pl->prev = NULL;
       n++; //increment n, saying tail is already created
   }
   else
   {
       pl = tail;
       r = pl;
       //check if new name is less than tail name, then update tail with new node
       if (strcmp(newp->name, pl->name) < 0)
       {
           newp->prev = pl;
           tail = newp;
           pl = tail;
           do
           {
           pl = pl->prev;   //traverse pl pointer till the beginning
           } while (pl->prev != NULL);
       }
       //check if new name is greater than tail name,
       else if (strcmp(newp->name, pl->name) > 0)
       {
           //traverse through the link till new name is greater than the node name
           while (pl != NULL &&strcmp(newp->name, pl->name) > 0)
           {
               r = pl;
               pl = pl->prev;
               //if pointer reaches to beginning add new node to the beginning
               if (pl == NULL)
               {
               r->prev = newp;
               newp->prev = NULL;
               head = newp;
               break;
               }
               //if node found where new name is greater than node name, then add the new node there
               else if (strcmp(newp->name, pl->name) < 0)
               {
                   r->prev = newp;
                   newp->prev = pl;
                   if (pl->prev != NULL)
                   {
                   do
                   {
                   pl = pl->prev; //traverse the pointer till the beginning
                   } while (pl->prev != NULL);
                   }
                   break;
               }
           }
       }
   }
}

//print the linked list name & weight for each node from the tail
void printfrom_tail()
{
node *t = tail; //create local pointer and assign with tail
while (t != NULL) //traverse the pointer through the link till beginning
{
cout << t->name << " ";
cout << t->weight << " ";
t = t->next;   // check this assignment if it is t=t->prev
}
cout << endl;

}

void printfrom_head()
{
node *t = head; //create local pointer and assign with head
while (t != NULL) //traverse the pointer through the link till end
{
cout << t->name << " ";
cout << t->weight << " ";
t = t->next; //go to next pointer
}
cout << endl;
}

// program starts from here
int main()
{

   int i = 0, n, x, ch; // unused variables
   ifstream file, filel; // create input file element (ifstream for input, ofstream for output)
   char name[25];
   int wgt, count = 0;

   filel.open("name.txt");   //open the file in read mode
   while (!filel.eof())    // while loop will continue till the end of file reading
   {
   filel >> name;    //first word of the file is assigning to name
   filel >> wgt;     // second word of file is assigning to wgt
   count = count + 1; // counting the no of nodes are being created
   cout << " " << name << " " << wgt;
   create(name, wgt);   // create a node with name and wgt read from file
   }
   filel.close();    // file close here
   cout << " Number of nodes = " << count;
   cout << " Names&weights sorted(ascending) by name.: ";
   printfrom_tail();   // printing from tail of doubly linked list
   cout << " Names&weights sorted(ascending) by weight. : ";
   printfrom_head(); // printing from head of doubly linked list
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote