Ejemplo n.º 1
0
/*
void print_int(void *value);
void print_int(void *value)
{
    printf("%d ",*(int *)value);
 }
*/
int main(int argc, char **argv)
{
    int i = 0;
    int a[]={1,2,3,4,5};
    void *value;

    Dlist *dlist = NULL;
    dlist = init_dlist();
    printf("3.push_front(dlist, &a[i]);\n");
    for(i=0; i< sizeof(a)/sizeof(a[0]);++i)
    {
        push_front(dlist, &a[i]);
    }
    show_dlist(dlist, print_int);
    printf("5.pop_front(dlist);\n");
    pop_front(dlist);

    show_dlist(dlist, print_int);
    printf("4.push_back(dlist, &a[i]);\n");
    for(i=0; i< sizeof(a)/sizeof(a[0]);++i)
    {
        push_back(dlist, &a[i]);
    }
    show_dlist(dlist, print_int);
    printf("6.pop_back(dlist);\n");
    pop_back(dlist);
    show_dlist(dlist, print_int);
    
    printf("7.insert_prev(dlist, dlist->head->next->next, &a[4]);\n");
    insert_prev(dlist, dlist->head->next->next, &a[4]);
    show_dlist(dlist, print_int);

    printf("8.insert_next(dlist, dlist->head->next->next, &a[4]);\n");
    insert_next(dlist, dlist->head->next->next, &a[4]);
    show_dlist(dlist, print_int);

    printf("9.remove_dlist_node(dlist, dlist->head->next->next->next);\n");
    remove_dlist_node(dlist, dlist->head->next->next->next);
    show_dlist(dlist, print_int);
   
    get_front(dlist, &value);
    printf("\n11.get_front:\n");
    print_int(value);

    get_tail(dlist, &value);
    printf("\n12.get_tail:\n");
    print_int(value);

    printf("\n13.get_dlist_count:\n");
    printf("%d \n",get_dlist_count(dlist));
    destroy_dlist(&dlist);
    return 0;
}
Ejemplo n.º 2
0
Archivo: list.cpp Proyecto: SamJia/Oj
void list<T>::merge_sort(Node *left_begin, Node *right_end, int size, bool (*cmp)(const T &data1, const T &data2)){
	if(size <= 1)
		return;
	int left_size = size / 2, right_size = size - size / 2;
	Node *left_end = left_begin->next_;
	for(int i = 0; i < left_size; ++i)
		left_end = left_end->next_;
	merge_sort(left_begin, left_end, left_size, cmp);
	Node *right_begin = left_end->prev_;
	merge_sort(right_begin, right_end, right_size, cmp);
	left_end = right_begin->next_;
	Node *left_p = left_begin->next_, *right_p = right_begin->next_;
	for(; right_p != right_end; left_end = right_p = right_begin->next_){
		for(; !(left_p == left_end || cmp(right_p->data_, left_p->data_)); left_p = left_p->next_);
		if(left_p == left_end)
			return;
		insert_prev(left_p, right_p);
	}
}
Ejemplo n.º 3
0
void init_menu_insert(AList_WS* list) {
	for (int i = 0; i < 3; ++i)
		printf("%s\n", MAIN_MENU[i]);

	printf("\ta. Insert at Head\n");
	printf("\tb. Insert at Tail\n");
	printf("\tc. Insert at position Next to an index\n");
	printf("\td. Insert at position Prev to an index\n");
	printf("\te. Go Back\n");

	for (int i = 3; i < 8; ++i)
		printf("%s\n", MAIN_MENU[i]);

	printf("\vPLease enter option: ");

	char* input = (char*)(malloc(100));
	memset(input, '-', 100);
	scanf("%s", input);
	char ch = input[0];

	if (isdigit(input[0]) && isdigit(input[1]))
		ch = 'X';

	switch (ch) {

	case '1':
	case 'a':
	case 'H':
	{
		printf("Please enter the value you want to insert: ");
		double data;
		scanf("%lf", &data);
		insert_head(list, data);
		break;
	}

	case '2':
	case 'b':
	case 'T':
	{
		printf("Please enter the value you want to insert: ");
		double data;
		scanf("%lf", &data);
		insert_tail(list, data);
		break;
	}

	case '3':
	case 'c':
	case 'N':
	{
		printf("Please enter the value you want to insert: ");
		double data;
		scanf("%lf", &data);
		printf("Please enter the position");
		size_t pos;
		scanf("%lu", pos);
		insert_next(list,pos,data);
		break;
	}

	case '4':
	case 'd':
	case 'P':
	{
		printf("Please enter the value you want to insert: ");
		double data;
		scanf("%lf", &data);
		printf("Please enter the position");
		size_t pos;
		scanf("%lu", pos);
		insert_prev(list,pos,data);
		break;
	}

	case '5':
	case 'e':
	case 'B':
		break;

	default:
		printf("Please enter a valid choice.\n\n");
		init_menu_insert(list);
	}
}