#include "intVektor.h" #include #include #include //Public intVektor::intVektor() { head = createNewNodes(0); sIze = 0; } intVektor::intVektor(int startSize) { head = createNewNodes(startSize); sIze = startSize; } intVektor::intVektor(const intVektor& rh) { sIze = rh.sIze; head = createNewNodes(sIze); intNode * currentPos = head; intNode * currentRh = rh.head; while(currentPos) { currentPos->data = currentRh->data; currentPos = currentPos->next; currentRh = currentRh->next; } } intVektor::~intVektor() { while(sIze > 0) removeLast(); } intVektor& intVektor::operator=(const intVektor& rh) { if(this != &rh) { if(sIze < rh.sIze) extendArray(rh.sIze - sIze); else if(sIze > rh.sIze) while(sIze > rh.sIze) removeLast(); intNode * currentPos = head; intNode * currentRh = rh.head; for(int i = 0; currentPos != 0; i++) { currentPos->data = currentRh->data; currentPos = currentPos->next; currentRh = currentRh->next; } } return *this; } int& intVektor::operator[](const int position) const { intNode * currentPos = head; for(int i = 0; i < position; i++) currentPos = currentPos->next; return currentPos->data; } const intVektor intVektor::operator+(const intVektor& rh) { intNode * currentPos = head; intNode * currentRh = rh.head; intVektor temp(rh.sIze); for(int i = 0; currentPos != 0; i++) { temp[i] = currentPos->data + currentRh->data; currentPos = currentPos->next; currentRh = currentRh->next; } return temp; } int intVektor::size() { return sIze; } //Private intVektor::intNode* intVektor::createNewNodes(int numberOfNodes) { intNode * first = 0; intNode * here; for(int i = 0; i < numberOfNodes; i++) { intNode * newNode; newNode = new intNode(); newNode->data = 0; if(i == 0) { first = newNode; here = first; here->next = 0; } else { here->next = newNode; here = newNode; here->next = 0; } } return first; } void intVektor::extendArray(int numberOfNodes) { intNode * newChain; newChain = createNewNodes(numberOfNodes); intNode * currentPos = head; if(head == 0) head = newChain; else { while(currentPos->next != 0) currentPos = currentPos->next; currentPos->next = newChain; } sIze += numberOfNodes; } void intVektor::removeLast() { intNode * CurrentPos = head; intNode * nearlyLast; if(sIze != 1) while(CurrentPos != 0) { if(CurrentPos->next != 0) nearlyLast = CurrentPos; CurrentPos = CurrentPos->next; } else nearlyLast = head; delete CurrentPos; nearlyLast->next = 0; sIze--; } //Other operators std::ostream& operator<<(std::ostream& os, intVektor rh) { os << "("; for(int i = 0; i < rh.size(); i++) os << "[" << rh[i] << "]"; os << ")"; return os; } std::istream& operator>>(std::istream& is, intVektor& rh) { std::string workString = ","; std::string inString; int pos = -1; int length = 0; int i = 0; is >> inString; workString.append(inString); while(((pos = workString.find( ",", pos + 1)) != workString.npos) && (i < rh.size())) { if((length = workString.find( ",", pos + 1)) == workString.npos) length = workString.size(); std::string stringTemp = workString.substr(pos + 1,length); int intTemp = atoi(stringTemp.data()); rh[i] = intTemp; i++; } return is; }