Exemplo n.º 1
0
int SLRemove(SortedListPtr list, void *newObj)
{

	SortedListIteratorPtr ptr = SLCreateIterator(list);
	if(ptr == NULL)
	{
		return 0;
	}

	listItem* cur;
	listItem* prev;


	//checking at head
	int compcur = list->compareF(newObj,list->head->data);
	if(compcur == 0)
	{
		//move head
		list->head = list->head->next;
		//if been viewed once, free
		if(ptr->current->viewers==1)
		{
			freeItem(ptr->current);
		}
		SLDestroyIterator(ptr);
		return 1;
	}

	cur = ptr->current->next;
	prev = ptr->current;

	while(ptr->current != NULL && prev != NULL)
	{
		compcur = list->compareF(newObj, SLNextItem(ptr));
		if(compcur == 0)
		{
			prev->next = cur->next;
			if(cur->viewers == 1)
			{
				freeItem(cur);
			}
			SLDestroyIterator(ptr);
			return 1;
		}

		prev = cur;
		cur = cur->next;


	}
	SLDestroyIterator(ptr);
	printf("we should not have gotten here");
	return 0;





}
Exemplo n.º 2
0
int SLRemove(SortedListPtr list, void *newObj) {
  Node *curr;
  Node *prev;
  curr = list->front;
  prev = NULL;
  // Search through the list for the node to be removed.
  while (curr != NULL) {
	// If found remove it from the list.
    if (list->compareFunct(curr->content, newObj) == 0) {
		// If it is in the front of the list.
		if (prev == NULL) {
			list->front = curr->next;
			curr->ptrCounter--;
			if (list->front != NULL) {
				list->front->ptrCounter++;
			}
		}
		else {
			prev->next = curr->next;
			curr->ptrCounter--;
			if (prev->next != NULL) {
				prev->next->ptrCounter++;
			}
		}
		// If its pointer counter falls to zero, delete it.
		if (curr->ptrCounter <= 0) {
			list->destroyFunct(curr->content);
			if (curr->next != NULL) {
				curr->next->ptrCounter--;
			}
			free(curr);
		}
      return 1;
    }
    /*
     * If the current content is larger than the content to be removed, move on.
     * The node to be removed must lie after this node.
     */
    else if (list->compareFunct(curr->content, newObj) > 0) {
      prev = curr;
      curr = curr->next;
    }
    /*
     * If the current content is less than the content to be removed, simply
     * stop. The content to be removed doesn't exist in the list. Return 0.
     */
    else {
      return 0;
    }
  }
  // Otherwise you couldn't find the element in the list.
  return 0;
}
Exemplo n.º 3
0
int SLRemove(SortedListPtr list, void *newObj)
{
												//If there is no data in the list, or no data at the head node, 
    if(!list || list->head == NULL || !newObj){	//or no data in the new object, nothing to remove.
        return 0;
    }		

    Node *ptr = list->head;
    Node *prev = NULL;
    while(ptr != NULL){
        if(list->cf(ptr->data, newObj) == 0){ //If the Object we are looking to remove is the head node
            if(prev == NULL){   				//Remove the head node and have the node that comes after take its place
                list->head = list->head->next;
                if(list->head){
                    list->head->counter++;     //increment counter so the SortedListPtr is pointing to the new head
                }
                ptr->counter--;    
                ptr->moveRef = 1;
                if(ptr->counter <= 0){     
                    list->df(ptr->data);	//If no object is found at the current node, destroy it.
                    if(ptr->next !=NULL){   
                        ptr->next->counter--;  //If there is a ptr to the next node, decrement the reference counter
                    }
                    free(ptr);				//Free the memory at the ptr if there was no data in the node
                    return 1;
                }
            }
            else{
                prev->next = ptr->next;     //If we do not delete the head node, we must find the node we need to delete
                if(ptr->next != NULL){      //If we are at the current node and it is not what we are looking for,
                    ptr->next->counter++;	//and the node has a next, we increment reference counter and go to the next node
                    ptr->counter--;
                    ptr->moveRef = 1;
                }
                if(ptr->counter <=0){
                    list->df(ptr->data);
                    if(ptr->next != NULL){
                        ptr->next->counter--;
                    }
                    free(ptr);
                    return 1;
                }
            }
        }
        prev = ptr;
        ptr = ptr->next;
    }
    return 0;
}
Exemplo n.º 4
0
int SLInsert(SortedListPtr list, void *newObj)
{
	// Create node cast link as a self referential node
	SortedNodePtr newItem = malloc(sizeof(struct SortedNode));

	// Set new item values
	newItem->value = newObj;
	newItem->count = 1;
	newItem->link = NULL;
	
	// Temp node to traverse through list
	SortedNodePtr current = list->head;

	// Insert
	if(current == NULL)
	{
		// Point to new item and increment size of list
		list->head = newItem;
		list->counter++;

		return 1;
	}
	else
	{
		if(list->compare(current->value, newObj) != 0)
		{
			if(list->compare(current->value, newObj) < 0)
			{
				// Add new item to front
				newItem->link = list->head;
				list->head = newItem;

				//Increment size of list
				list->counter++;

				return 1;
			}

			// Sort and add
			sort(list, newItem);

			return 1;
		}
	}

	// ERROR
	free(newItem);
	return 0;
}
Exemplo n.º 5
0
int SLRemove(SortedListPtr list, void *newObj)
{
	SortedNodePtr current = list->head, previous = NULL;

	while(current->link != NULL && list->compare(current->value, newObj) != 0)
	{
		// Store current node into previous
		previous = current;

		// Move to next node
		current = current->link;
	}

	// Only remove if count on node is one or lower
	if(previous == NULL && current->count <= 1)
	{
		list->head = current->link;
		list->destruct(current->value);
		free(current);
		
		// Decrement list size
		list->counter--;

		return 1;
	}
	else if(current->count <= 1)
	{
		// Previous points to node after the one being deleted
		previous->link = current->link;

		// Free the memory
		list->destruct(current->value);
		free(current);

		// Decrement list size
		list->counter--;

		return 1;
	}
	else
	{
		// Make reference count go down by one
		current->count--;
	}

	// Couldn't remove
	return 0;
}
Exemplo n.º 6
0
 /*SLInsert_T is a function that should only be called internally by SLInsert.
	it is used for the recursive cases of insertion.*/
