Пример #1
0
void SLList::Insert(int contents) {
	if(head_ == NULL) {
		InsertHead(contents);
	}
	else if(contents < GetHead()) {
		InsertHead(contents);
	}
	else if (head_->next_node() == NULL && head_ != NULL) {
		InsertTail(contents);
	}
	else if(contents > GetTail()) {
		InsertTail(contents);
	}
	else {
		SLNode* node = new SLNode(contents);
		SLNode* i = head_;
		SLNode* j = NULL;
		while(i->contents() <= contents && i->next_node() != NULL) {
			j = i;
			i = i->next_node();
		}
		j->set_next_node(node);
		node->set_next_node(i);
		size_++;
	}
}
Пример #2
0
void SLList::RemoveTail()
{
    //removes the tail node from the list, or does nothing if the list is empty
    SLNode *tmp = head_;
    if(tmp != NULL)
    {
        // list is not empty
        if (head_ == tail_)
        {
            // list only has 1 item in it, clear the list
            delete head_;
            head_ = NULL;
            tail_ = NULL;
            size_ = 0;
            return;
        }
        while(tmp->next_node() != tail_)
        {
            // find the item just before the tail
            tmp = tmp->next_node();
        }
        // delete the old tail
        delete tail_;
        // this will be the new end of the list
        tmp->set_next_node(NULL);
        tail_ = tmp;
        size_--;
        return;
    }
}
Пример #3
0
void SLList::Insert(int contents)
{
    if (contents < GetHead() || head_ == NULL)
    {
        InsertHead(contents);
    }
    else if (contents > GetTail())
    {
        InsertTail(contents);
    }
    else
    {
        SLNode *new_node = new SLNode(contents);
        SLNode *it = head_;
        SLNode *temp = it->next_node();
        if (size_ == 2)
        {
            head_->set_next_node(new_node);
            new_node->set_next_node(tail_);
        }
        else
        {
            while (temp->contents() < contents)
            {
                it = temp;
                temp = temp->next_node();
            }
            it->set_next_node(new_node);
            new_node->set_next_node(temp);
        }
        size_++;
    }
}
Пример #4
0
void SLList::Insert(int contents)
{
    //Checks if head is null or contents is < head_->contents();
    if (head_ == NULL || head_->contents() > contents)
    {
        InsertHead(contents);
    } else if ( tail_->contents() < contents)
    {
      //  cout << " got it: " << contents << endl;
     //   cout << ToString();
        InsertTail(contents);
    } else 
    {
        //creates iterator and newnode
        SLNode* iterator;
        iterator = head_;
        while (iterator->next_node() != NULL && contents > iterator->next_node()->contents())
        {
        iterator = iterator->next_node();
        }
        if (iterator != tail_)
        {
            SLNode *newNode = new SLNode(contents);
            newNode->set_next_node(iterator->next_node());
            iterator->set_next_node(newNode);
            size_ +=1;
        } else
        {
            InsertTail(contents);
        }
    }
}
Пример #5
0
 /*
  * returns a string representation of the contents
  * of all nodes in the list, in the format
  * NUM1, NUM2, ..., LASTNUM
  * returns the empty string on an empty list (i.e. returns "")
  */
 string SLList::ToString() const
 {
   std::stringstream ss;
   ss.clear();
   ss.str();
   
   SLNode* temp = head_;                                                       //create temp to be an iterator
   
   if(head_ != NULL)
   {
      while(temp->next_node() != NULL)                                         //cycles through the list until null is next
      {
         ss << temp-> contents();
      
         temp = temp->next_node();
       
         ss << ", ";
         
      }   
      ss << temp->contents();                                                  //prints out the last node
   }
   else
   {
       ss << "";
   }
 
   string someString;
 
   someString = ss.str();
 
   return someString;
 }
