Пример #1
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_++;
  }
}
Пример #2
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_++;
	}
}
Пример #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
 /*
  * 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;
 }
Пример #5
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;
  }    
}
Пример #6
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();
}
Пример #7
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();
	}
}
Пример #8
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
}
Пример #9
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;
}
Пример #10
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();
}
Пример #11
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();
}
Пример #12
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;
    }
}
// For NULL
#include <cstddef>
#include <sstream>

#include "sl_list.h"
// To test for Header guards
#include "sl_list.h"
#include "sl_node.h"
#include "sl_node.h"

TEST_CASE("Default Constructor for SLNode") {
  SLNode node, node2;
  SLNode* p_node2 = &node2;
  SECTION("Accessor for Contents") {
    CHECK(node.contents() == 0);
  }

  SECTION("Accessor for Next Node") {
    CHECK(node.next_node() == NULL);
  }

  SECTION("Mutator for Contents") {
    node.set_contents(100);
    CHECK(node.contents() == 100);
  }

  SECTION("Mutator for Next Node") {
    node.set_next_node(&node2);
    CHECK(node.next_node() == p_node2);
  }