Exemplo n.º 1
0
bool list_sort(node **head)
{
	node *right,*left;
	int leng;
	int i,j;
	int tmp;
	leng = list_get_length(*head);
	if(*head == NULL)
	{
		return false;
	}
	else
	{
		for(i = 0;i < leng - 1;i++)
		{
			//回到表头
			right = (*head) -> next;
			left = *head;
			for(j = 0;j < leng - (1 + i);j++)
			{
				//开始排序
				if((left -> num) > (right -> num))
				{
					tmp = left -> num;
					left -> num = right -> num;
					right -> num = tmp;
				}
				right = right -> next;
				left = left -> next;
			}
		}
	return true;	
	}
}
Exemplo n.º 2
0
bool list_modify_num(node **head,int idx,int n)
{
	int leng;
	int *data;
	leng = list_get_length(*head);
	if(leng < idx||idx < 1)
	{
		return false;
	}
	else
	{
		data = list_get_num_of_idx(*head,idx);
		*data = n;
		return true;	
	}
}
Exemplo n.º 3
0
int* list_get_num_of_idx(node *head,int idx)
{
	int leng,*number;
	leng = list_get_length(head);

	if(leng < idx || idx < 1)
	{
		return 0;
	}
	else
	{
		while(idx != 1)
		{
			idx--;
			head = head -> next;	
		}
		number =&(head -> num);
		return number;	
	}
}
Exemplo n.º 4
0
extern int addrlist_get_length(t_addrlist const * addrlist)
{
    return list_get_length(addrlist);
}
Exemplo n.º 5
0
int main(int argc,char *argv)
{
	node *list;
	bool info;
	int length,*num,pos;
	int c;
	list = NULL;

	printf("请输入:\n");

	//初始化与创建
	list = list_init(list);     
	list_create(&list);

	//打印链表 Y
//	list_print(list);

	//得到长度 Y
	length = list_get_length(list);
	printf("链表长度是%d\n",length);

//	printf("请输入下标:");
//	scanf("%d",&pos);

	//得到指定下标的数字 Y
//	num = list_get_num_of_idx(list,pos);
//	printf("第%d位置的数字是%d\n",pos,*num);
//	setbuf(stdin,NULL);

//	printf("请输入数字:");
//	scanf("%d",&num);

	//得到指定数字出现的首个下标 Y
//	pos = list_get_idx_of_num(list,num);
//	printf("数字【%d】首次出现在第%d位置\n",num,pos);
	
	//排序链表
	//从小到大 Y
	/*
	info = list_sort(&list);
	
	if(info == true)
	{
		print(list);
	}
	else
	{
		printf("排序失败 !\n");

	}
	*/
	//修改数值 Y
	//list_modify_num(&list,1,25);
	//list_print(list);
	
	
	// 在表尾添加数据 Y
	/*
	info = list_add_end(&list,5712);
	if(info == true)
		printf("yes\n");
	else
		printf("no\n");
	length = list_get_length(list);
	printf("链表长度是%d\n",length);
        list_print(list);	
	*/

	//在表头添加数据 Y
	/*
	info = list_add_head(&list,1256);
	if(info == true)
		puts("yes");
	else
		puts("no");
	length = list_get_length(list);
	printf("链表长度是:%d\n",length);
	list_print(list);
	*/
	//依据索引添加数据 Y
	/*
	list_add_idx(&list,-1,123);
	list_print(list);
	list_add_idx(&list,0,456);
	list_print(list);
	list_add_idx(&list,1,789);
	list_print(list);
	list_add_idx(&list,2,159);
	list_print(list);
	list_add_idx(&list,4,654);
	list_print(list);
	list_add_idx(&list,123,753);
	list_print(list);
	list_add_idx(&list,6,999);
	list_print(list);
	*/
	//清空最后节点 Y
	/*	
	list_del_end(&list);
	list_print(list);
	length = list_get_length(list);
	printf("链表长度为:%d\n",length);
	*/
	//清空开头节点 Y
	/*
	list_del_head(&list);
	list_print(list);
	length = list_get_length(list);
	printf("链表长度为:%d\n",length);
	*/
	
	//依据索引删除数据 Y
	/*
	list_del_idx(&list,1);
	list_print(list);
	length = list_get_length(list);
	printf("链表长度为:%d\n",length);
	*/
	
	//删除第一个出现的指定数字,如需全部删除,可以多次调用 Y
	/*
	list_del_num(&list,5);
	list_print(list);
	list_del_num(&list,1);
	list_print(list);
	list_del_num(&list,6);
	list_print(list);
	info = list_del_num(&list,111111);	
	if(info == false)
		puts("NO!");
	length = list_get_length(list);
	printf("链表长度为:%d\n",length);
	*/

	//多次调用list_del_num Y
	/*
	info = true;
	while(info == true)
	{
		info=list_del_num(&list,2);
		list_print(list);
	}
	*/
	//清空,结束 Y
	info = list_clear(&list);
	if(info == true)
	{
		printf("链表清除成功!!\n");
	}
	else
	{
		printf("链表为空!\n");
	}
	length = list_get_length(list);
	printf("链表长度是%d\n",length);

	setbuf(stdin,NULL);
	return 0;
}
Exemplo n.º 6
0
int32_t main(void){
	struct list 		list;
	uint32_t 			i;
	struct listIterator it;

	list_init(list, sizeof(uint32_t));

	for (i = 0; i < NB_ELEMENT_LIST; i++){
		if (list_add_tail(&list, &i) == NULL){
			log_err("unable to add element to list");
			return EXIT_FAILURE;
		}
	}

	if (list_get_length(&list) != NB_ELEMENT_LIST){
		log_err_m("incorrect list size: %u", list_get_length(&list));
		return EXIT_FAILURE;
	}

	for (listIterator_init(&it, &list); listIterator_get_next(&it) != NULL; ){
		if (it.index != *(uint32_t*)listIterator_get_data(it)){
			log_err_m("iterator error @ %u", it.index);
			return EXIT_FAILURE;
		}
	}
	for (listIterator_init(&it, &list); listIterator_get_prev(&it) != NULL; ){
		if (it.index != *(uint32_t*)listIterator_get_data(it)){
			log_err_m("iterator error @ %u", it.index);
			return EXIT_FAILURE;
		}
	}

	if (listIterator_get_index(&it, &list, MODIFY_INDEX) != NULL){
		if (MODIFY_INDEX != *(uint32_t*)listIterator_get_data(it)){
			log_err_m("iterator error after get index: %u", *(uint32_t*)listIterator_get_data(it));
			return EXIT_FAILURE;
		}
		listIterator_pop_next(&it);
	}
	else{
		log_err_m("unable to get index %u", MODIFY_INDEX);
		return EXIT_FAILURE;
	}

	for (listIterator_init(&it, &list); listIterator_get_next(&it) != NULL; ){
		if (it.index == MODIFY_INDEX){
			listIterator_push_prev(&it, &(it.index));
			break;
		}
	}

	for (listIterator_init(&it, &list); listIterator_get_next(&it) != NULL; ){
		if (it.index != *(uint32_t*)listIterator_get_data(it)){
			log_err_m("iterator error @ %u", it.index);
			return EXIT_FAILURE;
		}
	}

	list_clean(&list);

	return EXIT_SUCCESS;
}