Exemplo n.º 1
0
int main (void) 
{
	Tlist list;
	initialize_list(&list);
	insert_to_list_end(&list, 'a');
	insert_to_list_end(&list, 'b');
	insert_to_list_end(&list, 'c');
	insert_to_list_end(&list, 'd');
	print_list(&list);
	delete_first(&list);
	print_list(&list);		
	delete_first(&list);
	print_list(&list);		
	delete_first(&list);
	print_list(&list);		          
	delete_first(&list);
	print_list(&list);	        //tulostuu tyhjä lista
	insert_to_list_end(&list, 'e');
	print_list(&list);		

	#if defined(WIN32)||defined(_WIN32)
	system("pause");
	return 0;
	#else
	return 0;
	#endif
}
Exemplo n.º 2
0
	int
main(int argc, char *argv[])
{
	int i;

	struct node *h = NULL;
	for(i = 0; i < 10; i++)
		h = insert_last(h, i);

	print(h);

	h = reverse(h, NULL);
	print(h);

	h = reverse2(h);
	print(h);

	h = delete_first(h);
	print(h);

	h = delete_last(h);
	print(h);

	h = insert_first(h, 11);
	print(h);

	h = insert_last(h, 13);
	print(h);

	h = insert_after(h, newnode(14), 13);
	print(h);

	return 0;
}
Exemplo n.º 3
0
// Consolidate a queue in amortized O(log n)
static 
void consolidate_queue(priority_queue* q) {
  unsigned int length = q->length;
  
  unsigned int array_size = 2 * log(length) / log(2) + 1;
  priority_node* tree_by_degree[array_size]; // TODO: We need only 2log n entries
  unsigned int i;
  for (i=0; i<array_size; i++)
    tree_by_degree[i] = NULL;

  priority_node* n = NULL;
  while (((n = delete_first(q)))) {
    priority_node* n1 = NULL;
    while (((n1 = tree_by_degree[n->degree]))) {
      tree_by_degree[n->degree] = NULL;
      n = link_nodes(q, n, n1);
    }
    tree_by_degree[n->degree] = n;
  }

  // Find minimum value in O(log n) // Only if we shorten the array
  q->rootlist = NULL;
  q->min = NULL;
  for (i=0; i<array_size; i++) {    
    if (tree_by_degree[i] != NULL) {
      meld_queue(q, tree_by_degree[i], 0);
    }
  }

  q->length = length;
}
Exemplo n.º 4
0
int main (void) {
Tlist list;
int order_no;
char to_be_searched;
initialize_list(&list);
insert_to_list_end(&list, 'x');
insert_to_list_end(&list, 'a');
insert_to_list_end(&list, 'b');
insert_to_list_end(&list, 'c');
insert_to_list_end(&list, 'd');
insert_to_list_end(&list, 'y');
print_list(list);
printf("Enter first character to be searced\n");
fflush(stdin); scanf("%c", &to_be_searched);
if (find_pos_in_list(&list,to_be_searched, &order_no))

printf("The order no is %d\n", order_no);
else
printf("Not found\n");
printf("Enter second character to be searced\n");
fflush(stdin); scanf("%c", &to_be_searched);
if (find_pos_in_list(&list, to_be_searched, &order_no))
printf("The order no is %d\n", order_no);
else
printf("Not found\n");
delete_first(&list);
print_list(list);
delete_last(&list);
print_list(list);

system("PAUSE");
}
Exemplo n.º 5
0
int main()
{
    Sant* santi=(Sant*)malloc(sizeof(Sant));
    initializeSent(santi);
    //santi->head=NULL;
    // santi->tail=NULL;
    char s[30];
    int x;
    out=fopen("out.txt","w");
    in=fopen("in.txt","r");
    int c;
    c=fscanf(in,"%s %d",s,&x);
    while(c>0)
    {
        //printf("%s %d\n",s,x);
        if(!strcmp(s,"AL"))addLast(santi,x);
        if(!strcmp(s,"AF"))addFirst(santi,x);
        if(!strcmp(s,"DE"))delete_element(santi,x);
        if(!strcmp(s,"PRINT_F"))print_first(santi,x);
        if(!strcmp(s,"PRINT_L"))print_last(santi,x);
        if(!strcmp(s,"PRINT_ALL"))printlist(santi);
        if(!strcmp(s,"DOOM_THE_LIST"))doom_the_list(santi);
        if(!strcmp(s,"DF"))delete_first(santi);
        if(!strcmp(s,"DL"))delete_last(santi);
        strcpy(s,"");
        c=fscanf(in,"%s %d",s,&x);

    }
    return 0;
}
Exemplo n.º 6
0
void main() {

    start =NULL;
    int choice,index,count,element,position,num_choice;
    do {

        printf("Enter your choice\n1.Insert at beginning\n2.Insert at end\n3.Delete from beginning\n");
        printf("\n4.Delete end\n5.Delete a given element\n6.Display");
        scanf("%d",&choice);

        switch(choice) {

            case 1:printf("Enter the element to be inserted: ");
                   scanf("%d",&element);
                   insert_begin(element);
                   break;
            case 2:printf("Enter the element to be inserted: ");
                   scanf("%d",&element);
                   insert_end(element);
                   break;
            case 3:delete_first();
                   break;
            case 4:delete_end();
                   break;
            case 5:printf("Enter the element to be deleted");
                   scanf("%d",&element);
                   delete_element(element);
                   break;
            case 6:display();
                   break;

        }
        printf("Do you want to continue 1.yes,2.no");
        scanf("%d",&num_choice);
    }while(num_choice==1);
}
Exemplo n.º 7
0
void analize_tree(TREEPTR root, short condition, short reg, int level)
{
    char    neigh[10],path[10];
    PATHPTR path_list=NULL,
	    neigh_list=NULL,
	    breath_h=NULL,
	    breath_q=NULL;
    short   length, merged, end;
    TREEPTR node,neigh_node;
    MERGEPTR node1, node2;


    initialize_label(path,0);
    node=root;

    end=False;
    while (!end) {
	node=get_leaf(path);

	/*Compute merge operations*/
	if (node->region!=NULL) {    /*A leaf has been found*/
	    /*Merge its neighbours if condition is satisfied*/
	    if (node->included!=yes) {
		/*Add a new element to the list of merged regions*/
		add_new_merged_region(&merged_region_list);
		/*Insert the region in the list of merged regions*/
		insert_leaf_list(&merged_region_list, node);
		node->included=yes;
		/*Actualize the pointer to the merged region which includes it*/
		node->pmerge = merged_region_list;
	    }
	    /* Obtain the list of neighbours of the node referenced by path */
	    get_neighbours(&neigh_list,root,path);
	    while (neigh_list!=NULL) {
		initialize_label(neigh,0);
		/* Get and delete the first element in the list of neighbours */
		delete_first(&neigh_list,neigh);
		neigh_node=get_leaf(neigh);
		/* Get the pointers to the candidate regions */
		node1=*get_location(node);
		node2=*get_location(neigh_node);
		if ( node1!=node2 ) {
		    /* The neighbour region doesn't belong to a merged region yet*/
		    if (neigh_node->included!=yes) {
			/* Check whether both regions are homogeneous or not */ 
			if (similar(path,neigh,condition)) {
			    /* Insert the neighbour in the same list than path */
			    /* All the regions that are homogeneous belong to */
			    /* the same list */ 
			    insert_leaf_list(get_location(node),neigh_node);
			    neigh_node->included=yes;
			    /* Set pmerge field to point to the list */
			    /* where it has been inserted */
			    neigh_node->pmerge=*get_location(node);
			}
		    } else {  /* Each region belongs to a different merged region */
			/* Check whether both regions are homogeneous or not */ 
			if (similar(path,neigh,condition)) {
			    /* Check whether both merged regions are homogeneous */
			    if (abs( node1->mean - node2->mean ) < condition)
				/* Merge both lists of merged regions */
				merge_lists(&node1,&node2);
			}
		    }
		}
	    }
	} else {
	    length = strlen(path);
	    /* Insert new nodes to be analised using the breath first method */
	    path[length]='0'; insert_path(&breath_h,&breath_q,path);
	    path[length]='1'; insert_path(&breath_h,&breath_q,path);
	    path[length]='2'; insert_path(&breath_h,&breath_q,path);
	    path[length]='3'; insert_path(&breath_h,&breath_q,path);
	}
	/* Take a node from the list to analize it if there are nodes left */
	if (breath_h==NULL) end=True;
	    else delete_first(&breath_h,path);
    }
}
Exemplo n.º 8
0
void delete_list(list *l)
{
    while(l) l = delete_first(l);
}