Пример #1
0
//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;
    }
Пример #2
0
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;
}
Пример #3
0
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;
		}
	}
}
Пример #4
0
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;
}
Пример #5
0
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();
}
Пример #6
0
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();
}
Пример #7
0
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++;
			}
		}
	}
}
Пример #8
0
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();
}
Пример #9
0
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));
}
Пример #10
0
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;
}
Пример #11
0
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;
    }
}