//Prints the contents of the list in the format: NUM1,NUM2,...,LASTNUM //Empty string on empty list std::string SList::toString() const{ std::string listContents = ""; SLNode *p = head; std::stringstream ss; unsigned int count; if(p == NULL){ return ""; } count = 0; while(p != NULL){ if(count == 0){ ss << p->getContents(); //listContents += (std::to_string(p->getContents())); p = p->getNextNode(); count++; }else{ ss << ',' << p->getContents(); //listContents += (',' + std::to_string(p->getContents())); p = p->getNextNode(); } } listContents = ss.str(); return listContents; }
bool SList::removeFirst(int contents) { SLNode* toRemove = NULL; SLNode* nodeMark = head; SLNode* lagMark = NULL; for (unsigned int i = 0; i < size; i++){ if (nodeMark->getContents() != contents && nodeMark->getNextNode() != NULL){ lagMark = nodeMark; nodeMark = nodeMark->getNextNode(); } else if (nodeMark->getContents() == contents){ toRemove = nodeMark; } } if (toRemove == NULL) { return false; } if (lagMark == NULL){ removeHead(); } else if (toRemove->getNextNode() == NULL) { removeTail(); } else { size--; nodeMark = nodeMark->getNextNode(); lagMark->setNextNode(nodeMark); delete toRemove; } toRemove = NULL; nodeMark = NULL; lagMark = NULL; return true; }
bool SList::removeFirst (int target) { if (head == NULL) return false; else { SLNode* trailer = NULL; SLNode* leader = head; while (leader != NULL && leader->getContents() != target) { trailer = leader; leader = leader->getNextNode(); } if (leader == NULL) { return false; } else if (leader == head) { removeHead(); return true; } else { trailer->setNextNode(leader->getNextNode()); delete leader; --numNodes; return true; } } }
string SList::toString () const { stringstream ss; for (SLNode* i = head; i != NULL; i = i -> getNextNode()) { ss << i->getContents(); if (i -> getNextNode() != NULL) ss << ","; } string list = ss.str(); return list; }
string SList::toString() { stringstream list; SLNode* temp = head; if(head == NULL) { return list.str(); } else { while(temp->getNextNode() != NULL) { list << temp->getContents() << ","; temp = temp->getNextNode(); } list << temp->getContents(); } return list.str(); }
string SList::toString () const { //Need to include sstream for this. stringstream ss; //Fetch the contents. for (SLNode* i = head; i != NULL; i=i->getNextNode()) { ss << i->getContents(); if(i->getNextNode() != NULL) ss << ','; } return ss.str(); }
void SList::insert (int newContents) { if (head == NULL) { insertHead (newContents); } else if (head->getNextNode() == NULL) { if (newContents < head->getContents()) { insertHead(newContents); } else { insertTail(newContents); } } else { SLNode* trailer = NULL; SLNode* leader = head; while (leader->getNextNode() != NULL && newContents > leader->getContents()) { trailer = leader; leader = leader->getNextNode(); } if (leader->getNextNode() == NULL && newContents > leader->getContents()) { insertTail(newContents); } else { SLNode* theNode = new SLNode (newContents); theNode->setNextNode(leader); if (trailer == NULL) { head = theNode; numNodes++; } else { trailer->setNextNode(theNode); numNodes++; } } } }
string SList::toString() const { stringstream ss; if (head == NULL) { ss.str(""); } else { int i = 1; for (SLNode* n = head; n != NULL; n = n->getNextNode()) { ss << n->getContents(); if (i < size) ss << ","; i++; } } return ss.str(); }
string SList::toString () const { stringstream listStream; SLNode* current; current = head; if (head == NULL){ cout << "" << endl; } else { while (current != NULL) { listStream << current->getContents() << ","; current = current->getNextNode(); } } return(listStream.str().substr(0,listStream.str().length() - 1)); }
string SList::toString() const { string listContents; stringstream ss; if (head != NULL){ ss << head->getContents(); if (head->getNextNode() != NULL){ SLNode* currentNode = head->getNextNode(); for (unsigned int i = 1; i < size; i++){ ss << ","; ss << currentNode->getContents(); if (currentNode->getNextNode() != NULL){ currentNode = currentNode->getNextNode(); } } } } ss >> listContents; return listContents; }
void SList::insert(int contents) { if (head == NULL) { insertHead(contents); } else { size++; SLNode* newNode = new SLNode(contents); if (head->getContents() > contents){ SLNode* oldHead = head; head = newNode; head->setNextNode(oldHead); oldHead = NULL; } else { if (head->getNextNode() == NULL) { head->setNextNode(newNode); } else { SLNode* nodeMark = head->getNextNode(); SLNode* lagMark = NULL; for (unsigned int i = 0; i < size; i++) { if (nodeMark->getContents() < contents && nodeMark->getNextNode() != NULL) { lagMark = nodeMark; nodeMark = nodeMark->getNextNode(); } } if (lagMark == NULL) { head->setNextNode(newNode); newNode->setNextNode(nodeMark); } else if (nodeMark->getNextNode() == NULL) { nodeMark->setNextNode(newNode); } else { lagMark->setNextNode(newNode); newNode->setNextNode(nodeMark); } nodeMark = NULL; lagMark = NULL; } } newNode = NULL; } }