static int SLInsert_T(SortedListPtr list, ListNode prevNode, ListNode curNode, void *newObj){
	/*If traversed to node that doesn't exist, failure occurs*/
	if (curNode == NULL) return 0;
	
	if ( list->cf(newObj,  curNode->item) > 0) {
	
		/*If nowhere else to go, insert at end of list*/
		if (curNode->next == NULL){
			ListNode lnNew = NodeCreate(newObj, curNode->next);
			curNode->next = lnNew;
			return 1;			
		}
		
		/*If new obj is less than current, keep iterating through*/
		return SLInsert_T(list, curNode, curNode->next, newObj);
	}
	
	/*At this point, new obj is less than or equal current node's obj, insert before it*/
	
	ListNode lnNew = NodeCreate(newObj, curNode->next);
	
	/*At this point, it is in list node and pointing to
	  predecessor's sucessor*/
	prevNode->next = lnNew;
	lnNew->next = curNode;
	
	/*Now predecessor points to new object's list node,
	  and new list node points to the predecessor's previous
	  successor*/
	return 1;
}
Exemplo n.º 7
0
 /*-1 if first obj smaller, 0 if equal, 1 if 2nd obj is smaller*/
int SLInsert(SortedListPtr list, void *newObj){

	if (newObj == NULL) return 0; /*Disallow null objects storage*/
	
	ListNode curNode = list->begin;
	if (curNode == NULL) {
		ListNode lnNew = NodeCreate(newObj, list->begin);
		list->begin = lnNew;
		return 1;
	
	} else if ( list->cf(newObj,  curNode->item) < 0) {
		/*New obj is greater than head node, becomes new head node*/

		ListNode lnNew = NodeCreate(newObj, list->begin);
		
		if (lnNew == NULL) return 0;
		
		list->begin = lnNew;
		lnNew->next = curNode;
		/*Now predecessor points to new object's list node,
		  and new list node points to the predecessor's previous
		  successor*/
		return 1;	
	
	
	
	} else{
	
		return SLInsert_T(list, list->begin, curNode, newObj);
	}
	
		


}
Exemplo n.º 8
0
 /*SLRemove_T is a function that should only be called internally by SLRemove.
	it is used for the recursive cases of deletion.*/
