Пример #1
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;
}
Пример #2
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;
		}
	}
}
Пример #3
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;
    }
Пример #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 << ",";
    }
    return ss.str();
}
Пример #5
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();
}
Пример #6
0
void SList::insertTail (int contents) {
    if(head == NULL){
        insertHead(contents);
    }
    else {
    SLNode* i = head;
    while (i->getNextNode() !=NULL) {
        i=i->getNextNode();
    }
    SLNode* node = new SLNode(contents);
    i->setNextNode(node);
    size++;
    }
}
Пример #7
0
void SList::insertTail (int newTail) {
    SLNode* temp = new SLNode(newTail);
    if (head == NULL) {
        insertHead(newTail);
    }
    else {
    SLNode* i = head;
    while (i->getNextNode() != NULL) {
        i = i->getNextNode();
    }
    i->setNextNode(temp);
    size++;
    }
}
Пример #8
0
void SList::insertTail (int contents) {
	if (head == NULL) {
		insertHead(contents);
	}
	else {
		SLNode* i = head;
		SLNode* newNode = new SLNode(contents);
		
		while (i->getNextNode() != NULL) {
			i = i->getNextNode();
		}
		i->setNextNode(newNode);
		++numNodes;
	}
}
Пример #9
0
void SList::insertTail (int contents) {
	SLNode* temp = new SLNode(contents);
	temp->setNextNode(NULL);
	
	if (head == NULL) {
		head = temp;
	} else {
		SLNode* tail = head;
			while (tail->getNextNode() != NULL) {
				tail = tail->getNextNode();
			}
		tail->setNextNode(temp);
	}
	size++;
}
Пример #10
0
void SList::insertTail(int value)
{
    if(head == NULL)
    {
        insertHead(value);
    }
    else
    {
        SLNode* newNode = new SLNode(value);
        SLNode* temp = head;
        while(temp->getNextNode() != NULL)
        {
            temp = temp->getNextNode();
        }
        temp->setNextNode(newNode);
        size++;
    }
}// create a new SLNode and attach at the end of list
Пример #11
0
void SList::removeTail () {
    if(head == NULL){
    
    }
    else if (head->getNextNode()==NULL) {
        removeHead();
    } else {
    SLNode* i = head;
    SLNode* j=NULL;
    while (i->getNextNode() !=NULL) {
        j=i;
        i=i->getNextNode();
    }
    delete i;
    j->setNextNode(NULL);
size--;
         }
}
Пример #12
0
void SList::removeTail () {
	if (head != NULL) {
		if (head->getNextNode() == NULL) {
			delete head;
			head = NULL;
		} else {
			SLNode* nextToTail = head;
			SLNode* tail = head->getNextNode();
				while (tail->getNextNode() != NULL) {
					nextToTail = tail;
					tail = tail->getNextNode();
				}
			delete tail;
			nextToTail->setNextNode(NULL);
		}
		size--;
	}
}
Пример #13
0
void SList::insertTail (int contents){
     
	if(head != NULL) {
		//Finds the tail. 
		SLNode* i = head;
		while (i->getNextNode() != NULL) {
			//Starts at head and moves it over to the last node. 
			i = i->getNextNode();
		}
		SLNode* node = new SLNode(contents);
		//points at the end and inserts the new node that was just created. 
		i->setNextNode(node);
		size++;
	} else {
		//Empty list - make it to insert head as it's the same thing. 
		insertHead(contents);
	}
 
}
Пример #14
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;
}
Пример #15
0
void SList::removeTail () {
	if (head != NULL) {
		SLNode* i = head;
		SLNode* trailer = NULL;
		
		while (i->getNextNode() != NULL) {
			trailer = i;
			i = i->getNextNode();
		}
		delete i;
		--numNodes;
		if (trailer == NULL) {
			head = NULL;
		}
		else {
			trailer->setNextNode(NULL);
		}
	}
}
Пример #16
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();
}
Пример #17
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;
    }
}
Пример #18
0
void SList::removeTail (){
	if (head == NULL) {
	     //DO NOTHING
		} else if (head->getNextNode() == NULL) {
			removeHead();
		} else {
			SLNode* i = head;
			SLNode* j = NULL;
			while (i->getNextNode() != NULL) {
				//j is i and then i moves over one. j tails the node before i. 
				j = i;
			//Starts at head and moves it over to the last node. 
				i = i->getNextNode();
			}
			//i is the tail.
			delete i;
			
			//need j to tail it and null out the last node. 
			j->setNextNode(NULL);
			size--;
	}
		     
}
Пример #19
0
void SList::removeTail() {
	SLNode* currentNode = mHead;
	
	if(currentNode != 0) {
		SLNode* nextNode = currentNode->getNextNode();
		if(nextNode == 0) {
			delete mHead;
			mHead = 0;
			mSize = 0;
		} else {
			SLNode* secondNode = nextNode->getNextNode();
			while(secondNode != 0) {
				currentNode = nextNode;
				nextNode = secondNode;
				secondNode = secondNode->getNextNode();
			}
			
			delete nextNode;
			currentNode->setNextNode(0);
			--mSize;
		}
	}
}
Пример #20
0
void SList::removeTail()
{
    if(head != NULL)
    {
        SLNode* lastNode = head;
        SLNode* nodeToDelete = head;
        if(lastNode->getNextNode() == NULL)
        {
            removeHead();
        }
        else
        {
            while(lastNode->getNextNode()->getNextNode() != NULL)
            {
                lastNode = lastNode -> getNextNode();
            }
            nodeToDelete = lastNode->getNextNode();
            lastNode-> setNextNode(NULL);
            delete nodeToDelete;
            size--;
        }
    }
}
Пример #21
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++;
			}
		}
	}
}
Пример #22
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();
}
Пример #23
0
void SList::removeTail()
{
    if(head != NULL)
    {
        SLNode* i = head;
        SLNode* trailer = NULL;
        
        while(i->getNextNode() != NULL)
        {
            trailer = i;
            i = i->getNextNode();
        }
        delete i;
        size--;
        if(trailer == NULL)
        {
            head = NULL;
        }
        else
        {
            trailer->setNextNode(NULL);
        }
    }
}//remove the tail node from the list
Пример #24
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));
}
Пример #25
0
SLNode* SList::getTail() const {
	SLNode* currentNode = mHead;
	
	if(currentNode != 0) {
		while(true) {
			SLNode* const nextNode = currentNode->getNextNode();
			if(nextNode == 0) {
				return currentNode;
			}
			
			currentNode = nextNode;
		}
	}
	
	return currentNode;
}
Пример #26
0
void SList::insertTail (int newContents)
{
    SLNode* newNode = new SLNode(newContents);
    SLNode* temp = head;
    if(head == NULL)
    {
        head = newNode;
    }
    else
    {
        while(temp->getNextNode() != NULL)
        {
            temp = temp -> getNextNode();
        }
        temp -> setNextNode(newNode);
    }
    size++;
}
Пример #27
0
void SList::insertTail(int nodeContents) {
    if (head == NULL) {
        insertHead(nodeContents);
    } else {
        SLNode* newTail = new SLNode(nodeContents);
        if (head->getNextNode() == NULL) {
            head->setNextNode(newTail);
        } else {
            SLNode* oldTail = head->getNextNode();
            for (unsigned int i = 1; i < size; i++){
                if (oldTail-> getNextNode() != NULL){
                    oldTail = oldTail->getNextNode();
                }
            }
            oldTail->setNextNode(newTail);
            oldTail = NULL;
        }
        newTail = NULL;
        size++;
    }
}