Пример #6
0
bool SLList::RemoveFirstOccurence(int data)
{ 
  if(head_) //if there is a list
  {
    if(head_->contents() == data) //if the data is same as head                                          
    {
      RemoveHead();
      return true;
    }
    SLNode* prev = head_; //assign node to head_
    SLNode* current = head_->next_node(); //assign current to node after head
    while(current) //while current is not NULL                                                      
    {
      if(current->contents() == data)  //if current = to data                                      
      {
        prev->set_next_node(current->next_node()); //prev points to the node after current
        if(current == tail_)  //if current = tail, tail assigned to prev                                                     
        {
          tail_ = prev;
        }
        current = NULL; //dereference current
        delete current; //delete current
        size_--; //now we minus the size
        return true;
      }
      prev = current; //these traverse prev before current
      current = current->next_node();
    }
  }
  else
  {
    return false;
  }    
}
Пример #7
0
string SLList::ToString() const {
	stringstream ss;
		for(SLNode* iterator = head_; iterator != NULL; iterator = iterator->next_node()) {
			ss <<  iterator->contents();
			if(iterator->next_node() != NULL) {
			  ss << ", ";
		}
	}
	return ss.str();
}
Пример #8
0
int SLList::GetTail() {
	if(head_ == NULL) {
		return 0;
	}
	else {
		SLNode* i = head_;
		while(i->next_node() != NULL) {
			i = i->next_node();
		}
		return i->contents();
	}
}
Пример #9
0
string SLList::ToString() //uses string stream to display
{
  stringstream stream;
  for(SLNode* out = head_; out != NULL; out = out->next_node()) //sets out* to head, while not NULL points to next node
  { 
    stream << out->contents(); //puts contents of each node into stream
    if(out->next_node() != NULL) //if the next node is not NULL then is adds a comma for the incoming next node
    {
      stream << ", ";
    }
  }
  return stream.str(); //returns contents of string
}
Пример #10
0
void SLList::InsertTail(int newTail) {
	if(head_ == NULL) {
		InsertHead(newTail);
	}
	else {
		SLNode* i = head_;
		while (i->next_node() != NULL) {
			i = i->next_node();
		}
		SLNode* node = new SLNode(newTail);
		i->set_next_node(node);
		size_++;
	}
}
Пример #11
0
void SLList::Insert(int data)
{  
  
  SLNode* insert = new SLNode(data); //assigns data to new node 
  SLNode* current = head_; //assigns head to current
  SLNode* prev = current;
  if(!head_ || data <= current->contents()) //if there is a list or data is less than or equal to head_                                                          
  {
    InsertHead(data); 
  }
  else if(data > tail_->contents()) //if data is greater than tail
  {
    InsertTail(data);
  }
  else
  {
  while(current->contents()  < data) //while current is less than data
  {
    prev = current; //we assign prev to currents position
    current = current->next_node(); //then assign current to next position
  }
  insert->set_next_node(current); //set node at currents position
  prev->set_next_node(insert); //set data at proper position
  size_++;
  }
}
Пример #12
0
/**
 * returns a string representation of the contents
 * of all nodes in the list, in the format
 * NUM1, NUM2, ..., LASTNUM
 * returns the empty string on an empty list (i.e. returns "")
 */