static ListNode SLRemove_T(SortedListPtr list, ListNode curNode, void *newObj){


	if (curNode == NULL) return NULL; /*Unsuccessful*/
	
	/*Compare with comparator function, then remove*/
	if ( list->cf(newObj,  curNode->item) == 0) {
		/*Found node, remove*/
		
		ListNode lsTemp;
		lsTemp = curNode->next;
		
		if (curNode->refCount == 1){
			free (curNode->item);
			free (curNode);	
		}
		
		return lsTemp;
	}
	
	curNode->next = SLRemove_T(list, curNode->next, newObj);
	return curNode;
	
	
}
Exemplo n.º 9
0
int SLRemove(SortedListPtr list, void *newObj){

	ListNode curNode = list->begin;	
	if ( list->cf(newObj,  curNode->item) == 0) {
		/*Node to remove is at head*/
		list->begin = curNode->next;
		
		if (curNode->refCount == 1){
		
			free (curNode->item);
			free (curNode);
		}
		
		return 1;
	
	} else {
		if ( SLRemove_T(list, curNode, newObj) == NULL){
			return 0;  /*Unsuccessful*/
		} else return 1;	
	
	}

	

}
Exemplo n.º 10
0
int SLInsert(SortedListPtr list, void *newObj)
{
	//If there is no list or no new data to insert into the node, return successfully
    if(!list || !newObj){       
        return 0;               
    }

    Node *newNode = nodeMake(newObj);	//Create a node object to store the data to be inserted
	
    if(list->head == NULL || list->cf(list->head->data, newObj) < 0){
        newNode->next = list->head;     //If the data entered is greater than the data held at the head node
        list->head = newNode;           //Make the new node the head node and make the head the new node
        return 1;
    }

    Node *ptr = list->head;		//If the new node is not greater, iterate through the list to determine where to enter the new data
    Node *prev= NULL;		
	
    while(ptr != NULL){			//While the list is the not at the last node
        if(list->cf(ptr->data, newObj) == 0){   //if the data is a duplicate, do not add the node to the list
            return 0;
        }
        else if(list->cf(ptr->data, newObj) < 0){ //if newObj is bigger, insert
            Node *newNode = nodeMake(newObj);
            if(prev == NULL){       //if the data is bigger than that of the data held at the head node
                Node *temp = ptr;       //Make the new data the head node and point the old head to the head->next node
                list->head = newNode;   
                newNode->next = temp;   
            }
            prev->next = newNode;
            newNode->next = ptr;
            newNode->counter++;
            return 1;
        }
        else if(list->cf(ptr->data, newObj) > 0){ //As long as the data is smaller, we must continue to traverse through the list
            prev = ptr;
            ptr = ptr->next;
        }
    }
    prev->next = newNode;
    return 1;
}
Exemplo n.º 11
0
/*
 * SLDestroy destroys a list, freeing all dynamically allocated memory.
 *
 * You need to fill in this function as part of your implementation.
 */
void SLDestroy(SortedListPtr list) {
  Node *temp;
  // Runs through the entire list, deleting the front node every time through.
  while (list->front != NULL) {
    temp = list->front;
    list->front = list->front->next;
    // Destroying the content pointed to by the node and then freeing the node.
    list->destroyFunct(temp->content);
    free(temp);
  }
  // Freeing the list after we destroy all the nodes.
  free(list);
}
Exemplo n.º 12
0
void SLDestroy(SortedListPtr list)
{
  node *p = list->base->next;
  // destroy every node in the list until reach the end of the list
  while (p != list->base) {
    p->prev->next = p->next;
    p->next->prev = p->prev;
    list->DF(p->data);
    free(p);
    p = list->base->next;
  }
  free(list->base);
  free(list);
}
Exemplo n.º 13
0
int SLRemove(SortedListPtr list, void *newObj)
{
  node *p = list->base->next;
  // find the first item equal to newObj
  while (p != list->base) {
    if (list->CF(newObj, p->data) == 0) {
      break;
    }
    p = p->next;
  }
  // if not find an item equal to newObj
  if (p == list->base)
    return 0;
  p->next->prev = p->prev;
  p->prev->next = p->next;
  //if there is no iterator "pointing" to this item, free it.
  if (p->refByItrCount == 0) {
    list->DF(p->data);
    free(p);
  }
  p->next = NULL;
  p->prev = NULL;
  return 1;
}
Exemplo n.º 14
0
void SLDestroy(SortedListPtr list)
{
    if(!list){
        return;
    }
	//As long as the list is still exsistant, iterate through destroying all node data.
    Node *ptr = NULL;	//Ptr to the node now points to null
    while(list->head != NULL){
        list->df(list->head->data);			//Use the destroy function
        ptr = list->head;
        list->head = list->head->next;		//The new head of the list is the next node
        free(ptr);						//free the memory that was allocated for the node ptr
    }
    free(list);				//Free the memory that was allocated for the list
}
Exemplo n.º 15
0
/*
 * SLInsert looks through the list until it finds the end of the list or 
 * a value that is less that newObj. If the value of newObj is already 
 * in the list, then SLInsert tells the caller that they're an idiot and 
 * returns zero. If the value of newObj is valid, it is added to the list.
 * If it was inserted at the beginning of the list, the list's head pointer 
 * points to it. Otherwise, the preceeding node points to the node that was 
 * just added. Finally, the recently added node's numPointers variable 
 * is incremented.
 */
