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

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 #include #include

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 << "\t"; cout << t->weight << "\t"; t = t->next; } cout << endl;

}

void printfrom_head() { node *t = head; while (t != NULL) { cout << t->name << "\t"; cout << t->weight << "\t"; 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 << "\t" << 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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!