int main()
{
	list_t l = list_create(5);
	if (!l)
	{
		printf("list create error.\n");
		return -1;
	}
	assert(1 == list_is_empty(l));
	assert(0 == list_insert(l, (position_t)l, 333));
	position_t p = list_first(l);
	assert(p->elm == 333);
	p = list_find(l, 333);
	assert(0 == list_insert(l, p, 444));
	assert(p->elm == 333);
	p = list_find_previous(l, 444);
	assert(p->elm == 333);
	assert(p->next->elm == 444);
	p = list_find(l, 444);
	assert(p->elm == 444);
	list_make_empty(l);
	assert(NULL == list_find(l, 444));
	list_destroy(l);
	return 0;
}
int main()
{
	list_t l = list_create(0);
	assert(l);
	assert(list_is_empty(l));
	list_elm_add(l, l, 1);
	position_t p = list_first_elm(l);
	assert(list_elm_retrieve(p)==1);
	list_elm_add(l, l, 2);
	p = list_find(l, 1);
	assert(p);
	assert(list_elm_retrieve(p)==1);
	p = list_first_elm(l);
	assert(list_elm_retrieve(p)==2);
	list_elm_del(l, 2);
	p = list_first_elm(l);
	assert(list_elm_retrieve(p)==1);
	list_make_empty(l);
	assert(1 == list_is_empty(l));

	return;
}
Exemplo n.º 3
0
int main()
{
	char buf[256];
	char *cmd = NULL, *args = NULL;
	List list;
	Node *current = NULL;	
	list_make_empty(&list);
	while(1)
	{
		printf("> ");
		fgets(buf, sizeof(buf), stdin);
		cmd = strtok(buf, " \n");
		args = strtok(NULL, "\n");
		if (!cmd)
			continue;
		if (!strcmp(cmd, "quit"))
			break;
		else if (!strcmp(cmd, "clear"))
			list_clear(&list);
		else if (!strcmp(cmd, "help"))
			printf("%s\n", help_str);
		else if (!strcmp(cmd, "size"))
			printf("Size: %d\n", list_get_size(&list));	
		else if (!strcmp(cmd, "push_back"))
			push(args, &list, pd_back);
		else if (!strcmp(cmd, "current"))
			print_value(current);
		else if (!strcmp(cmd, "jump_first"))
			current = list.first;
		else if (!strcmp(cmd, "jump_last"))
			current = list.last;
		else if (!strcmp(cmd, "jump_next")) {
			if (current)		
				if (current->next)
					current = current->next;
		}
		else if (!strcmp(cmd, "jump_prev")) {
			if (current)
				if (current->prev)
					current = current->prev;
		}
		else if (!strcmp(cmd, "print"))
			list_print(&list);
		else if (!strcmp(cmd, "remove")) {
			list_remove(&list, current);
			current = NULL;
		}
		else if (!strcmp(cmd, "insert_after"))
			insert(args, &list, current, id_after);
		else if (!strcmp(cmd, "insert_before"))
			insert(args, &list, current, id_before);
		else if (!strcmp(cmd, "pop_back"))
			pop(args, &list, &current, pd_back);
		else if (!strcmp(cmd, "pop_front"))
			pop(args, &list, &current, pd_front);
		else if (!strcmp(cmd, "push_front"))
			push(args, &list, pd_front);
		else if (!strcmp(cmd, "front"))
			print_value(list.first);
		else if (!strcmp(cmd, "back"))
			print_value(list.last);
		else
			printf("Command \"%s\" not found\n", cmd);
	}
	list_clear(&list);
	return 0;
}
void list_destroy(list_t l)
{
	if (!l) return;
	list_make_empty(l);
	free(l);
}