Example #1
0
bool StringLinkedList::remove(const std::string& e) {
	bool found = false;
	if (empty()){ //Empty list
		found = false;
	}
	else if (head->elem == e && head->next == NULL){
	removeFront();
	head = NULL;
	found = true;
	}
	else if (head->elem == e && head->next != NULL){
		removeFront();
		found = true;
	}
}
Example #2
0
/**
* Runs tests on the removeFront function by removing all the nodes in the list
**/
void removeFrontAll(ListPtr list, int count)
{
	int i;
	for(i = count; i > 0; i--)
	{
		NodePtr node = removeFront(list);
		if(DEBUG > 0)
		{
			if(node == NULL) {printf("Node is NULL"); continue;}
			//printf("i: %d jobid: %d size: %d", i, node->data->jobid, list->size);
			if(node->data->jobid != i-1 
				|| list->size != i-1 
				|| (list->head != NULL && list->head->data->jobid != i-2) 
				|| (list->tail != NULL && list->tail->data->jobid != 0)
			)
			{
				printf("\n%s\n%s\n", sep, "Error: removeFrontAll invalid");
				break;
			}
		}
		freeNode(node);
	}
	if(DEBUG > 0)
		verifyFrontAll(list, count);
}
int main()
{
    system("color 0a");
    int ch,data;
    do
    {
        printf("Enter choice : \n1. Add at rear\n2.Add at front\n3.Remove from rear\n4.Remove from Front\n5.Display\n6.search for an item\n0. to exit\n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:printf("Enter data :");
            scanf("%d",&data);
            addRear(data);
            break;
        case 2:
            printf("Enter data :");
            scanf("%d",&data);
            addFront(data);
            break;
        case 3:removeRear();
            break;
        case 4:removeFront();
            break;
        case 5:display();
            break;
        case 6:
            printf("Enter data to search :");
            scanf("%d",&data);
            search(data);
            break;

        }
    }while(ch!=0);
    return 0;
}
Example #4
0
LinkedList<T>::~LinkedList() 
{
	while(!isEmpty())
	{
		removeFront();
	}
}
Example #5
0
/**
 * This method removes the node whose pointer is passed in
 */
