Example #1
0
void HashTable::print(void) {
	Node* navigator = m_head;
	while (navigator != 0) {
		cout << navigator->getValue() << endl;
		navigator = navigator->getNext();
	}
}
Example #2
0
/**********************************************************************
 * display whole link list
 ***********************************************************************/
void List::display() const 
{
   cout << "----DISPLAY----" << endl;
   Node* cur = firstNode;


  for (int i = 0; cur; ++i)
  {
     cout <<  i << " = i " <<  cur->getData() << "-> " << cur->getNext() <<  endl;
     cur = cur->getNext();
  }

   cout << "~~~NULL" << endl;

   cout << numItems << endl;
}   
Example #3
0
void Q::push(Sortable *s)
{
	if(!m_start) // Empty list
	{
		m_start = new Node(s);
		m_start->setNext(m_start);
		m_start->setPrev(m_start);
	}
	else 
	{
		Node *new_node = new Node(s);
		Node *cur = m_start;
		
		while( cur->getData()->compareTo(s) <= 0 )
		{
			cur = cur->getNext();
			
			if(cur == m_start)
				break; // Looped around
		}
		
		new_node->setNext(cur);
		new_node->setPrev(cur->getPrev());
		cur->getPrev()->setNext(new_node);
		cur->setPrev(new_node);
		
		// We inserted before the first node, so update m_start
		if(cur == m_start && m_start->getData()->compareTo(s) > 0)
			m_start = new_node;
	}	
}
Example #4
0
void List<T>::insertBefore(int target, const T& data) {
  if (!hasFirst()) {
    return;
  }

  Node<T>* currentNode = getFirst();
  Node<T>* newNode = new Node<T>(data);

  for (int i = 0; i <= target; i++) {
    if (i == target) {
      if (currentNode->hasPrev()) {
        currentNode->getPrev()->setNext(newNode);
        newNode->setPrev(currentNode->getPrev());
      }

      newNode->setNext(currentNode);
      currentNode->setPrev(newNode);

      return;
    }

    if (!currentNode->hasNext()) {
      return;
    }

    currentNode = currentNode->getNext();
  }

  delete currentNode;
}
Example #5
0
bool HashTable::findWord(string word) {
	Node* navigator = m_head;
	string dictWord;
	while (navigator != 0) {
		dictWord = navigator->getValue();
		if (dictWord == word) {
			return true;
		}
		//check for omissions
		if (dictWord.length() <= word.length() + 1 || dictWord.length() <= word.length() - 1) {
			int count = 0;
			//check for misplaced letter
			for (int i = 0; i < dictWord.length() && i < word.length(); i++) {
				if (dictWord[i] == word[i]) {
					count++;
				}
			}
			if (count >= (word.length() - 1)) {
				if (m_suggestions.size() < 10) {
					m_suggestions.push_back(dictWord);
				}
			}
		}
		navigator = navigator->getNext();
	}
	return false;
}
Example #6
0
LinkedBag<ItemType>::LinkedBag(const LinkedBag<ItemType>& aBag)
{
   itemCount = aBag->itemCount;
   Node<ItemType>* origChainPtr = aBag->headPtr;  // Points to nodes in original chain
   
   if (origChainPtr == NULL)
      headPtr = NULL;  // Original bag is empty; so is copy
   else
   {
      // Copy first node
      headPtr = new Node<ItemType>();
      headPtr->setItem(origChainPtr->getItem());
      
      // Copy remaining nodes
      Node<ItemType>* newChainPtr = headPtr; // Last-node pointer
      while (origChainPtr != NULL)
      {
         origChainPtr = origChainPtr ->getNext(); // Advance pointer
         
         // Get next item from original chain
         ItemType nextItem = origChainPtr->getItem();
         
         // Create a new node containing the next item
         Node<ItemType>* newNodePtr = new Node<ItemType>(nextItem);
         
         // Link new node to end of new chain
         newChainPtr->setNext(newNodePtr);
         
         // Advance pointer to new last node
         newChainPtr = newChainPtr->getNext();
      } // end while
      
      newChainPtr->setNext(NULL); // Flag end of new chain
   } // end if
} // end copy constructor
void ContactManager::print(){

     cout << "Print Fun.\n";
     Node<Contact>* nPtr = contacts.getHead() ;

     int c = 0 ;
     while( nPtr != NULL)
     {
    //     cout << "here";
    //string cn = nPtr->getData().getName() ;
            cout << nPtr->getData() << endl;
          //cout << cn << endl;
/*************************  my edit 34an ygeeb 2li gwa kol contact **************************************/

          /*Node<string>* first = nPtr->getData().phone_numbers.getHead() ;
          while( first != NULL )
          {
              cout << first->getData() << " " ;
              first = first->getNext() ;
          }
            cout << "\n-----\n";*/

          nPtr = nPtr->getNext() ;
          c++ ;
     }
     cout << " - Unique Names : " << c ;

}
 //removes the node at the specified index
 //if the index is not in the list or there are no items in the list it returns nullptr
 //otherwise it returns the node that was removed
 Node<Type>* remove(int index){
     if(size == 0){
         ioc << cat_error << vrb_quiet << "List is empty" << io_end;
         return nullptr;
     }
     else if(index < 0 || index > size - 1)
     {
         ioc << cat_error << vrb_quiet << "Index out of bounds" << io_end;
         return nullptr;
     }
     else if(index == 0)
     {
         Node<Type>* prev = head;
         head = head -> getNext();
         size--;
         return prev;
     }
     else{
         Node<Type>* prev = head;
         Node<Type>* curr = head;
         for(int i = 0; i < index; i++, prev = curr, curr = curr -> getNext());
         prev->setNext(curr->getNext());
         size--;
         return curr;
     }
 }
