Пример #1
0
int main() {

	#ifdef TEST1
		NodeP head, tail;
		list_init(&head, &tail);

		printf("--- test1\n-- creating the list:\n   20 19 .. 1 1 2 .. 10\n");
		for (int i = 0; i < SIZE_TEST1 - 10; i++)
			list_insert(&head, &tail, i+1);
		for (int i = 0; i < SIZE_TEST1 - 20; i++)
			list_append(&head, &tail, i+1);
		list_print_head(&head);
		printf("-- replace 10th node with 1337\n");
		printf("press key to continue: "); getchar();
		list_insert_at(&head, 10, 1337);
		list_print_head(&head);
		printf("-- deleting head and tail\n");
		printf("press key to continue: "); getchar();
		list_del_at(&head, &tail, 1);	// 1  -->head
		list_del_at(&head, &tail, 0);	// <1 -->tail
		list_print_head(&head);
		printf("-- inserting 20 at head, 10 at tail\n");
		printf("press key to continue: "); getchar();
		list_insert(&head, &tail, 20);
		list_append(&head, &tail, 10);
		list_print_head(&head);
		printf("-- reversing head to tail list\n");	
		list_rev(&head);
		list_print_head(&head);
		//list_print_head(&head);
		//printf("%d\n", head->next->data);
		printf("-- deleting list and attempting to print\n\n");
		list_del(&head);
		list_print_head(&head);
		// once deleted, the list becomes unusable
	#endif
	
	#ifdef TEST2
		printf("--- test2\n");
		NodeP head2, tail2;
		list_init(&head2, &tail2);
		for (int i = 0; i < SIZE_TEST2; i++)
			list_insert(&head2, &tail2, i);
	#endif

	#if defined TEST2 && defined TEST3
		// traverse and insert at end
		printf("--- test3\n");
		int toInsert = 0;
		// essentially traveral beginning to end
		list_insert_at(&head2, SIZE_TEST2 - 1, toInsert);	
		printf("-- inserted %d, traversed to %d\n", toInsert, tail2->data);
	#endif

	return 0;
}
Пример #2
0
void
test_list_insert_at_last(
		)
{
	list_type *list;
	TEST_INIT("list_insert_at (first)", "testingXXX");
	list = create_testing();

	list = list_insert_at(list, NULL, (void *) 'X');
	list = list_insert_at(list, NULL, (void *) 'X');
	list = list_insert_at(list, NULL, (void *) 'X');
	TEST_FINISH(list);
}
Пример #3
0
void
test_list_insert_at_first(
		)
{
	list_type *list;
	TEST_INIT("list_insert_at (first)", "XXXtesting");
	list = create_testing();

	list = list_insert_at(list, list, (void *) 'X');
	list = list_insert_at(list, list, (void *) 'X');
	list = list_insert_at(list, list, (void *) 'X');
	TEST_FINISH(list);
}
Пример #4
0
///
/// Prints a formatted string to the input cache of the current state.
///
void ppimpl_printf(state_t* state, const char* fmt, ...)
{
    char* store;
    int count, i;
    va_list argptr;
#ifndef WIN32
    va_list argptrc;
#endif
    va_start(argptr, fmt);
#ifndef WIN32
    va_copy(argptrc, argptr);
#endif
    count = vsnprintf(NULL, 0, fmt, argptr);
#ifndef WIN32
    count++;
#endif
    store = malloc(count + 1);
    memset(store, '\0', count + 1);
#ifndef WIN32
    vsnprintf(store, count, fmt, argptrc);
#else
    vsnprintf(store, count, fmt, argptr);
#endif
    va_end(argptr);
    for (i = 0; i < count; i++)
    {
        if (store[i] == '\0')
            continue;
        list_insert_at(&state->cached_input, (void*)store[i], state->print_index++);
    }
    free(store);
}
Пример #5
0
void main(int argc, char* argv[])
{
	list * l;
	list_iter it;
	intptr_t i;
	char* sv[] = { "fff", "zzz", "rrr", "bbb", "ggg", "hhh", "lll", "ttt" };
	
	//list test
	l = list_new();
	for (i=0; i<10; ++i) {
		(i%2 ? list_append(l, (void*)i): list_prepend(l, (void*)i));
	}
	list_insert_at(l, (void*)20,9);
	list_append(l, (void*)22);
	list_delete_item(l, (void*)22);
	list_delete_at(l, 10);
	list_dump(l, "%d\n");
	list_free(l);
	
	//string list test
	l = list_new_full(free);
	for(i=0; i<8; ++i) list_append(l, strdup(sv[i]));
	list_prepend(l, strdup("nine"));
	list_delete_item_comp(l, strdup("seven"), (list_comparator)strcmp);
	list_delete_item_comp(l, "eee", (list_comparator)strcmp);
	list_dump(l, "%s\n");
	list_free(l);
	
	l = list_new();
	for(i=0; i<8; ++i) list_insert_sorted_comp(l, sv[i], (list_comparator)strcmp);
	list_dump(l, "%s\n");
	list_free(l);
}
Пример #6
0
int main(int argc, const char *argv[])
{
	int i, ret;
	int arr[] = {12, 9, -1, 23, 2, 34, 6, 45};
	list *ptr = NULL, *ptr2 = NULL; 
	ptr = list_create();
	ptr2 = list_create();
	if (NULL == ptr) {
		printf("error\n");
		exit(1);
	}

	for (i = 0; i < sizeof(arr)/sizeof(*arr); i++) {
		ret = list_insert_at(ptr, DATA_INIT_INDEX, &arr[i]);
		if (ret < 0) {
			fprintf(stderr, "isnert err %d\n", ret);
			exit(1);
		}
	}

	list_display(ptr);

	int tmp = 100;
	list_insert_at(ptr, 1, &tmp);
	list_display(ptr);
	list_destory(ptr);

	for (i = 0; i < sizeof(arr)/sizeof(*arr); i++) {
		ret = list_order_insert(ptr2, &arr[i]);
		if (ret < 0) {
			fprintf(stderr, "isnert err %d\n", ret);
			exit(1);
		}
	}

	list_display(ptr2);

	tmp = 23;
	list_delete(ptr2, &tmp);
	list_display(ptr2);

	list_delete_at(ptr2, 2, &tmp);
	printf("delete_at %d\n", tmp);
	list_display(ptr2);

	return 0;
}
Пример #7
0
void
test_list_insert_at_middle(
		)
{
	list_type *list;
	list_type *ptr;
	TEST_INIT("list_insert_at (middle)", "teXstXiXng");
	list = create_testing();

	ptr = list_find(list, (void *) 'i');
	list = list_insert_at(list, ptr, (void *) 'X');
	ptr = list_find(list, (void *) 's');
	list = list_insert_at(list, ptr, (void *) 'X');
	ptr = list_find(list, (void *) 'n');
	list = list_insert_at(list, ptr, (void *) 'X');
	TEST_FINISH(list);
}
Пример #8
0
static int __register(struct timejob *tj)
{
	struct list_iterator li;
	int i, retval;
	int insert_it;
	struct timejob *p;
	
	retval = 0;
	insert_it = 0;
	i = 0;
	list_iter_set(&li, &timejob_list);
	while ((p = list_iter_get(&li))) {
		if (p->j_ts.tv_sec > tj->j_ts.tv_sec ||
		    (p->j_ts.tv_sec == tj->j_ts.tv_sec && p->j_ts.tv_nsec > tj->j_ts.tv_nsec)) {
			insert_it = 1;
			break;
		}
		i++;
	}
	list_iter_end(&li);
	if (insert_it) {
		list_insert_at(&timejob_list, i, tj);
		if (i == 0)
			retval = 1;	/* reschedule timer */
	} else {
		list_enqueue(&timejob_list, tj);
		if (list_count(&timejob_list) == 1)
			retval = 1;	/* reschedule timer */
	}
#if 0
	printf("registered:\n");
	list_iter_set(&li, &timejob_list);
	while ((p = list_iter_get(&li))) {
		printf("%x - %d\n", p, p->j_arg_sec);
	};
	list_iter_end(&li);
#endif
	return retval;
}
Пример #9
0
int main(int argc,char **argv) {
	char *zero, *one, *two, *three, *minusone;
	zero = strdup("Zero");
	one  = strdup("One");
	two  = strdup("Two");
	three = strdup("Three");
	minusone = strdup("-1");
	List *list = list_append(NULL,zero);
	list_append(list,one);
	list_append(list,two);
	list_append(list,three);
	list_remove_at(list,1);
	one = strdup("One");
	list_insert_at(list,1,one);
	list = list_prepend(list,minusone);
	int i;
	for(i = 0;i <= 2; i++) {
		printf("%s\n",(char *)list_get_nth(list,i)->data);
	}
	printf("\n%d\n",list_index_of(list,two));
	return 0;
}
Пример #10
0
///
/// Checks to see if the current output cache
/// has a match against a specified match definition.
///
bool ppimpl_match_test(state_t* state, match_t* match)
{
    size_t i;
    int32_t j;

    // Check if the output cache is even long enough to hold
    // the match.
    if (list_size(&state->cached_output) < (size_t)blength(match->text.ref))
        return false;

    // Check each character until we find one that doesn't
    // match, going backwards through the list (remember that
    // we're matching against the end of the list).
    for (i = 0; i < (size_t)blength(match->text.ref); i++)
    {
        if (!match->case_insensitive)
        {
            if ((char)list_get_at(&state->cached_output, list_size(&state->cached_output) - i - 1) !=
                    (char)match->text.ref->data[blength(match->text.ref) - i - 1])
                return false;
        }
        else
        {
            if (tolower((char)list_get_at(&state->cached_output, list_size(&state->cached_output) - i - 1)) !=
                    tolower((char)match->text.ref->data[blength(match->text.ref) - i - 1]))
                return false;
        }
    }

    // We have a match, ensure that it is at the start of the line
    // if required.
    if (match->line_start_only)
    {
        // We have to make sure that we only have spaces or tabs between
        // the first character of the text and a \n or the start of the output.
        j = (int32_t)(list_size(&state->cached_output) - (size_t)blength(match->text.ref) - 1);
        while (j >= 0)
        {
            switch ((char)list_get_at(&state->cached_output, j))
            {
                case ' ':
                case '\t':
                    // Permitted; continue.
                    break;
                case '\n':
                    // This means that it is at the start of a line.
                    return true;
                default:
                    // Anything else means that it's not at the start.
                    return false;
            }
            j--;
        }

        // If we reach here, check if i == 0, which means that we've
        // matched the start of the input (which also counts as a match).
        if (j <= 0)
            return true;
        else
            assert(false); // Something is not right.
    }

    // We have a match, ensure it is a proper identifier.
    if (match->identifier_only)
    {
        // We have to get the surrounding characters.
        bool has_next = ppimpl_has_input(state);
        bool has_previous = (list_size(&state->cached_output) >= (size_t)blength(match->text.ref) + 1);
        char next;
        char previous;
        bool is_identifier = false;
        if (has_next)
        {
            next = ppimpl_get_input(state);
            list_insert_at(&state->cached_input, (void*)next, 0);
        }
        if (has_previous)
            previous = (char)list_get_at(&state->cached_output, list_size(&state->cached_output) - blength(match->text.ref) - 1);

        // Check to see if the preceeding character (before the
        // match) and the proceeding character (after the match)
        // both isolate this match as an identifier.
        if (has_next && has_previous)
            is_identifier = ppimpl_isolates(next, false) && ppimpl_isolates(previous, true);
        else if (has_next)
            is_identifier = ppimpl_isolates(next, false);
        else if (has_previous)
            is_identifier = ppimpl_isolates(previous, true);
        else
            is_identifier = true;

        return is_identifier;
    }

    // We have a match that can be matched anywhere.
    return true;
}
Пример #11
0
int list_prepend(list_t *l, const void *data) {
    return list_insert_at(l, data, 0);
}
Пример #12
0
int list_append(list_t *l, const void *data) {
    return list_insert_at(l, data, l->numels);
}
Пример #13
0
int main()
{
    List *l = list_new();
    char choice = '\0';
    int pos, data;

    while (choice!='x') {
        printf("\nLinked List\n--------------------------------");
        printf("\n\n\nWhat would you like to do?\n");
        printf("\n  1. Insert at First Position");
        printf("\n  2. Insert at Last Position");
        printf("\n  3. Insert at nth Position");
        printf("\n  4. Delete First Element");
        printf("\n  5. Delete Last Element");
        printf("\n  6. Delete from nth Position");
        printf("\n  7. Print the List");
        printf("\n  x. Exit");
        printf("\n\nEnter Choice: ");
        fflush(stdin);
        scanf("%c", &choice);
        printf("-----\n");
        switch (choice) {
        case 'x':
            break;
        case '1':
            printf("\nEnter data: ");
            scanf("%d", &data);
            list_push_front(l, data);
            print_list(l);
            break;
        case '2':
            printf("\nEnter data: ");
            scanf("%d", &data);
            list_push_back(l, data);
            print_list(l);
            break;
        case '3':
            printf("\nEnter data: ");
            scanf("%d", &data);
            printf("Enter position: ");
            scanf("%d", &pos);
            list_insert_at(l, pos, data);
            print_list(l);
            break;
        case '4':
            list_pop_front(l);
            print_list(l);
            break;
        case '5':
            list_pop_back(l);
            print_list(l);
            break;
        case '6':
            printf("Enter position: ");
            scanf("%d", &pos);
            list_delete_at(l, pos);
            print_list(l);
            break;
        case '7':
            print_list(l);
            break;
        }

    }
    list_free(l);
}
Пример #14
0
/* implementation of Test B */
void testb_check(size_t const n) {
  list_t lists[NUM_OF_LISTS];
  int *added_value;
  int values[NUM_OF_LISTS][n];
  int ret, i, j;
  int value;

  // --------------
  // INITIALIZATION
  // of the lists and check for correctness
  // --------------
  for(i=0; i<NUM_OF_LISTS; ++i) {
    ret = list_init(&lists[i]);
    assert(ret == 0 && "list initialization fails");
  }

  // for achieving the insertion of new elements creating new
  // memory areas, instead of calling malloc(), we set the
  // list_attribute_copy and check the correctness.
  for(i=0; i<NUM_OF_LISTS; ++i) {
    ret = list_attributes_copy(&lists[i], int_size, 1);
    assert(ret == 0 && "setting attribute copy fails");
  }

  // ---------
  // INSERTION
  // both successful and unsuccessful
  // ---------
  for(i=0; i<NUM_OF_LISTS; ++i) {
    // successful insertion in empty list
    randomi(&value);
    ret = list_insert_at(&lists[i], &value, 0);
    assert(ret > 0 && "element insertion fails");
    assert(list_size(&lists[i]) == 1 && "list size is wrong");
    // checking the inserted value
    added_value = (int *)list_get_at(&lists[i], 0);
    assert(added_value != NULL && "retrieving element fails");
    assert(value == *added_value && "retrieved value has wrong value");
    // successful deletion 
    ret = list_delete_at(&lists[i], 0);
    assert(ret == 0 && "delete element fails");
    assert(list_size(&lists[i]) == 0 && "list size is wrong");
  }

  // Failure of insertion on multiple elements.
  // The insertion is done at indexes that are not
  // reachable for the list because it's empty
  for(i=0; i<NUM_OF_LISTS; ++i) {
    randomi(&value);
    for(j=1; j<4; ++j) {
      ret = list_insert_at(&lists[i], &value, j);
      assert(ret < 0 && "element insertion failure fails");
      assert(list_size(&lists[i]) == 0 && "list size is wrong");

      ret = list_delete_at(&lists[i], j);
      assert(ret < 0 && "element deletion failure fails");
      assert(list_size(&lists[i]) == 0 && "list size is wrong");
    }
  }

  // ---------
  // PREAPPEND
  // ---------

  // append the second half of the lists
  for(i=0; i<NUM_OF_LISTS; ++i) {
    for(j=0; j<(n/2); ++j) {
      randomi(&value);
      ret = list_prepend(&lists[i], &value);
      assert(ret == 1 && "element prepend fails");
      // checking also the value appended
      added_value = (int *)list_get_at(&lists[i], 0);
      assert(added_value != NULL && "retrieving element fails");
      assert(value == *added_value && "retrieved value has wrong value");
      // store the max for each list
      values[i][(n/2-1)-j] = value;
    }

    assert(list_size(&lists[i]) == n/2 && "list size is wrong");
  }

  // ------
  // APPEND
  // ------

  // append the first half of the lists
  for(i=0; i<NUM_OF_LISTS; ++i) {
    for(j=(n/2); j<n; ++j) {
      randomi(&value);
      ret = list_append(&lists[i], &value);
      assert(ret == 1 && "element append fails");
      // checking also the value appended
      added_value = (int *)list_get_at(&lists[i], j);
      assert(added_value != NULL && "retrieving element fails");
      assert(value == *added_value && "retrieved value has wrong value");
      // store the value in the matrix
      values[i][j] = value;
    }

    assert(list_size(&lists[i]) == n && "list size is wrong");
  }

  // check the values inserted in the lists
  for(i=0; i<NUM_OF_LISTS; ++i) {
    for(j=0; j<n; ++j) {
      assert(values[i][j] == *(int *)list_get_at(&lists[i], j)
             && "retrieved value has wrong value");
    }
  }

  // -----
  // CLEAR
  // -----

  // check the correctness of the clear function execution
  // and check also the length of the cleared list
  for(i=0; i<NUM_OF_LISTS; ++i) {
      unsigned int isize;
    isize = list_size(&lists[i]);

    ret = list_clear(&lists[i]);
    assert(ret == (int)isize && "clearing list fails");

    ret = list_size(&lists[i]);
    assert(ret == 0 && "list size is wrong");
  }

  // -------
  // DESTROY
  // -------

  // destroy both lists
  for(i=0; i<NUM_OF_LISTS; ++i)
    list_destroy(&lists[i]);
}