Project: A Resistor Linked List Due: 4/17/2017, 10pm Project Description: In thi
ID: 3812389 • Letter: P
Question
Project: A Resistor Linked List Due: 4/17/2017, 10pm
Project Description:
In this project, you will create and maintain a resistor linked list. The linked list
structure template and all function prototypes are given in the header file "resistorList.h". In your program, you will prompt user to enter one of the three choices (I) insert (2) delete (3) exit. If an invaluable choies is entered, display error message "invaled Choice." and wait for next input.
Ifchoice is l, the program will ask the user to enter a resistor ID and value of resistance to be inserted into the end of list. Make sure you check thoroughly for relevant errors and display associated error messages including invalid resistor ID (ID number has to be a positive integer) and invalid resistance values (postie real number). lfthe resistor ID exists in the list already, display an error message and return to the main menu. Upon a successful insertion, the program will display the
resistor list and also the total equivalent resistance assuming all the resistors in the list are connected in series.
Ifchoice is 2 and the resistor list is not empty, the program will first display the current list and ask the user enter the resister ID to be deleted. Ifresister 1D is invalid, display an error message and return to main menu. Ifresister 1D is not found in the list, display an error message and return to main menu. The program will display the resulting resistor list and the new total equivalent resistance.
If choice is 3, program will terminate.
Explanation / Answer
#include "resistorList.h"
//insert node into linked list
void insert_node(resistor **head, resistor *node)
{
node->next = *head;
*head = node;
}
//printing list
void print_list(resistor *head)
{
resistor *current = head;
while(current != NULL)
{
cout<<"|"<<current->ID<<"|"<<current->value<<"| ";
current=current->next;
}
cout<<endl;
}
//calculating total series resistance
double total_resistance(resistor *head)
{
double total = 0.0;
resistor *temp = head;
while(temp != NULL)
{
total = total + temp->value;
temp = temp->next;
}
return total;
}
//checking ID present in list already.If present return true,else false.
bool find_ID(resistor *head, int ID)
{
resistor *temp = head;
while(temp != NULL)
{
if(temp->ID == ID)
return true;
temp = temp->next;
}
return false;
}
int main()
{
int choice;
int ID;
double value;
resistor *temp;
resistor *head = NULL;
while(1)
{
cout<<"Choose from below option"<<endl;
cout<<"(1) Insert (2) delete (3) exit"<<endl;
cin>>choice;
if(choice == 1)
{
while(1)
{
cout<<" enter Resistor ID : ";
cin>>ID;
if(ID < 0)
cout<<" Invalid ID "<<endl;
else
break;
}
if(find_ID(head,ID))
{
cout<<"ID already exist"<<endl;
continue;
}
while(1)
{
cout<<" enter Value of Resistor : ";
cin>>value;
if(value < 0.0)
cout<<" Invalid Value "<<endl;
else
break;
}
temp = new resistor; //creating empty node
temp->ID = ID;
temp->value = value;
temp->next = NULL;
insert_node(&head, temp);
print_list(head);
cout<<"Total resistance : "<<total_resistance(head)<<endl;
}
else if(choice == 2)
{
print_list(head);
cout<<" Enter resistor ID to be deleted : ";
cin>>ID;
if(ID < 0)
{
cout<<" Invalid ID"<<endl;
continue;
}
if(!find_ID(head,ID))
{
cout<<"ID not found in list"<<endl;
continue;
}
print_list(head);
cout<<"Total resistance : "<<total_resistance(head)<<endl;
}
else if(choice == 3)
break;
else
{
cout<<"Invalid Choice"<<endl;
continue;
}
}
return 0;
}
please refer below output for reference
Choose from below option
(1) Insert
(2) delete
(3) exit
1
enter Resistor ID : 5
enter Value of Resistor : 21.1
|5|21.1|
Total resistance : 21.1
Choose from below option
(1) Insert
(2) delete
(3) exit
1
enter Resistor ID : 3
enter Value of Resistor : 78.9
|3|78.9| |5|21.1|
Total resistance : 100
Choose from below option
(1) Insert
(2) delete
(3) exit
1
enter Resistor ID : 1
enter Value of Resistor : 45.99
|1|45.99| |3|78.9| |5|21.1|
Total resistance : 145.99
Choose from below option
(1) Insert
(2) delete
(3) exit
2
|1|45.99| |3|78.9| |5|21.1|
Enter resistor ID to be deleted : 4
ID not found in list
Choose from below option
(1) Insert
(2) delete
(3) exit
3
Process returned 0 (0x0) execution time : 58.398 s
Press any key to continue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.