Example #9
0
bool LinkedList<T>::search(T value) const
{
	Node<T>* temp = m_front;
	bool isFound = false;

	if(isEmpty())//if it's empty juts return false
	{
		return(false);
	}
	while(temp!=nullptr) //this condition loops until the last node
	{
		if(temp->getValue()==value)
		{
			isFound=true;//return true if it matches the value
			break;//and break the loop when you find it
		}

		else
		{
			temp=temp->getNext();//if you don't find it, move on to the next element
		}
	}

	return(isFound);
}
Example #10
0
int main (int argc, char** argv)
{
  using namespace std;
   
  int c = 1;
  // create a new list made by 1,2,3
  Node start (c);

  Node *second=new Node(2);//new node to add
  start.append(second);

  Node *third=new Node(3);//new node to add
  start.append(third);
   
    
  //start.print ();
  
   
  //test on getPrevious 
  //returns a pointer to the second node that stores value 2
  Node *prec;
  prec=third->getPrevious() ;

   //test on getNext
  //returns a pointer to the second node that stores value 2
  Node *succ;
  succ=start.getNext() ;
  
  //I check the value stored in the Node pointers to verify that getNext and getPrevious points to the correct node
  fprintf(stdout,"%d and %d",prec->getData(),succ->getData());

return 0;


}
Example #11
0
 TYPE* toArray(TYPE* arr,int cur,Node* root){
     if(root==NULL){
         return NULL;
     }
     arr[cur]=root->getKey();
     return toArray(arr,cur+1,root->getNext());
 }
