Example #1
0
static void print_list(const List *list) {

ListElmt           *element;

int                *data,
                   i;

/*****************************************************************************
*                                                                            *
*  Display the linked list.                                                  *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "List size is %d\n", list_size(list));

i = 0;
element = list_head(list);

while (1) {

   data = list_data(element);
   fprintf(stdout, "list[%03d]=%03d\n", i, *data);

   i++;

   if (list_is_tail(element))
      break;
   else
      element = list_next(element);

}

return;

}
Example #2
0
File: list-1.c Project: whatot/ma_c
static void print_list(const List *list) {
	ListElmt *element;
	int *data;
	int i;

	fprintf(stdout, "List size is %d\n", list_size(list));

	i = 0;
	element = list_head(list);
	while (1) {
		data = list_data(element);
		fprintf(stdout, "list[%03d]=%03d\n", i, *data);
		
		i++;

		if (list_is_tail(element)) {
			break;
		} else {
			element = list_next(element);
		}
	}
	return;
}
Example #3
0
void* search_in_list(List *list, char *hash){
   ListElmt           *element;
   void *res;
   struct dalvik_hook_t *dh;
   element = list_head(list);

   while (1) {

   dh = list_data(element);

   if(strncmp(element->hashvalue,hash,sizeof(hash)) == 0 ) {
      return dh;
   }

   if (list_is_tail(element))
      break;
   else
      element = list_next(element);

   }
   return;

}
Example #4
0
static void print_list_tsp(List *vertices) {

	TspVertex *vertex;

	ListElmt *element;

	int i;

	/*****************************************************************************
	 *                                                                            *
	 *  Display the list of vertices in a traveling-salesman problem.             *
	 *                                                                            *
	 *****************************************************************************/

	fprintf(stdout, "Vertices=%d\n", list_size(vertices));

	i = 0;
	element = list_head(vertices);

	while (1) {

		vertex = list_data(element);
		fprintf(stdout, "vertices[%03d]=%s: (%.1lf, %.1lf)\n", i,
				(char *) vertex->data, vertex->x, vertex->y);

		i++;

		if (list_is_tail(element))
			break;
		else
			element = list_next(element);

	}

	return;

}
Example #5
0
int main(int argc, char **argv) {

	List               list;
	ListElmt           *element;

	int                *data,
					   i;

	/*****************************************************************************
	*                                                                            *
	*  Initialize the linked list.                                               *
	*                                                                            *
	*****************************************************************************/

	list_init(&list, free);

	/*****************************************************************************
	*                                                                            *
	*  Perform some linked list operations.                                      *
	*                                                                            *
	*****************************************************************************/

	element = list_head(&list);

	for (i = 10; i > 0; i--) {

	   if ((data = (int *)malloc(sizeof(int))) == NULL)
		  return 1;

	   *data = i;

	   if (list_ins_next(&list, NULL, data) != 0)
		  return 1;

	}

	print_list(&list);

	element = list_head(&list);

	for (i = 0; i < 7; i++)
	   element = list_next(element);

	data = list_data(element);
	fprintf(stdout, "Removing an element after the one containing %03d\n", *data);

	if (list_rem_next(&list, element, (void **)&data) != 0)
	   return 1;

	print_list(&list);

	fprintf(stdout, "Inserting 011 at the tail of the list\n");

	*data = 11;
	if (list_ins_next(&list, list_tail(&list), data) != 0)
	   return 1;

	print_list(&list);

	fprintf(stdout, "Removing an element after the first element\n");

	element = list_head(&list);
	if (list_rem_next(&list, element, (void **)&data) != 0)
	   return 1;

	print_list(&list);

	fprintf(stdout, "Inserting 012 at the head of the list\n");

	*data = 12;
	if (list_ins_next(&list, NULL, data) != 0)
	   return 1;

	print_list(&list);

	fprintf(stdout, "Iterating and removing the fourth element\n");

	element = list_head(&list);
	element = list_next(element);
	element = list_next(element);

	if (list_rem_next(&list, element, (void **)&data) != 0)
	   return 1;

	print_list(&list);

	fprintf(stdout, "Inserting 013 after the first element\n");

	*data = 13;
	if (list_ins_next(&list, list_head(&list), data) != 0)
	   return 1;

	print_list(&list);

	i = list_is_head(&list, list_head(&list));
	fprintf(stdout, "Testing list_is_head...Value=%d (1=OK)\n", i);
	i = list_is_head(&list, list_tail(&list));
	fprintf(stdout, "Testing list_is_head...Value=%d (0=OK)\n", i);
	i = list_is_tail(list_tail(&list));
	fprintf(stdout, "Testing list_is_tail...Value=%d (1=OK)\n", i);
	i = list_is_tail(list_head(&list));
	fprintf(stdout, "Testing list_is_tail...Value=%d (0=OK)\n", i);

	/*****************************************************************************
	*                                                                            *
	*  Destroy the linked list.                                                  *
	*                                                                            *
	*****************************************************************************/

	fprintf(stdout, "Destroying the list\n");
	list_destroy(&list);

	return 0;

}
Example #6
0
File: list-1.c Project: whatot/ma_c
int main() {
	List list;
	ListElmt *element;

	int *data;
	int i;

	list_init(&list, free);
	element = list_head(&list);
	
	for (i = 10; i > 0; i--) {
		if ((data = (int *)malloc(sizeof(int))) == NULL) {
			return 1;
		}

		* data = i;

		if (list_ins_next(&list, NULL, data) != 0) {
			return 1;
		}
	}

	print_list(&list);

	element = list_head(&list);

	for (i = 0; i < 7; i++) {
		element = list_next(element);
	}

	data = list_data(element);
	fprintf(stdout, "Removing an element after the one containing %03d\n",
			*data);
	if (list_rem_next(&list, element, (void **)&data) != 0) {
		return 1;
	}

	print_list(&list);

	fprintf(stdout, "Inserting 011 at the tail of the list\n");
	*data = 11;
	if (list_ins_next(&list, list_tail(&list), data) != 0) {
		return 1;
	}

	print_list(&list);

	fprintf(stdout, "Removing an element after the first element\n");
	element = list_head(&list);
	if (list_rem_next(&list, element, (void **)&data) != 0) {
		return 1;
	}

	print_list(&list);

	fprintf(stdout, "Inserting 012 at the head of the list\n");
	*data = 12;
	if (list_ins_next(&list, NULL, data) != 0) {
		return 1;
	}

	print_list(&list);

	fprintf(stdout, "Iterating and removing the fourth element\n");
	element = list_head(&list);
	element = list_next(element);
	element = list_next(element);
	if(list_rem_next(&list, element, (void **)&data) != 0) {
		return 1;
	}

	print_list(&list);

	fprintf(stdout, "Inserting 013 after the first element\n");
	*data = 13;
	if (list_ins_next(&list, list_head(&list), data) != 0) {
		return 1;
	}

	print_list(&list);

	i = list_is_head(&list, list_head(&list));
	fprintf(stdout, "Testing list_is_head...Value=%d (1=OK)\n", i);
	i = list_is_head(&list, list_tail(&list));
	fprintf(stdout, "Testing list_is_head...Value=%d (1=OK)\n", i);
	i = list_is_tail(list_tail(&list));
	fprintf(stdout, "Testing list_is_head...Value=%d (1=OK)\n", i);
	i = list_is_tail(list_head(&list));
	fprintf(stdout, "Testing list_is_head...Value=%d (1=OK)\n", i);

	fprintf(stdout, "Destroying the list\n");
	list_destroy(&list);

	return 0;
}
Example #7
0
int test_linked_list()
{
	int i,j, result = 0;
	List_Node *test_node1 = NULL;
	List_Node *test_node2 = NULL;
	List_Node *test_node3 = NULL;
	List_Node *pTrack[5];
	void* pArr[6];

	List_Head *test_list1 = list_new();
	List_Head *test_list2 = list_new();

	test_msg_start("Test Linked List Creation");
		if(test_list1 == NULL) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List Empty Length - Using Variable");
		if(test_list1->count != 0) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List Empty Length - Using Length Function");
		if(list_len(test_list1) != 0 ) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Adding Node To Empty List");
		if(list_ins_tail(test_list1) == NULL) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Adding Node To Non-Empty List");
		if(list_ins_tail(test_list1) == NULL) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Clearing List With More Than One Node");
		list_clear(test_list1);
		if(test_list1->count != 0) result++;
		if(list_len(test_list1) != 0 ) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Clearing List With No Nodes");
		list_clear(test_list1);
		if(test_list1->count != 0) result++;
		if(list_len(test_list1) != 0 ) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List End - No Nodes");
		list_clear(test_list1);
		if(list_tail(test_list1) != NULL) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List End - One Node Only");
		list_clear(test_list1);
		test_node1 = list_ins_tail(test_list1);
		if(test_node1 == NULL) result++;
		if(list_tail(test_list1) != test_node1) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List End - More Than One Node");
		list_clear(test_list1);
		list_ins_tail(test_list1);
		test_node1 = list_ins_tail(test_list1);
		if(test_node1 == NULL) result++;
		if(list_tail(test_list1) != test_node1) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Removing Node From List With More Than One Node");
		list_clear(test_list1);
		test_node1 = list_ins_tail(test_list1);
		test_node2 = list_ins_head(test_list1);
		list_rm_node(test_list1, test_node1);
		if(test_list1->pNext != test_node2) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Removing Node From Empty List");
		list_clear(test_list1);
		test_node1 = (List_Node*)&test_list1; /* pointer points to known bad location */
		/* should not crash but return gracefully */
		if(list_rm_node(test_list1, test_node1) != -1) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Inserting Node After");
		list_clear(test_list1);
		test_node1 = NULL;
		test_node2 = NULL;
		test_node1 = list_ins_tail(test_list1);
		test_node2 = list_ins_after(test_list1, test_node1);

		/* test beginning */
		if(test_list1->pNext != test_node1) result++;
		/*...and end nodes. */
		if(list_tail(test_list1) != test_node2) result++;
		/* end node next should be null */
		if(list_tail(test_list1)->pNext != NULL) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Inserting Node Before");
		list_clear(test_list1);
		test_node1 = NULL;
		test_node2 = NULL;
		test_node1 = list_ins_head(test_list1);
		test_node2 = list_ins_before(test_list1, test_node1);

		/* test beginning */
		if(test_list1->pNext != test_node2) result++;
		/*...and end nodes. */
		if(list_tail(test_list1) != test_node1) result++;
		/* end node next should be null */
		if(list_tail(test_list1)->pNext != NULL) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Test Lengths");
		list_clear(test_list1);
		if(list_len(test_list1) != 0) result++;

		/* adding nodes using each function */
		list_ins_head(test_list1);
		if(list_len(test_list1) != 1) result++;

		list_ins_tail(test_list1);
		if(list_len(test_list1) != 2) result++;

		list_ins_before(test_list1, list_tail(test_list1));
		if(list_len(test_list1) != 3) result++;

		list_ins_after(test_list1, list_tail(test_list1));
		if(list_len(test_list1) != 4) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Previous Node Check");
		list_clear(test_list1);
		test_node1 = NULL;
		test_node2 = NULL;
		if(list_prev_node(test_list1, NULL) != NULL) result++;
		if(list_prev_node(NULL, test_list1->pNext) != NULL) result++;
		if(list_prev_node(NULL, test_node1 + 1000) != NULL) result++;
		test_node1 = list_ins_head(test_list1);
		if(list_prev_node(test_list1, test_list1->pNext) != NULL) result++;
		test_node2 = list_ins_head(test_list1);
		if(list_prev_node(test_list1, test_node1) != test_node2) result++;
		if(list_prev_node(test_list1, test_node2) != NULL) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Removing Nodes By Address");
		/* removing nodes from various places */
		list_clear(test_list1);
		test_node1 = NULL;
		test_node2 = NULL;

		if(list_rm_node(NULL, test_list1->pNext)!= -1) result++;
		if(list_rm_node(test_list1, NULL) != -1) result++;
		test_node1 = list_ins_head(test_list1);
		test_node2 = list_ins_head(test_list1);
		list_rm_node(test_list1, test_list1->pNext);
		if(list_len(test_list1) != 1) result++;
		test_node1 = list_tail(test_list1);
		list_rm_node(test_list1, test_node1);
		if(list_len(test_list1) != 0) result++;
		if(list_rm_node(test_list1, list_tail(test_list1)) != -1) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Removing Nodes After Address");
		list_clear(test_list1);
		test_node1 = NULL;
		test_node2 = NULL;
		test_node1 = list_ins_tail(test_list1);
		test_node2 = list_ins_tail(test_list1);
		if(list_rm_next(test_list1, test_node1) != 1) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Removing Nodes Before Address");
		list_clear(test_list1);
		test_node1 = NULL;
		test_node2 = NULL;
		if(list_rm_before(test_list1, NULL) != -1) result++;
		if(list_rm_before(NULL, NULL) != -1) result++;
		if(list_rm_before(test_list1 + 1000, NULL) != -1) result++;
		test_node1 = list_ins_tail(test_list1);
		test_node2 = list_ins_tail(test_list1);
		if(list_rm_before(test_list1, test_node2 + 1000) != -1) result++;
		if(list_rm_before(test_list1, test_node2) != 1) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Copying List");
		list_clear(test_list1);
		list_clear(test_list2);
		test_node1 = list_ins_tail(test_list1);
		test_node1->pData = &result;
		test_node2 = list_ins_tail(test_list1);
		if(list_copy(test_list2, test_list1) != 0) result++;
		if(test_list1->pNext == test_list2->pNext) result++;
		if(test_list1->count != 2) result++;
		if(test_list2->count != 2) result++;
		if(test_list1->pNext->pData != test_list2->pNext->pData) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Copying Empty List");
		list_clear(test_list1);
		list_clear(test_list2);
		if(list_copy(test_list2, test_list1) != 0) result++;
		if(test_list1->pNext != NULL) result++;
		if(test_list2->pNext != NULL) result++;
		if(test_list1->count != 0) result++;
		if(test_list2->count != 0) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Get Node Address By Node Number - First Node");
		list_clear(test_list1);
		test_node1 = list_ins_head(test_list1);
		test_node2 = list_ins_head(test_list1);
		test_node3 = list_get_num(test_list1, 1);
		if(test_node3 != test_node2) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Get Node Address By Node Number - Middle Node");
		list_clear(test_list1);
		list_ins_tail(test_list1);
		list_ins_tail(test_list1);
		list_ins_tail(test_list1);
		list_ins_tail(test_list1);
		test_node2 = list_ins_tail(test_list1);
		list_ins_tail(test_list1);
		list_ins_tail(test_list1);
		list_ins_tail(test_list1);
		list_ins_tail(test_list1);
		test_node3 = list_get_num(test_list1, 5);
		if(test_node3 != test_node2) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Get Node Address By Node Number - Last Node");
		list_clear(test_list1);
		test_node1 = list_ins_tail(test_list1);
		test_node2 = list_ins_tail(test_list1);
		test_node3 = list_get_num(test_list1, 2);
		if(test_node3 != test_node2) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Get Node Address By Node Number - Empty List");
		list_clear(test_list1);
		test_node1 = list_get_num(test_list1, 1);
		if(test_node1 != NULL) result++;
		test_node1 = list_get_num(test_list1, 55);
		if(test_node1 != NULL) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Swap Nodes In List");
		list_clear(test_list1);
		test_node1 = list_ins_tail(test_list1);
		test_node2 = list_ins_tail(test_list1);
		test_node3 = list_ins_tail(test_list1);
		pTrack[1] = test_node1->pNext;
		pTrack[2] = test_node2->pNext;
		if(list_node_swap(test_node1, test_node2) != 0) result++;
		if(test_node1->pNext != pTrack[2]) result++;
		if(test_node2->pNext != pTrack[1]) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - List Reverse - Pointer Tracking");
		list_clear(test_list1);
		for(i = 0; i < 5; i++) {
			pTrack[i] = list_ins_tail(test_list1);
			pTrack[i]->pData = &pTrack[i]->pData;
		}
		test_list1 = list_reverse(test_list1);
		for(i = 1, j = 4; i <= 4; i++, j--) {
			test_node1 = list_get_num(test_list1, i);
			if(test_node1->pData != pTrack[j]) result++;
		}
	test_msg_end(result);

	test_msg_start("Test Linked List - List Next Preprocessor");
		list_clear(test_list1);
		list_clear(test_list2);
		test_node1 = NULL;
		test_node2 = NULL;
		test_node1 = list_ins_head(test_list1);
		test_node2 = list_ins_head(test_list1);
		if(list_next(test_node1) == test_node2) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Default Payload");
		list_clear(test_list1);
		test_node1 = NULL;
		test_node2 = NULL;
		test_node1 = list_ins_tail(test_list1);
		test_node2 = list_ins_tail(test_list1);
		if(test_node1->pData != NULL) result++;

		/* make data point to something and test again... */
		test_node1->pData = test_node1;
		if(list_data(test_node1) != test_node1) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - List Is Head Preprocessor");
		if(list_is_head(test_list1, test_node2) != 0) result++;
		if(list_is_head(test_list1, test_node1) != 1) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - List Is Tail Preprocessor");
		if(list_is_tail(test_node2) != 1) result++;
		if(list_is_tail(test_node1) != 0) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - List Head Preprocessor");
		if(list_head(test_list1) != test_node1) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Append Lists - Pointer Tracking");
		list_clear(test_list1);
		list_clear(test_list2);
		list_ins_tail(test_list1);
		list_ins_tail(test_list1);
		list_ins_tail(test_list1);
		pTrack[0] = list_ins_tail(test_list1);
		pTrack[1] = list_ins_tail(test_list2);
		list_ins_tail(test_list2);
		list_ins_tail(test_list2);
		list_append(test_list1, test_list2);
		if(test_list1->count != 7) result++;
		if(!list_search(test_list1, pTrack[0])) result++;
		if(!list_search(test_list1, pTrack[1])) result++;
		if(pTrack[0] != list_get_num(test_list1, 4)) result++;
		if(pTrack[1] != list_get_num(test_list1, 5)) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Create Data Array - Pointer Tracking");
		list_clear(test_list1);
		pTrack[0] = list_ins_tail(test_list1);
		pTrack[0] = pTrack[0]->pData = &test_list1;
		pTrack[1] = list_ins_tail(test_list1);
		pTrack[1] = pTrack[1]->pData = &test_list2;
		pTrack[2] = list_ins_tail(test_list1);
		pTrack[2] = pTrack[2]->pData = &pTrack;
		pTrack[3] = list_ins_tail(test_list1);
		pTrack[3] = pTrack[3]->pData = &test_node1;
		pTrack[4] = list_ins_tail(test_list1);
		pTrack[4] = pTrack[4]->pData = &test_node2;
		pArr[5] = &test_list1;
		if(list_data_array(test_list1, pArr, 6) != 0) result++;
		if(pArr[0] != pTrack[0]) result++;
		if(pArr[1] != pTrack[1]) result++;
		if(pArr[2] != pTrack[2]) result++;
		if(pArr[3] != pTrack[3]) result++;
		if(pArr[4] != pTrack[4]) result++;
		if(pArr[5] != NULL) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Create Node Array - Pointer Tracking");
		list_clear(test_list1);
		pTrack[0] = list_ins_tail(test_list1);
		pTrack[1] = list_ins_tail(test_list1);
		pTrack[2] = list_ins_tail(test_list1);
		pTrack[3] = list_ins_tail(test_list1);
		pTrack[4] = list_ins_tail(test_list1);
		pArr[5] = &test_list1;
		if(list_node_array(test_list1, pArr, 6) != 0) result++;
		if(pArr[0] != pTrack[0]) result++;
		if(pArr[1] != pTrack[1]) result++;
		if(pArr[2] != pTrack[2]) result++;
		if(pArr[3] != pTrack[3]) result++;
		if(pArr[4] != pTrack[4]) result++;
		if(pArr[5] != NULL) result++;
	test_msg_end(result);

	test_msg_start("Test Linked List - Search List");
		list_clear(test_list1);
		if(list_search(test_list1, test_node1) != 0) result++;
		test_node1 = list_ins_head(test_list1);
		if(list_search(test_list1, test_node1) != 1) result++;
		if(list_search(test_list1, test_node2) != 0) result++;
		if(list_search(test_list1, test_node3) != 0) result++;
		if(list_search(test_list1, NULL) != 0) result++;
		if(list_search(test_list1, (List_Node *)test_list1) != 0) result++;
		test_node2 = list_ins_head(test_list1);
		if(list_search(test_list1, test_node1) != 1) result++;
		if(list_search(test_list1, test_node2) != 1) result++;
		if(list_search(test_list1, test_node3) != 0) result++;
		if(list_search(test_list1, NULL) != 0) result++;
		if(list_search(test_list1, (List_Node *)test_list1) != 0) result++;
		list_clear(test_list1);
		if(list_search(test_list1, test_node1) != 0) result++;
		if(list_search(test_list1, test_node2) != 0) result++;
		if(list_search(test_list1, test_node3) != 0) result++;
		if(list_search(test_list1, NULL) != 0) result++;
		if(list_search(test_list1, (List_Node *)test_list1) != 0) result++;
	test_msg_end(result);

	return result;
}