NodePtr removeNode(ListPtr list, NodePtr node)
{
	if (list == NULL )
		return NULL ;
	if (list->head == NULL )
		return NULL ;
	if (list->tail == NULL )
		return NULL ;
	if (node == NULL )
		return NULL ;
	if (list -> size <= 0 )
		return NULL;
	if (list ->size == 0 && list->head == NULL && list->tail ==NULL)
		return NULL;
	if (node == list->head || node->prev == NULL )
		return removeFront(list);

	if (node == list->tail || node->next == NULL )
		return removeRear(list);

	node->prev->next = node->next;
	node->next->prev = node->prev;
	node->next = NULL;
	node->prev = NULL;
	list->size--;
	return node;
}
Example #6
0
bool LinkedList<T>::removeBack()
{
	Node<T>* lastNode = nullptr;
	Node<T>* secondintoLast = nullptr;
	bool isRemoved = false;

        //Fix by Josiah Gray
        if(m_size < 2)
        {
            isRemoved = removeFront();
        }
        else
        {
            secondintoLast = m_front;

            while(secondintoLast->getNext()->getNext() != nullptr)
            {
                secondintoLast = secondintoLast->getNext();
            }

            lastNode = secondintoLast->getNext();
            secondintoLast->setNext(nullptr);
            delete lastNode;
            m_size--;
            isRemoved = true;
        }

	return(isRemoved);
}	
Example #7
0
void StringLinkedList::remove(int index) {
	if (empty()){
		throw EmptyException("Empty list");
	}
	
	if (index < 0 || index > n - 1){
		throw OutOfBoundException("Index is out of bounds");
	}

	else{
		StringNode* remove = head, *prev = head;
		int pos = 0;
		while (pos != index && remove->next != NULL){
			prev = remove;
			remove = remove->next;
			pos++;
		}
		if (index == 0){ removeFront(); }
		else if(index == n) { removeBack(); }
		else{
			prev->next = remove->next;
			delete remove;
			n--;
		}
		
	
	}
}
Example #8
0
bool LinkedList<T>::removeBack()
{
    if (isEmpty())// if it is empty
        {
                return false;

        }
    else if (m_size == 1) // if size is one
        {
        removeFront();// remove only one
        m_size--; // lower size
                return true;
        }
    else// if 2 or more are in list
        {
        Node<T>* temp1 = m_front;// create temp node
        Node<T>* temp2 = m_front;// create another temp node
        while (temp1->getNext() != nullptr)// loop through temp1 node
                {
            temp1 = temp1->getNext(); // change values of temp1
                }
        while (temp2->getNext() != temp1)//loop through temp2 node until it doesnt equal temp1
                {
            temp2 = temp2->getNext();//change values of temp2
                }

        delete temp1;// delete temp one node
                temp2->setNext(nullptr);
        m_size--;//lower the size
                return true;
        }

}	
Example #9
0
/* Remove the first element in the queue */
node* dequeue (Queue *Q)
{
    node *front = removeFront(Q->myList);
    Q->queueLength = Q->myList->length;
    
    return (front);
}
bool LinkedList<T>::removeBack()
{
	Node<T>* lastNode = nullptr;
	Node<T>* secondintoLast = nullptr;
	bool isRemoved = false;
	
	if(m_size == 0)
	{
		isRemoved = false;
	}else if(m_size == 1)
	{
		removeFront();
		isRemoved = true;
		m_size--;
	}else
	{
		Node<T>* temp1 = m_front;
		Node<T>* temp2 = m_front->getNext();
		while(temp2->getNext()!=nullptr)
		{
			temp1 = temp2;
			temp2 = temp2->getNext();
		}
		delete temp2;
		temp2 = nullptr;
		temp1->setNext(nullptr);
		isRemoved = true;
		m_size--;
	}

	return(isRemoved);
}	
Example #11
0
LinkedList<T>::~LinkedList()   // destructor
{
	while(!isEmpty())
	{
		removeFront();
	}
}
Example #12
0
List::~List()
{
	while(!isEmpty())
	{
		removeFront();
	}//while
}//List Default Destructor
Example #13
0
/* Generics ----------------------------------------------------------------- */
static void clear(LinkedList *list) {
    
    while(list->front) {
        removeFront(list);
    }

}
int main()
{
    int ch,data;
    do
    {
        printf("1.Add at rear\n2.Add at front\n3.Remove from rear\n4.remove from front\n5.Display\n0.exit\n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:printf("Enter data : ");
                scanf("%d",&data);
                addRear(data);
            break;
        case 2:printf("Enter data : ");
                scanf("%d",&data);
                addFront(data);
            break;
        case 3:removeRear();
            break;
        case 4:removeFront();
            break;
        case 5:display();
            break;
        case 0:printf("\nExitting\n");
            break;
        }
    }while(ch!=0);
    return 0;
}
Example #15
0
Hash::~Hash()
{
	while(!isEmpty())
	{
		removeFront();
	}//while
}//Hash Default Destructor
Example #16
0
File: prog59.c Project: hdddt/C
int main(void){
  Card card;
  Node *root = NULL, *ptr;
 

  printf("Enter name and age > ");

  while(scanf("%s %d", card.name, &card.age) !=EOF)
    {
 
  insertFront( &root, card );

  ptr = root;
  while ( ptr != NULL ) {
    printNode(ptr->card );
    ptr = ptr->next;
  }

  while ( root != NULL )
    removeFront( &root );

    }

  return (0);
}
Example #17
0
void destroyList(listNode * list) {
    if (list == NULL)
        return;
    while (list != NULL)
        list = removeFront(list);
    return;
}
Example #18
0
bool LinkedList<T>::removeBack()
{
	Node<T>* lastNode = nullptr;
	Node<T>* secondintoLast = nullptr;
	bool isRemoved = false;

	if(!isEmpty())
	{
		if(m_size == 1)
		{
			removeFront();
		}
		else
		{
			lastNode = m_front;
			while(lastNode->getNext() != nullptr)
			{
				secondintoLast = lastNode;
				lastNode = lastNode->getNext();
			}
			secondintoLast->setNext(nullptr);
			delete lastNode;
			m_size--;
		}
		isRemoved = true;
	}

	return(isRemoved);
}	
Example #19
0
Queue<T>::~Queue()
{
    DebugConsole::debug_print (1, true, COLOR_CYAN, "Destroying Queue.\n");
    while (!isEmpty())
    {
        // Responsibly destroy all nodes from queue
        removeFront();
    }
}
Example #20
0
/**
* Runs tests on the removeFront function by removing one node
**/
void removeFrontOne(ListPtr list)
{
	NodePtr node = removeFront(list);
	if(DEBUG > 0)
	{
		printf("\n%s\nRemoved Node: jobid: %d info: %s list size: %d", sep, node->data->jobid, node->data->command, list->size);
		//displayDebugInfo(list);
	}
}
Example #21
0
/*Destory the list, freeing all of the associated memory*/
void destroyList (Node ** theList) {
    if ((theList == NULL) || (*theList == NULL)) {
        return;
    }
    while (*theList != NULL) {
        Node * temp;
        temp = *theList;
        *theList = (*theList)->next;
        removeFront(temp);
    }
    *theList = NULL;

    return;
}
Example #22
0
File: prog59.c Project: hdddt/C
void removeRear ( Node **root){
  Node *ptr, *pre;

  if( (*root) != NULL){
    if( (*root)->next == NULL){
      removeFront( &(*root) );
    }else{
      ptr = (*root);
      while ( ptr->next != NULL){
	pre = ptr;
	ptr = ptr->next;
      }
      pre->next = NULL;
      free( ptr );
    }
  }
}
Example #23
0
/* Return any memory allocated for the List to the system */
void destroyList (List *L)
{
    node *currPos = L->head;
    
    if (currPos != NULL)    /* check to see if the list is empty */
    {
        while (currPos->next != NULL)
        {
            currPos = removeFront(L);
                
            currPos->nodeValue = 0;
            free(currPos);
        }
    }

    free(L);
    printf("\n");
}
Example #24
0
bool List::removeBack()
{
	if(isEmpty())
	{
		std::cout << "There are no values to remove" << std::endl;
		return false;
	}//if - display emptiness
	else if (m_size == 1)
	{
		return removeFront();
	}//elseif - 
	else
	{
		ONode* temp = m_front;
		ONode* temp1 = m_front;
		for(temp=m_front;((temp->getNext())->getNext())!=nullptr;temp=temp->getNext());
		temp1 = temp->getNext();
		temp->setNext(nullptr);
		delete temp1;
	}//else - list is not empty therefore, continue
	m_size--;
	return true;
}//removeBack - remove the end ONode from the list
Example #25
0
bool LinkedList<T>::removeBack()
{
	Node<T>* lastNode = nullptr;
	Node<T>* secondintoLast = nullptr;
	bool isRemoved = false;

	if(m_size != 0)
	{
		lastNode = m_front;
		if(m_size > 1)
		{
			for(int i = 1; i < m_size; i++)
			{
				if(i == (m_size-1))
				{
					secondintoLast = lastNode;
				}
				lastNode = lastNode->getNext();
			}
			secondintoLast->setNext(nullptr);
			delete lastNode;
			m_size--;
			isRemoved = true;
		}
		else if(m_size == 1)
		{
			removeFront();
			isRemoved = true;
		}
	}
	/** TODO 
		Fix this method
	*/

	return(isRemoved);
}	
Example #26
0
int Hash::pop()
{
	int val = m_front->getValue();
	removeFront();
	return val;
}//pop - returns first node value and destroys first node
Example #27
0
void runRandomTests(struct parameters *state, ListPtr list)
{
	int i;
	int start;
	int test;
	NodePtr node;
	JobPtr job;
	long int num;

   	srand(state->seed);
	start=0;
	if (state->restart) {
		start = state->done+1;
		srand(state->randomNum);
	}
    for (i=start; i<state->count; i++) {
		num = rand();
        test = num % NUM_TESTS;
		if ((i > 0) && ((i % CHECKPOINT_COUNT) == 0)) {
			fprintf(stderr, "checkpointing list, count = %d\n", i);
			state->done = i-1;
			state->randomNum = num;
			checkpointList(list, state->saveFile);
			checkpointTestSuite(state, stateFile);
		}
        switch (test) {
            case 0:
				if (DEBUG > 1) fprintf(stderr,"addAtFront\n");
                state->n++;
                job = createJob(state->n, "some info");
                node = createNode(job);
                addAtFront(list, node);
                break;
            case 1:
				if (DEBUG > 1) fprintf(stderr,"addAtRear\n");
                state->n++;
                job = createJob(state->n, "some info");
                node = createNode(job);
                addAtRear(list, node);
                break;
            case 2:
				if (DEBUG > 1) fprintf(stderr,"removeFront\n");
                node = removeFront(list);
                break;
            case 3:
				if (DEBUG > 1) fprintf(stderr,"removeRear\n");
                node = removeRear(list);
                break;
            case 4:
				if (DEBUG > 1) fprintf(stderr,"removeNode\n");
                node = removeNode(list, search(list, i));
                break;
            case 5:
				if (DEBUG > 1) fprintf(stderr,"reverseList\n");
                reverseList(list);

            default:
                break;
        }
    }
}
StringLinkedList::~StringLinkedList()			// destructor
{
	while (!empty()) removeFront();
}
Example #29
0
void DLinkedList<T>::deleteAllNodes()
{
        while(head != NULL)
        { removeFront(); }
        return;
}
// Empties list by deleting all the nodes
void List::clearList()
{
	while(head != NULL)
		removeFront();
}