int SLInsert( SortedListPtr list, void *newObj ) {
  NodePtr prev = NULL;
  NodePtr current = list->head;
  while ( current != NULL && list->cf(current->data, newObj) < 0 ) {
      prev = current;
      current = current->next;
  }
  NodePtr n = NCreate( newObj, list->df, current );
  if ( prev == NULL ) {
    list->head = n;
  } else {
    prev->next = n;
  }
  n->numPointers++;
  return 1;
}
Exemplo n.º 16
0
void SLDestroy(SortedListPtr list){
	if(list == NULL)
		return; // dont do anything, nothing to destroy
	else{
		NodePtr deleter;

		while(list->head != NULL){ //delete all dynamically allocated nodes
			deleter = list->head;
			list->head = list->head->next;
			list->destroyer(deleter->data);
			free(deleter);
		}

		//functions are not dynamically allocated, they are created in main, so do not need to be free'd
		free(list); //then free the structure
	}
}
Exemplo n.º 17
0
void SLDestroy(SortedListPtr list)
{
	// Traverse to end up list while deallocating memory
	while(list->head != NULL)
	{
		// TempNode points to current and current points to next node
		SortedNodePtr tempNode = list->head;
		list->head = tempNode->link;

		// Free data if need be
		list->destruct(tempNode->value);

		// Free the node
		free(tempNode);
	}

	// Free everything else in list
	free(list->head);
	free(list);
}
Exemplo n.º 18
0
/*
 * SLRemove first finds newObj in the list. If it can't be found, the function 
 * returns zero. The node containing the value of newObj is removed from 
 * the list and it's numPointers variable is decremented. If the node's 
 * numPointers is less than or equal to zero, it is destroyed.
 */