Example #12
0
bool LinkedList::insert(int newPosition, const ItemType& newEntry)
{
   bool ableToInsert = (newPosition >= 1) && (newPosition <= itemCount + 1);
   
   if (ableToInsert)
   {
      // Create a new node containing the new entry
      Node* newNodePtr = new Node(newEntry);
      
      // Attach new node to chain
      if (newPosition == 1)
      {
         // Insert new node at beginning of chain
         newNodePtr->setNext(headPtr);
         headPtr = newNodePtr;
      }
      else
      {
         // Find node that will be before new node
         Node* prevPtr = getNodeAt(newPosition - 1);
         
         // Insert new node after node to which prevPtr points
         newNodePtr->setNext(prevPtr->getNext());
         prevPtr->setNext(newNodePtr);
      }
      
      itemCount++; // Increase count of entries
   }
   
   return ableToInsert;
}  // end insert
Example #13
0
void SinglyLinkedList<ItemType>::add(const ItemType &newItem)
{
	Node<ItemType> *newNodePtr = new Node<ItemType>();
	Node<ItemType> *pos = headPtr;
	Node<ItemType> *prevPtr = 0;

	while (pos != 0 && pos->getItem() != 0)
	{
		prevPtr = pos;
		pos = pos->getNext();
	}

	newNodePtr->setNext(pos);
	if (prevPtr != 0)
	{
		prevPtr->setNext(newNodePtr);
	}
	else
	{
		headPtr = newNodePtr;
	}
	
	itemCount++;

}
Example #14
0
template <class ItemType> bool LinkedList <ItemType>::remove(int position)
{
  // remove an item from an existing list.  if successful list will have
  // count - 1 items.  Deleted position can be anywhere from 1 to count
  // 1 corresponds to the beginning of the list, count corresponds to
  // the end of the list, and anything in between will be at the appropriate
  // location in the middle of the list

  bool result;

  if ((position < 1) || (position > count))
	{
	  result = false;
	}
  else {

	Node<ItemType>* curPtr = nullptr;

	if (position == 1)
	  {
		// Remove the first node in the chain
		curPtr = headPtr;
		headPtr = headPtr->getNext();
	  }
	else
	  {
		// Find node that is before the one to delete
		Node<ItemType>* prevPtr = getNodeAt(position - 1);

		// Point to node to delete
		curPtr = prevPtr->getNext();

		// Disconnect indicated node from chain by connecting the
		// prior node with the one after
		prevPtr->setNext(curPtr->getNext());
	  }

	// Return node to system
	curPtr->setNext(nullptr);
	delete curPtr;
	curPtr = nullptr;
	count--;
	result = true;
  }

  return result;
}
Example #15
0
LinkedBag<ItemType> LinkedBag<ItemType>::bagUnion(const LinkedBag& input) const {
    // make a new LinkedBag that will hold the union
    // put input values into theUnion with copy constructor
    LinkedBag<ItemType> theUnion = LinkedBag(input);
    // put caller values into theUnion
    theUnion.itemCount = itemCount+input.itemCount;
    Node<ItemType>* origChainPtr = headPtr;  // Points to nodes in original chain

    if (origChainPtr == nullptr)
        //theUnion->next = nullptr;  // caller bag is empty
        // do nothing
        else
        {
            // Copy first node
            Node<ItemType>* newChainPtr = theUnion.

                                          ***** STOPPED HERE

                                          headPtr = new Node<ItemType>();
            headPtr->setItem(origChainPtr->getItem());

            // Copy remaining nodes
            Node<ItemType>* newChainPtr = headPtr;      // Points to last node in new chain
            origChainPtr = origChainPtr->getNext();     // Advance original-chain pointer

            while (origChainPtr != nullptr)	// origChainPtr is used as our counting pointer to traverse
                // the old chain
            {
                // Get next item from original chain
                ItemType nextItem = origChainPtr->getItem();

                // Create a new node containing the next item
                Node<ItemType>* newNodePtr = new Node<ItemType>(nextItem);

                // Link new node to end of new chain
                newChainPtr->setNext(newNodePtr);

                // Advance pointer to new last node
                newChainPtr = newChainPtr->getNext();

                // Advance original-chain pointer
                origChainPtr = origChainPtr->getNext();
            }  // end while

            newChainPtr->setNext(nullptr);              // Flag end of chain
        }  // end if
}  // end copy constructor
Example #16
0
void LinkedList<Item>::insertAt(const int& location, const Item& _key, const Item& _value) {
  if (location > size)
    throw std::runtime_error("Parameter 'location: int' in function LinkedList<Item>::insertAt() is out of range.");
  else {
    Node<Item>* traverser = head;
    for (int i = 0; i < location; i++)
      traverser = traverser->getNext();

    if (head == NULL) {
      /* This is the case of when the list is empty. */
      head = new Node<Item>(_key, _value);

    } else if (location == size) {
      /* This is the case where we're inserting at the end of the list when it's not empty. */

      /* I know the below loop is a pretty inefficient thing to do.
       * But since efficiency is not the matter right now, so be it.
       * Hackin' together a solution!
       */
    	traverser = head;
      for (int i = 0; i < location-1; i++)
        traverser = traverser->getNext();

      Node<Item>* newNode = new Node<Item>(_key, _value);
      traverser->setNext(newNode);
    } else if (location == 0) {
      /* This is the case of head insertion when the list isn't empty. */

      Node<Item>* newNode = new Node<Item>(_key, _value);
      newNode->setNext(head);
      head = newNode;
    } else {
      /* This is the case where we're inserting between 2 Nodes. */

      Node<Item>* beforeTraverser = head;
      for (int i = 0; i < location - 1; i++)
        beforeTraverser = beforeTraverser->getNext();

      Node<Item>* afterTraverser = traverser;
      traverser = new Node<Item>(_key, _value);

      traverser->setNext(afterTraverser);
      beforeTraverser->setNext(traverser);
    }
    size++;
  }
}
void DLList<T>::insert(T item, int pos) 
{
   // Check pos to see if it is negative
   if (pos < 0)
   {
      cout << "Invalid position. List cannot have negative index.";
      exit(1);
   }
   else 
   {
      Node<T>* trav = firstNode; // traversal pointer
      Node<T>* temp = new Node<T>;     //create node and fill with item
      temp->setData(item);
      
      if (pos > numItems) // if pos > list size, set pos for tail insert
         pos = numItems;

      if (pos == 0) // head insert
      {
         if (firstNode != NULL) // make sure list not empty 
         {
            temp->setNext(firstNode);
            firstNode->setPrevious(temp);
         }
         firstNode = temp;
      }
      else if (pos == numItems) // tail insert
      {
         for (int i = 0; i < pos - 1; i++)
            trav = trav->getNext();
         
         trav->setNext(temp);
         temp->setPrevious(trav);
      }
      else //any inserts in between nodes
      {
         for (int i = 0; i < pos - 1; i++)
            trav = trav->getNext();
         
         temp->setNext(trav->getNext());
         temp->setPrevious(trav);
         trav->setNext(temp);
         temp->getNext()->setPrevious(temp);
      }
      numItems++;
   }
}
Example #18
0
    // Append new node to the end of the list
    void appendToEnd(T d)
    {
        Node<T> *tmp = new Node<T>(d);
        if (next == nullptr)
        {
            next = tmp;
            return;
        }

        Node<T> *n = next;
        while (n->getNext() != nullptr)
        {
            n = n->getNext();
        }

        n->setNext(tmp);
    }
