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;	
	
	}

	

}
 /*-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);
	}
	
		


}
 /*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;
	
	
}
 /*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;
}
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;
}
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;
}
Esempio n. 7
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;
}
Esempio n. 8
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;
}
Esempio n. 9
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;
}