Exemple #1
0
T Stack<T>::pop() throw(PreconditionViolationException) {

Node<T>* temp = m_front;
T tempValue = T();

if(isEmpty()) {
	throw PreconditionViolationException("Pop attempted on an empty stack");
}

else if(size() == 1) {
	tempValue = m_front->getValue();	
	delete m_front;
	m_front = nullptr;
	m_size--;
	return(tempValue);
}

else {
	tempValue = m_front->getValue();	
	m_front = m_front->getNext();
	delete temp;
	m_size--;
	return(tempValue);
}

}
Exemple #2
0
T Stack<T>::peek() const throw(PreconditionViolationException){
    if(isEmpty()){
        throw (PreconditionViolationException("Peek attempted on an empty stack"));
    }
    else{
        return m_front->getValue();
    }
}
Exemple #3
0
void Stack<T>::pop() throw(PreconditionViolationException){
    if(isEmpty()){
        throw (PreconditionViolationException("Pop attempted on an empty stack"));
    }
    else{
        m_front->getValue();
        m_front=m_front->getNext();
        m_size--;
    }
}
Exemple #4
0
T Stack<T>::peek() const throw(PreconditionViolationException){

	if(isEmpty()){
		throw(PreconditionViolationException("Exception: Peek attempted on an empty stack\n"));
	}

	else{
		return (m_top->getValue());
	}
}
Exemple #5
0
T Stack<T>::peek() const throw(PreconditionViolationException){
	
	//throw exception if list is empty
	if(isEmpty()){
		throw PreconditionViolationException("Peek attempted on empty stack.\n");
	}
	//return top value of the stack
	else{
		return m_top->getValue();
	}
}
Exemple #6
0
T Stack<T>::pop() throw(PreconditionViolationException){
	
	if(isEmpty()){
		throw(PreconditionViolationException("Exception: Pop attempted on an empty stack\n"));
	}
	else{

		Node<T>* n = (m_top->getNext());
		T temp = (m_top->getValue());
		delete m_top;
		m_size--;
		m_top = n;
		return temp;
	}
}
Exemple #7
0
void Stack<T>::pop() throw(PreconditionViolationException)
{
    if (isEmpty())
    {
        throw PreconditionViolationException("Pop attempted on an empty stack");
    }
    else
    {
        Node<T>* nodeToDelete = m_top;
        m_top = m_top->getNext();
        m_size--;

        delete nodeToDelete;
        nodeToDelete = nullptr;
    }
}
Exemple #8
0
void Stack<T>::pop() throw(PreconditionViolationException){
	//throws exception if list is empty
	if(isEmpty()){
		throw PreconditionViolationException("Pop attempted on empty stack.\n");
	}
	
	else{
		//temporary pointer that moves
		Node<T>* traverse = m_top;
		
		//move m_top pointer so traverse is the only pointer holding node to be deleted
		m_top = m_top->getNext();
		
		//delete node and set traverse to nullptr
		delete traverse;
		traverse = nullptr;
		
		//decrement to reflect that one fewer node exists
		m_size--;
	}
}