-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResistorList.cpp
118 lines (104 loc) · 2.63 KB
/
ResistorList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* File: ResistorList.cpp
* Author: Matthew
*
* Created on November 6, 2016, 5:50 PM
*/
#include "ResistorList.h"
ResistorList::ResistorList() {
setHead(NULL);
setTail(NULL);
total = 0;
}
ResistorList::~ResistorList() {
clear();
}
void ResistorList::clear(){
delete head;
setHead(NULL);
setTail(NULL);
total = 0;
}
void ResistorList::setHead(Resistor* r){
head = r;
}
void ResistorList::setTail(Resistor* r){
tail = r;
}
void ResistorList::addResistor(Resistor* r){
if(head==NULL){
setHead(r);
setTail(r);
total++;
return;
}
r->setPrev(tail);
tail->setNext(r);
setTail(r);
total++;
return;
}
Resistor* ResistorList::getR(string name){
for(Resistor *cur = head; cur!=NULL; cur = cur->getNext())
if(cur->getName()==name) return cur;
return NULL;
}
int ResistorList::getSize(){
return total;
}
double ResistorList::inverseSum(){
double sum;
for(Resistor *cur = head; cur!=NULL; cur = cur->getNext())
sum += (1/(cur->getResistance()));
return (1/sum);
}
int* ResistorList::getOtherNodes(int id){
if(head==NULL) return NULL;
int* otherNodes;
int count = 0;
for(Resistor *cur = head; cur!=NULL; cur = cur->getNext()){
if(id==cur->getP1())
otherNodes[count] = cur->getP2();
else
otherNodes[count] = cur->getP1();
count++;
}
otherNodes[count] = -1; //To stop at higher level
return otherNodes;
}
double* ResistorList::getResistors(){
if(head==NULL) return NULL;
int count=0;
double* resVals;
for(Resistor *cur = head; cur!=NULL; cur = cur->getNext()){
resVals[count] = cur->getResistance();
count++;
}
return resVals;
}
void ResistorList::deleteR(string name){
Resistor* temp = getR(name);
if(temp==NULL) return; //Shouldn't happen, already checked
total--;
if(temp==head){
setHead(temp->getNext()); //Second is head
head->setPrev(NULL);
temp->setNext(NULL); //Don't want to delete the rest of the list
delete temp;
return;
}
if(temp==tail){
setTail(temp->getPrev());
delete temp; //next is NULL, no problem
tail->setNext(NULL);
return;
}
(temp->getPrev())->setNext(temp->getNext());
(temp->getNext())->setPrev(temp->getPrev());
temp->setNext(NULL);
delete temp;
}
void ResistorList::printAll(){
for(Resistor *cur = head; cur!=NULL; cur = cur->getNext())
cur->print();
}