I need help to complet this program: Purpose : This program demonstrates the use of multi-linked lists. / This file contains the implementation file for the 2170_10_7b.h header file. This implementation uses a dynamic linked list structure implementing the MultiListClass object. The implementation uses nodes which have two pointer fields: one to point to the next record by name (nextName) and one to point to the next record by account number (nextNum). The linked list is also maintained with a dummy header node called head.

  1. include “2170_10_7b.h”
  2. include <iostream>
  3. include <cstddef> using namespace std; This is the structure of nodes in the list struct Node { TypeData name; Person’s name TypeInt number; Person’s account number TypePtr nextNum; Pointer to next record by account number TypePtr nextName; Pointer to next record by name };

Constructor for MultiListClass which sets up the dummy header node. MultiListClass::MultiListClass() { head is a private data member of the MultiListClass object head=new Node; head->nextName = NULL; head->nextNum=NULL;

}

Destructor function which deallocates memory set up to create linked list MultiListClass::MultiListClass() { TypePtr q = head->nextName; bool emptylist = q==NULL; while (!emptylist) { delete(head); head = q; q = q->nextName; emptylist = q==NULL; } delete (head); }

Function which receive a person’s name and account number from a client program, creates a node to store this information in, calls the insertNameList() function to insert this node in the correct position of the linked list ordered by customer name and lastly, calls the insertNumList() function to insert this node in the correct position of the linked list ordered by customer account number.

void MultiListClass::insertNode (TypeData newName, TypeInt newNum) IN: customer’s name IN: customer’s account number { TypePtr p;

p = new Node; p->name = newName; p->number = newNum; p->nextNum = NULL; p->nextName = NULL; insertNameList (p); insertNumList (p); }

Function to insert a node into a linked list ordered by customer name void MultiListClass::insertNameList (TypePtr p) IN: pointer which points to the node to be inserted into the list { initialize prev and current TypePtr prev = head; TypePtr current = head->nextName;

move prev and current until current is NULL or current is pointing to a node which contains a name that is larger than the name to be inserted into the list while (!insertName (current,p)) { prev = current; current = current->nextName; }

insert the node between prev and current prev ->nextName = p; p->nextName = current; }

Function to insert a node into a linked list ordered by customer account number void MultiListClass::insertNumList (TypePtr p) { Initialize prev and current TypePtr prev = head; TypePtr current = head->nextNum;

Move prev and current until current is NULL or is pointing to a node which contains a customer number that is larger than the customer number contained in the node to be inserted while (!insertNum (current, p)) { prev = current; current = current->nextNum; }

Insert the node between prev and current prev ->nextNum = p; p->nextNum = current; }

bool function which returns TRUE if current is NULL or if current is pointing to a node which contains a customer’s name that is larger than the name contained in the node to be inserted. bool MultiListClass::insertName (TypePtr q, TypePtr t) IN: pointer to the current position in the list to be examined IN: pointer to a node to be inserted { return ((q==NULL) || (q->name > t->name)); }

bool function which returns TRUE if the current pointer is at the end of the list or if the current pointer is pointing to a node with a number field greater than the number in the node to be inserted bool MultiListClass::insertNum(TypePtr q, TypePtr t) IN: pointer to the current position in the list to be examined IN: pointer to the node to be inserted { return ((q==NULL) || (q->number > t->number)); }

Function which prints out the data contained in a linked lists of customer data which is ordered by customer name Add code to traverse the list by customer name and print the information in each node. Use the following statement to print the information: cout << current->name << ‘t’ << current->number << endl; void MultiListClass:: nameTraverse () { Add code to traverse the list by customer name and print the information in each node. Use the following statement to print the information: cout << current->name << ‘t’ << current->number << endl; }

Function which prints out the data contained in a linked list of customer data which is ordered by customer account numbers. Add code to traverse the list by account number and print the information in each node. Use the following statement to print the information: cout << current->number << ‘t’ << current->name << endl; void MultiListClass::numTraverse () { Add code to traverse the list by account number and print the information in each node. Use the following statement to print the information: cout << current->number << ‘t’ << current->name << endl; }

Leave a Reply