Example #19
0
	void traverse(){
		Node<T>* trav = head;

		while(trav != NULL){
			std::cout << trav->getData() << std::endl;
			trav = trav->getNext();
		}
	}
Example #20
0
/**
 * Aumenta la edad de todos en 1
 */
void Population::EverybodyBirthday(){
	Node<Entity*>* tmp = _individuos->getHead();
	for(int i = 0 ; i < (_individuos->getLength()); i++){
		tmp->getData()->Birthday();
		tmp = tmp->getNext();
	}
	return;
}
Example #21
0
void Stack::display(){
  Node * ptr = head;
  while(ptr){
    cout << ptr->getData() << ' ';
    ptr = ptr->getNext();
  }
  cout << endl;
}
Example #22
0
bool detectCircularList(Node* head) {

	Node* fastPtr = head;
	Node* slowPtr = head;
	
	while(fastPtr != nullptr && slowPtr	!= nullptr) {
		fastPtr = fastPtr->getNext();
		if(fastPtr == nullptr)	break;
		if(fastPtr == slowPtr)	return true;
		fastPtr = fastPtr->getNext();
		if(fastPtr == nullptr)	break;
		if(fastPtr == slowPtr)	return true;
		slowPtr = slowPtr->getNext();
	}

	return false;
}
Example #23
0
	void size()
	{
		if(head==0)
			cout<<0<<endl;
			else
			{
		int cnt=1;
		Node *temp;
		temp=head;
		while(temp->getNext()!=0)
		{
			cnt++;
			temp=temp->getNext();
		}
		cout<<cnt<<endl;
	}
	}
Example #24
0
/**
 * @brief print
 * Prints the content of the linked list
 */
void ShapeList::print(){
    Node *current = _pHead;
    while (current != 0){ //->getNext() != 0) {
        Shape* s = current->getValue();
        s->print();
        current = current->getNext();
    }
}
Example #25
0
		~List(){
			Node *head = this->root;
			while(head){
				Node *tmp = head->getNext();
				delete head;
				head = tmp;
			}
		}
Example #26
0
vector<Customer*> LinkedList::getAll()
{
	vector<Customer*> customers;
	if(isEmpty())
	{
		return customers;
	}

	Node* curr = this->head;
	while(curr->getNext() != NULL)
	{
		curr = curr->getNext();
		customers.push_back(curr->getData());
	}

	return customers;
}
Example #27
0
void LinkedList::addToEnd(Data toAdd){
	clock_t start = clock();
	//set to start if there is no elements in the list
	if (theStart == nullptr){
		theStart = new Node(&toAdd, nullptr);
	}
	else {
		Node* current = theStart;
		//read through list to get to end
		while (current->getNext() != nullptr){
			current = current->getNext();
		}
		current->setNext(new Node(&toAdd, nullptr));
	}
	currSize++;
	ticks += clock() - start;
}
Example #28
0
void List::remove(Node *element)
{
	Node *p = head, *q = NULL;
	while ((p != element) && (p != NULL))
	{
		q = p;
		p = p->getNext();
	}
	if (p == NULL)
		return;
	if (p == tail)
		tail = q;
	if (p == head)
		head = p->getNext();
	if (q != NULL)
		q->setNext(p->getNext());
}
Example #29
0
//Input: index of person you want to get
//Output: string of the person at that index
//Purpose: to get the a single person in the list
std:: string Queue:: get(int index){
    Node* next = first;
    for ( int i = 0; i < index; i++){
        next = next->getNext();
    }
    std:: string info = next->getName() + " ID Number: " + std::to_string( next->getIdNumber() );
    return info;
}
Example #30
0
//Destructor
//Input: nothing
//Output nothing
//Purpose: code that needs to be destroyed before queue is
Queue:: ~Queue(){
    if (first == nullptr) {
        return;
    }
    if (first->getNext() == nullptr) {
        return;
    }
    Node* next = first->getNext();
    delete first;
    for (int i = 1; i < size; i++) {
        if (next->getNext() == nullptr) {
            return;
        }
        next = next->getNext();
        delete next;
    }
}