Beispiel #1
0
Datei: List.c Projekt: kr094/c
void add(List *list, int val)
{
     list->size++;
     //if theres no list or we're pushing a value onto head
     if(list->head == 0)
	   addToHead(list, val);
     else if(val < list->head->val)
	   addToHead(list, val);
     else
     {     
		 Node* newNode = (Node*) malloc(sizeof(Node*));
		 newNode->val = val;
		 Node* curr = list->head;
		 
		 while(curr->next)
		 {
                 if(val >= curr->val && val <= curr->next->val)
                 {                   
			      newNode->next = curr->next;
			      curr->next = newNode;   
			      return;     
                 }
                 curr = curr->next;    
         }
	     newNode->next = 0;
	     curr->next = newNode;
     }     
}
Beispiel #2
0
void testAddToHead(){
    Node *list = NULL;
    list = addToHead(list, 5);
    printList(list);
    list = addToHead(list, 19);
    printList(list);
#if DELETE_DEFINED
    delList(list);
#endif
    printf(">> Test add to head completed! <<\n");
}
Beispiel #3
0
void testFindElement(){
    int vals[] = {0, 3, -153, 33895, 3049, 101010};
    int testCases[] = {3,   101010, -10,    0,      15,     1};
    bool answers[] =  {true, true,  false,  true,   false,  false};

    assert((int)getArrLen(testCases) == (int)getArrLen(answers));

    Node *list= NULL;
    assert(findElement(list, testCases[0]) == NULL);

    for(int i = 0 ; i < (int)getArrLen(vals) ; i++){
        list = addToHead(list, vals[i]);
    }

    for(int i = 0 ; i < (int)getArrLen(vals) ; i++){
        Node *temp = findElement(list, testCases[i]);
        assert((temp != NULL) == answers[i]);
        printf("Test %d in testFindElement passed!\n", i);
    }
#if DELETE_DEFINED
        delList(list);
#endif

    printf(">> Test find element completed! <<\n");
}
Beispiel #4
0
int runTestCase(CASE* c, linked_list* list) {
    int retval = 0;
    switch(c->op) {
        case ADD_FRONT:
            retval = addToHead(list, &(c->value));
            break;
        case ADD_BACK:
            retval = addToTail(list, &(c->value));
            break;
        case REMOVE_FRONT:
            retval = *((int*) removeFromHead(list));
            break;
        case REMOVE_BACK:
            retval = *((int*) removeFromTail(list));
            break;
        case SIZE:
            retval = list -> size;
            break;
    }
    if (retval != c->expected) {
        printf("failure, want %d, got %d\n", c->expected, retval);
        return FAILURE;
    }
    return SUCCESS;
}
Beispiel #5
0
/** \brief add new item to bucket list
 *
 * \param[in] guid Unique identifier for a bucket item (e.g. a URL)
 * \param[in,out] head pointer to the bucket list
 * \param[in] maxBucketItems number of maximum items in bucket list
 * \return always returns 0
 *
 * The size of the provided bucket list is kept to maxBucketItems.
 * If it gets larger than the specified value, the oldest element is removed from the list.
 */
int addToBucket(const char* guid, NODE **head, const int maxBucketItems) {
	addToHead(am_strdup(guid), head);
	if(maxBucketItems > 0 && listCount(*head) > (uint32_t)maxBucketItems) {
		dbg_printf(P_INFO2, "[add_to_bucket] bucket gets too large, deleting last item...\n");
		removeLast(*head, NULL);
	}

	return 0;
}
Beispiel #6
0
void testGetNumElements(){
    int vals[] = {0, 3, -153, 33895, 3049, 101010};
    Node *list= NULL;
    assert(getNumElements(list) == 0);
    for(int i = 0 ; i < (int)getArrLen(vals) ; i++){
        list = addToHead(list, vals[i]);
        assert(getNumElements(list) == i+1);
    }
#if DELETE_DEFINED
        delList(list);
#endif
    printf(">> Test get num elements completed! <<\n");
}
Node *insertAtN(Node *l, int v, int n){
    if(!l)
        return makeList(v);
    if(n == 0){
        return addToHead(l, v);
    }
    Node *before = traverseToN(l, n-1);
    assert(before);
    Node *after = before->next;
    before->next = makeList(v);
    before->next->next = after;
    return l;
}
LinearizedTrie *linearizeRTrie(RTrie *rt, LinearizedTrie *mp) {
    if (rt != NULL && rt->keys != NULL) {
        if (rt->value != NULL)
            mp = addToHead(mp, rt->value);
        
        register unsigned int i=0;
        while (i < BASE) {
            if (*(rt->keys + i) != NULL)
                mp = linearizeRTrie(*(rt->keys + i), mp);
            ++i;
        }
    }

    return mp;
}
Beispiel #9
0
/* Function name:   insertAtN
 * Description:     Inserts a new node BEFORE the Nth node of l.  That is, the
 *                  inserted node becomes the Nth node.  If l is NULL and n is
 *                  0, returns a new list with a node of value v.
 * Return:          The new list.
 * Parameters
 *      - l:        An existing list.
 *      - v:        The value of the new node.
 *      - n:        The place to insert into.
 * Failure cases:   If n < 0 or n > length(l), then an assertion will be
 *                  triggered, and a NULL list returned.
 */
Node *insertAtN(Node *l, int v, int n){
    myassert(nIsInRange(l, n));
    if(!l)
        return makeList(v);
    if(n == 0){
        return addToHead(l, v);
    }

    // Set before to the node before the place to insert
    Node *before  = traverseToN(l, n-1);
    Node *after   = before->next; // Save the current next
    Node *newNode = makeList(v);  // Create the new node
    before->next  = newNode;      // Link the previous nodes to the new node
    newNode->next = after;        // Link the new node to the posterior nodes
    return l;
}
Beispiel #10
0
Element *getCloseMatches(const char *query, RTrie *dict, const double percentMatch) {
    if (query == NULL || dict == NULL)
        return NULL;
    else {
        // First check if the query exists in the dict
        void *check = get(dict, pjwCharHash(query));

        // In case of any collisions, strcmp should help sort things out
        if (check != NULL) {
            Element *matchList = NULL;
            matchList = addToHead(matchList, check);
            return matchList;
        }
        else {
            int ownRank = getRank(query, query);
            return matches(query, dict, ownRank, percentMatch);
        }
    }
}
Beispiel #11
0
void addAstroid(LinkedList *pit, int sz) {
    ASTROID *psnow = (ASTROID *) malloc(sizeof(ASTROID));
    *psnow = makeAstroid(sz);
    addToHead(pit, psnow);
}
Beispiel #12
0
Node *makeRandListOfSizeN_bounded(int n, int upperBound){
    Node *list = NULL;
    for(int i = 0 ; i < n ; i++)
        list = addToHead(list, arc4random_uniform(upperBound));
    return list;
}
Beispiel #13
0
Node *makeRandListOfSizeN(int n){
    Node *list = NULL;
    for(int i = 0 ; i < n ; i++)
        list = addToHead(list, arc4random());
    return list;
}
Beispiel #14
0
int addItem(void* elem, NODE **head) {
	return addToHead(elem, head);
}
Beispiel #15
0
 // Method to push an item onto the stack
 void push(TCListNode* node) { addToHead(node); };