string SLList::ToString() const
{
    if (head_ == NULL)
    return "";
    stringstream ss;
        SLNode* temp = head_;
        
            while (temp != NULL)
            {
                ss << temp->contents();
                if (temp->next_node() != NULL)
                ss << ", ";
                temp = temp->next_node();
            }
    
    
    return ss.str();
}
Пример #13
0
void SLList::RemoveTail() {
	if(head_ == NULL) {
		//DO NOTHING
	}
	else if (head_->next_node() == NULL) {
		RemoveHead();
	}
	else {
	SLNode* i = head_;
	SLNode* j = NULL;
		while (i->next_node() != NULL) {
			j = i;
			i = i->next_node();
		}
		delete i;
		j->set_next_node(NULL);
		size_--;
	}
}
Пример #14
0
void SLList::RemoveTail()
{
  if(head_ && size_ >= 2) //we need to know that there is a list bigger that 1 before we remove tail
  { 
    SLNode* remove_tail = tail_;
    SLNode* current = head_; //new pointer assigned to head
    while(current->next_node() != tail_)//traverses list while not at tail position
    {
      current = current->next_node();
    }
    tail_ = current;
    tail_->set_next_node(NULL);
    delete remove_tail;
    size_--;
  }
  else if(size_ == 1) //checks if the size is one
  {
    head_ = NULL; //assigns head to null
    size_ = 0; //set size to 0
  }
}      
Пример #15
0
bool SLList::RemoveFirstOccurence(int contents) {
	SLNode* node = new SLNode(contents);
	if (contents == GetHead()) {
		RemoveHead();
		return true;
	}
	else if(contents == GetTail()) {
		RemoveTail();
		return true;
	}
	else if(head_ == NULL) {
		return false;
	}
	else if (node == NULL) {
		return false;
	}
	else if (contents != GetHead() && contents != GetTail()) {
		SLNode* i = head_;
		SLNode* j = NULL;
		while (i->contents() != contents && i->next_node() != NULL) {
			j = i;
			i = i->next_node();
		}
		if(i->next_node() == NULL && i->contents() != contents) {
			return false;
		}
		else {
			SLNode* temp = i->next_node();
			SLNode* temp2 = i;
			delete temp2;
			j->set_next_node(temp);
			size_--;
			return true;
		}
	}
	else
		return false;
}
Пример #16
0
string SLList::ToString() const
{
    /* returns a string representation of the contents
    of all nodes in the list, in the format
    NUM1, NUM2, ..., LASTNUM
    returns the empty string on an empty list (i.e. returns "")
    */ 
    SLNode* current = head_;
    std::stringstream ss;
    
    if(current != NULL)
    {
        while (current != NULL)
        {
            ss << current->contents();
            if(current->next_node() != NULL)
            {
                ss << ", ";
            }
            current = current->next_node();
        }
    }
    return ss.str();
}
Пример #17
0
/**
 * removes the tail node from the list,
 * or does nothing if the list is empty
 */
void SLList::RemoveTail()
{
    if (head_ != NULL)
    {
         if (head_ == tail_)
         {
             RemoveHead();
         } else 
         {
             SLNode* temp = head_;
             while (temp->next_node() != tail_)
             temp = temp->next_node();
             temp->set_next_node(NULL);
             delete tail_;
             tail_ = temp;
             size_ -= 1;
         }
    }
}
Пример #18
0
 bool SLList::RemoveFirstOccurence(int contents)
 {
     //returns false for empty list
     if (head_ == NULL)
     {
         return false;
         // uses RemoveTail() if tail == contents
     } else if (tail_->contents() == contents)
     {
         RemoveTail();
         return true;
     } else 
     {
         //creating pointers
         SLNode* iterator = head_, *temp = head_->next_node();
         //traverses to find match for contents
         while (contents != temp->contents() && temp->next_node() != NULL)
         {
             temp = temp->next_node();
             iterator = iterator->next_node();
         }
     //returns false if contents are not in list
     if (temp == tail_ && temp->contents() != contents)
     {
         return false;
     
     } else
     //checks if temp is the node to delete then executes
         if(temp->contents() == contents)
         {
             iterator->set_next_node(temp->next_node());
             delete temp;
             size_ = size_ - 1;
             temp = NULL;
             return true;
             
         } 
     } 
 }
Пример #19
0
 void SLList::RemoveHead()
  {
      if (head_ != NULL)
      {
           
          SLNode* temp;              //create temporary node
          temp = head_;              //point temp at head
          head_ = temp->next_node(); //
          delete temp;               //delete contents of temp(formerly head)
          temp = NULL;               //assign NULL to temp
          size_--;                   //decrement size
          if (size_ == 0)
          {
              tail_ = NULL;
          }
         
      }
      // (*MODIFY*)
      // removes the head node from the list,
      // or does nothing if the list is empty
      // (*NEW*) - Handle tail_ when the last remaining node in the list is removed
  }
Пример #20
0
string SLList::ToString() const
{

    SLNode *print = head_;
    stringstream ss;
    ss.str();
    string list;
    const char* separator = "";
    if (head_ == NULL)
    {
        return "";
    }
    else if (head_ != NULL)
    {
       while (print != NULL)
        {
            ss << separator << print->contents();
            print = print->next_node();
            separator = ", ";
        }
        list = ss.str();
        return list;
    }
}