int SLRemove( SortedListPtr list, void *newObj ) {
  NodePtr prev = NULL;
  NodePtr current = list->head;
  while ( current != NULL && list->cf(current->data, newObj) != 0 ) {
    prev = current;
    current = current->next;
  }
  if ( current == NULL ) {
    printf( "Item not found in list.\n" );
    return 0;
  }
  if ( prev == NULL ) {
    list->head = current->next;
  } else {
    prev->next = current->next;
  }
  current->numPointers--;
  if ( current->numPointers <= 0 ) {
    NDestroy( current );
  }
  return 1;
}
Exemplo n.º 19
0
int SLInsert(SortedListPtr list, void *newObj)
{
  node *p = list->base->next;
  // find the first item larger or equal to newObj
  while (p != list->base) {
    if (list->CF(newObj, p->data) != -1) {
      break;
    }
    p = p->next;
  }
  node *tmp = malloc(sizeof(node));
  if (tmp == NULL)
    return 0;
  // initialize a node
  tmp->refByItrCount = 0;
  tmp->data = newObj;
  // insert a node
  tmp->prev = p->prev;
  p->prev->next = tmp;
  tmp->next = p;
  p->prev = tmp;
  return 1;
}
Exemplo n.º 20
0
int SLInsert(SortedListPtr list, void *newObj)
{
	if ((list->thing == NULL) && (list->PtrNext == NULL)) //they are giving us the head, but we cannot check directly
	{
		list->thing = newObj;
		list->PtrNext = NULL;
		return 1;
	}
	
	//int insertbefore = 0;
	SortedListPtr thingThatPointsToUs = list;
	SortedListPtr current = list;
/*
 * "insertbefore" is the index item of the element of the list, that our newly inserted item will point TO.
*/
	BOOL endOfList = FALSE, stillStepping = FALSE;

	do
	{
		int comparison_result = list->cf(current->thing, newObj);
		printf("compare result of %d to %d is %d\n", *((int*)current->thing), *((int*)newObj), comparison_result);
		//It's not doing this comparison enough. Not comparing 3 to 4 after we add it. 
		switch (comparison_result)
		{
			case 0:
				//*(current->thing) == *newObj
				return 0;
			case 1:
				//*(current->thing) > *newObj
				
				if (current->PtrNext != NULL)
				{
					thingThatPointsToUs = current;
					if (current != NULL) {
						current = current->PtrNext;
					}
					continue;
				}
				else
				{
					printf("we're not supposed to be here\n");
					endOfList = TRUE;
				}

				stillStepping = FALSE;
				break; 
			case -1:
				//*(current->thing) < *newObj

				// 1 is in the list, we're trying to add 3
				// ok.
				// 3, 1 are in the list. we want to add 0.
				//
				stillStepping = TRUE;

				break;
			default:
				printf("bad CompareFuncT implementation! exiting\n");
				exit(-1);
		}
		
		if (!stillStepping)
		{
			SortedListPtr newThing = malloc(sizeof(SortedList));
			if(newThing != NULL)
			{
				newThing->thing = newObj;
				//printf("current = %x\n", current);
				//printf("endOfList = %d\n", endOfList);
				newThing->PtrNext = endOfList ? NULL : current;
				//newThing->PtrNext = GetSLPFromIndex(insertbefore, list);
				
				if (thingThatPointsToUs == list->head) {
					//printf("tTPtU's value is %d\n", *((int*)thingThatPointsToUs->thing));
					list->head = newThing;
					printf("we are the new head\n");
				} else {		
					thingThatPointsToUs->PtrNext = newThing;
					printf("set thingThatPointsToUs's PtrNext to %x", thingThatPointsToUs->PtrNext);
				}

				printf("set newThing's PtrNext to %x\n", newThing->PtrNext);

				return 1; 
			}
		}

		thingThatPointsToUs = current;
		current = current->PtrNext;

	} while((current != NULL) && (current->PtrNext != NULL));

	//if we're here, we're adding something to the end of the list.
	SortedListPtr newThing = malloc(sizeof(SortedList));
	if (newThing != NULL)
	{
		newThing->thing = newObj;
		newThing->PtrNext = NULL;
		return 1;
	}	

	printf("unsuccessful insertion\n");
	return 0;
}
Exemplo n.º 21
0
int SLInsert(SortedListPtr list, void *newObj) {
  Node *newNode;
  Node *curr;
  Node *prev;
  curr = list->front;
  prev = NULL;
  // Creating the node to be inserted.
  newNode = (Node *)malloc(sizeof(Node));
  // If malloc fails print an error statement and return 0.
  if (newNode == NULL) {
    //printf("Error: Out of memory.\n");
    return 0;
  }
  // Set the values of the new node.
  newNode->content = newObj;
  newNode->ptrCounter = 0;
  // If the list is empty just put the node in front.
  if (list->front == NULL) {
    list->front = newNode;
    newNode->ptrCounter++;
    newNode->next = NULL;
    return 1;
  }
  // Searching through the list for the correct position of the new node.
  while (curr != NULL) {
    // If a duplicate is found print an error statement and return 0.
    if (list->compareFunct(curr->content, newObj) == 0) {
      //printf("Error: Duplicate value insertion.\n");
      return 0;
    }
    /* 
     * If the current content is "smaller" than the content to be inserted, then
     * the content to be inserted must come right before the current content.
     */
    else if (list->compareFunct(curr->content, newObj) < 0) {
      // Check to see if the new node is to be put in the front of the list.
      if (prev == NULL) {
		list->front = newNode;
		newNode->ptrCounter++;
		newNode->next = curr;
		return 1;
      }
      // Otherwise just insert the new node.
      else {
		prev->next = newNode;
		newNode->ptrCounter++;
		newNode->next = curr;
		return 1;
      }
    }
    /* 
     * If the current content is "bigger" than the content to be inserted, then
     * simply continue, fr the correct position must be somewhere after the
     * current node.
     */ 
    else {
      prev = curr;
      curr = curr->next;
    }
  }
  /*
   * If you get to the end of the non-empty list and all the content was larger,
   * then just throw the new node at the end (it is smaller than everything else
   * in the list.
   */
  if (curr == NULL && prev != NULL) {
    prev->next = newNode;
    newNode->ptrCounter++;
    newNode->next = curr;
    return 1;
  }
  // Otherwise you somehow failed.
  return 0;
}
Exemplo n.º 22
0
int SLInsert(SortedListPtr list, void *newObj)
{
	SortedListIteratorPtr iPtr = SLCreateIterator(list);

	if(iPtr == NULL)
	{
		return 0;
	}

	listItem *temp;
	listItem *swap;
	temp = (listItem*)malloc(sizeof(listItem));

	if(temp == NULL){
		fprintf(stderr, "Out of memory\n");
		exit(EXIT_FAILURE);
	}
	temp->data = newObj;
	temp->next = NULL;
	
	if(list->head == NULL)
	{
		printf("first item\n");
		list->head = temp;

	}
	else{

		while(iPtr->current->next != NULL){
			int compcurr = list->compareF(temp->data,iPtr->current->data);
			int compnext = list->compareF(temp->data, iPtr->current->next->data );
			if(compcurr==0){
				printf("already here\n");

				return 0;
			}
			else if(compcurr == -1 && compnext == 1){//found the right place
				printf("Put in middle\n");
				swap = iPtr->current->next;//save the next one
				iPtr->current->next = temp;//put it in the right place
				iPtr->current = iPtr->current->next;//iterate
				iPtr->current->next = swap; //place the saved one
				//SLDestroyIterator(iPtr);
				return 1;
			}
			else
				iPtr->current = iPtr->current->next;
		}
		int compcurr = list->compareF(temp->data, iPtr->current->data);
		if(compcurr == -1){
			printf("put at end\n");
			iPtr->current->next = temp;//put it in the right place
			iPtr->current = iPtr->current->next;//iterate
			//SLDestroyIterator(iPtr);
			return 1;
		}
		else{
			printf("put at beginning\n");
			swap = list->head;
			list->head = temp;
			iPtr->current =  list->head;
			iPtr->current->next = swap;
			//SLDestroyIterator(iPtr);

			return 1;

		}
	}


	return 1;
}
Exemplo n.º 23
0
int StringInsert(SortedListPtr list, void *newObj, char*key){
    if(list->head==NULL)
    {
        NodePtr newNode = NodeCreate(newObj, key);
        
        list->head = newNode;
        
        
        
        
        
        return 1;
    }
    else
    {
        
        
        NodePtr cur = (NodePtr)malloc(sizeof(Node));
        cur = list->head;
        
        
        NodePtr prev = NULL;
        
        
        /*Move the cur and prev pointers until cur points to the node where the newObj
         should be inserted and prev points to the node before that point. */
        
        CompareFuncT comp  = list->compareTo;
        int aa = strcmp(key, cur->name);
        if(aa < 0)
        {
            
            
            NodePtr newNode = NodeCreate(newObj, key);
            
            newNode->next = cur;
            
            list->head = newNode;
            newNode->refcount += 1;
            
            
            
            
            return 1;
        }
        else
        {
            
            while((cur != NULL) && (list->compareTo(newObj,cur->data)) >= 0)
            {
                if(newObj == cur->data)
                {
                    
                    return 1;
                }
                prev = cur;
                cur = cur->next;
            }
            
            
            NodePtr newNode = NodeCreate(newObj, key);
            /*Insert*/
            
            
            prev->next = newNode;
            
            newNode->refcount += 1;
            
            newNode->next = cur;
            
            
            
            return 1;
        }
    }
}
Exemplo n.º 24
0
//also acts as a sort of garbage collection, when looking for newObj, it looks for invalid objects with no pointers to free the node
int SLRemove(SortedListPtr list, void *newObj){
	//base cases, avoids segfault
	if(list == NULL){
		printf("List was not allocated properly\n");
		return 0;
	}
	else if(list->head == NULL){
		printf("List is empty\n");
		return 0;
	}
	
	//checks against head
	else if(list->comparator(list->head->data, newObj) == 0){
		if(list->head->numPointers == 0){
			NodePtr tempNode = list->head; //must save node to be freed
			list->head = list->head->next; //update head
			list->destroyer(tempNode->data);
			free(tempNode);
			return 1;
		}
		else{
			if(!list->head->isValid){ //if item was already removed but still has a pointer
				printf("Item was not found\n");
				return 0;
			}

			list->head->isValid = false;
			return 1;
		}
	}
	else
	{
		NodePtr lagging = list->head;
		NodePtr traverse = list->head->next;

		while(traverse != NULL)
		{	
			//garbage collection !!TEST GARBAGE COLLECTION MAKE SURE IT WORKS PROPERLY
			if(!(traverse->isValid) && (traverse->numPointers == 0)){
				lagging->next = traverse->next;
				list->destroyer(traverse->data);
				free(traverse);
				traverse = lagging->next;
			}

			if(list->comparator(traverse->data, newObj) == 0)
			{
				if(traverse->numPointers == 0){
					lagging->next = traverse->next;
					list->destroyer(traverse->data);
					free(traverse);
					return 1;
				}
				else{
					if(!traverse->isValid){ //if item was already removed but still has a pointer
						printf("Item was not found\n");
						return 0;
					}

					traverse->isValid = false;
					return 1;
				}	
			}

			lagging = traverse;
			traverse = traverse->next;
			
		}
	}
	//garbage collection for first item, done last so parsing isn't affected !!TEST THIS CASE AS WELL
	if(!(list->head->isValid) && (list->head->numPointers == 0)){
		NodePtr oldHead = list->head; //keep track of the old head to be free'd
		list->head = list->head->next;
		list->destroyer(oldHead->data);
		free(oldHead);
	}

	printf("Item was not found\n");
	return 0;
}
Exemplo n.º 25
0
//insert avoids duplicates as stated in the reqs
// since duplicates are important for the word frequency I made it return a special error code of 2
int SLInsert(SortedListPtr list, void *newObj){
	if(list == NULL){
		printf("List was not allocated properly\n");
		return 0;
	}
	else{
		//allocate a temporary node, which will be inserted into the list if its valid, free'd if its a duplicate
		//each parameter is explained in the header file
		NodePtr tempNode = (NodePtr) malloc(sizeof(struct Node));
		tempNode->data = newObj;
		tempNode->next = NULL;
		tempNode->numPointers = 0;
		tempNode->isValid = true;

		if(list->head == NULL){ //if this is the first item to be added to the list
			list->head = tempNode; //simply make it the head of the list
		}
		else if(list->head->next == NULL){ //there is only 1 item in the list
			if(list->comparator(list->head->data, newObj) == 0){ //same as head
				free(tempNode); //don't need it to store data
				return 2;
			}
			else if(list->comparator(list->head->data, newObj) < 0){ //new object is smaller (it becomes second on list)
				list->head->next = tempNode;
				return 1;
			}
			else{ //new object is larger, it becomes the head
				tempNode->next = list->head;
				list->head = tempNode;
				return 1;
			}
		}
		else{ //there are at least 2 items in the list
			if(list->comparator(list->head->data, newObj) == 0){ //since the loop beggins on second object we must check it against head first
				list->destroyer(tempNode->data);
				free(tempNode); //don't need it to store data
				return 2;
			}
			else if(list->comparator(list->head->data, newObj) > 0){ //if object is larger than head it becomes head
				tempNode->next = list->head;
				list->head = tempNode;
				return 1;
			}

			NodePtr traverse = list->head->next;
			NodePtr lagging = list->head;
			while(traverse != NULL){
				if(list->comparator(traverse->data, newObj) == 0){ //duplicate
					list->destroyer(tempNode->data);
					free(tempNode); //don't need it to store data
					return 2; //duplicate error
				} 		
				else if(list->comparator(traverse->data, newObj) < 0){ //new object is smaller (it goes towards end of list)
					lagging = traverse;
					traverse = traverse->next;
				}
				else{ //new object is larger (add it to this spot)
					tempNode->next = traverse;
					lagging->next = tempNode;
					return 1;
				}
			}
			if(list->comparator(lagging->data, newObj) < 0){ //tests one more time, if smaller than last item, adds to the end of the list
				lagging->next = tempNode;
				return 1;
			}
		}
	}
	return 1; //makes compiler happy
}