Exemplo n.º 1
0
Arquivo: tree.c Projeto: MarcoQin/DSAA
SearchTree make_empty(SearchTree T) {
    if (T != NULL) {
        make_empty(T->left);
        make_empty(T->right);
        free(T);
    }
    return NULL;
}
Exemplo n.º 2
0
Tree *make_empty(Tree *t) {
	if (t != NULL) {
		make_empty(t->left);
		make_empty(t->right);
		free(t);
	}
	return NULL;
}
Exemplo n.º 3
0
static void make_empty(T tree, Node * node)
{
    if (node == NIL(tree))
        return;
    make_empty(tree, node->left);
    make_empty(tree, node->right);
    free(node);
}
Exemplo n.º 4
0
tree_node_t *make_empty(tree_node_t *tree)
{
    if (NULL != tree)
    {
        make_empty(tree->left);
        make_empty(tree->right);
        free(tree);
    }

    return NULL;
}
Exemplo n.º 5
0
Arquivo: main.c Projeto: b3h3moth/L-C
int main(void) {
    Stack st1, st2;
    int st_size = 10;

    // Imposta l'ampiezza massima di entrambi gli stack a 10
    st1 = create(st_size);
    st2 = create(st_size);

    // Riempie entrambi gli stack
    for (int i=0; i<st_size; i++) {
        push(st1, i+i);
        push(st2, i*i);
    }

    destroy(st1);

    while (!is_empty(st2))
        printf("popped %d from st2 Stack\n", pop(st2));

    make_empty(st2);
    printf("popped %d from st2 Stack\n", pop(st2));

    push(st2, 100);

    if (is_empty(st2))
        fputs("st2 is empty\n", stdout);
    else
        fputs("st2 is not empty\n", stdout);
    
    destroy(st2);

    return(EXIT_SUCCESS);
}
int main(void)
{
	Stack s1, s2;
	void *n;
	
	s1 = create();
	s2 = create();
	
	push(s1, "\"first\"");
	push(s1, "\"second\"");
	
	n = pop(s1);
	printf("Popped %s from s1\n", n);
	push(s2, n);
	n = pop(s1);
	printf("Popped %s from s1\n", n);
	push(s2, n);
	
	destroy(s1);
	
	while (!is_empty(s2))
		printf("Popped %s from s2\n", pop(s2));
		
	push(s2, "\"third\"");
	make_empty(s2);
	if(is_empty(s2))
		printf("s2 is empty\n");
	else
		printf("s2 is not empty\n");
	
	destroy(s2);
	
	return 0;
}
Exemplo n.º 7
0
int main(void)
{
    Stack s1, s2;
    int n;

    s1 = create();
    s2 = create();

    push(s1, 1);
    push(s1, 2);

    printf("Peeked at %d from s1\n", peek(s1));

    n = pop(s1);
    printf("Popped %d from s1\n", n);
    push(s2, n);
    n = pop(s1);
    printf("Popped %d from s1\n", n);
    push(s2, n);
    destroy(s1);

    while (!is_empty(s2))
        printf("Popped %d from s2\n", pop(s2));

    push(s2, 3);
    make_empty(s2);
    if (is_empty(s2))
        printf("s2 is empty\n");
    else
        printf("s2 is not empty\n");

    destroy(s2);

    return 0;
}
Exemplo n.º 8
0
int main(int argc, char * argv[])
{
	int n = atoi(argv[1]);
	int m = atoi(argv[2]);
	int i = 0;
	List L = make_empty();
	L->next = L;
	position p = L;

	for (i = 1; i <= n; i++ ) {
		p->next = (position)malloc(sizeof(Node));
		p->next->element = i;
		p = p->next;
		p->next = L;
	}

	p->next = p->next->next;

	position prev = p;
	p = L->next;

	while (p->next != p) {
		for (i = 0; i < m; i++) {
			p = p->next;
			prev = prev->next;
		}

		printf("%d\n", p->element);
		prev->next = p->next;
		p = prev->next;
	}

	printf("%d\n", p->element);
	return 0;
}
Exemplo n.º 9
0
int main(void)
{
  char ch;

  printf("Enter parentheses and/or braces: ");
  make_empty();

  while (nested && !is_full()) {
    ch = getchar();
    if (ch == '(' || ch == '{') push(ch);
    else if (ch == ')') {
      if (pop() != '(') nested = false;
    }
    else if (ch == '}') {
      if (pop() != '{') nested = false;
    }
    else if (ch == '\n') {
      if (!is_empty) nested = false;
      break;
    }
  }
  if (nested) printf("Parenteses/braces are nested properly\n");
  else printf("Parenteses/braces are not nestes properly\n");

  return 0;
}
Exemplo n.º 10
0
stack_t *create_stack(int esz, int nr)
{
	stack_t *s;

	if (nr < MIN_STACK_SZ) {
		printf("error: stack size too small!\n");
		return NULL;
	}

	s = malloc(sizeof(stack_t));
	if (!s) {
		printf("out of memory!\n");
		return NULL;	
	}

	s->array = malloc(esz * nr);
	if (!s->array) {
		printf("out of array memory!\n");	
		return NULL;
	}
	s->cap = nr;
	s->esz = esz;
	make_empty(s);

	return s;
}
Exemplo n.º 11
0
int main()
{
  struct node	*head;
   int	pos,choice,data;
	head=(struct node*) malloc(sizeof(struct node));
	head=NULL;
	do
	{
	  printf("\n1.Create\n2.Insert\n3.Delete\n4.Locate\n5.Retrieve\n6.Display\n7.Make empty\n8.Exit");
	  printf("\n Enter choice : ");
	  scanf("%d",&choice);
	  switch(choice)
	  {
                    case 1: head=create(head);break;
                    case 2:
                           {
                              printf("\n1.Beginning\n2.End\n3.Middle\nEnter choice :");
                              scanf("%d",&choice);
                              switch(choice)
                              {
                                            case 1: head=insert(head,1);break;
                                            case 2: head=insert(head,0);break;
                                            case 3: printf("\nEnter the position to be inserted : ");
                                                 	scanf("%d",&pos);
                                                    head=insert(head,pos);
                                                    break;
                              }
                           }break;
                    case 3: 
                           {
                              printf("\nEnter the position to be deleted : ");
                              scanf("%d",&pos);
                              head=del(head,pos); 
                           }break;             
                    case 4:
                           {
                              printf("\nEnter the data to be located : ");
                              scanf("%d",&data);
                              pos=locate(head,data);
                              if(pos==0)printf("\nData not found\n");
                              else printf("\nData found at position %d\n",pos);
                           }break;
                    case 5:
                           {
                              struct node *temp;
                              temp=(struct node*) malloc(sizeof(struct node));
                              printf("\nEnter the position to be retrieved : ");
                              scanf("%d",&pos);
                              temp=retrieve(head,pos);
                              if(temp->data==-1)printf("\nData not found\n");
                              else printf("\nData found at position %d is %d\n",pos,temp->data);
                           }break;
                    case 6: display(head);break;
                    case 7: head=make_empty(head);break;
                    case 8: return 0;
      }
   }while(5);
   getch();
} 
Exemplo n.º 12
0
/*
 * tree_free --
 *   Free all the nodes of the tree.
 */
void tree_free(T * tree)
{
    assert(tree);

    if (*tree == NULL)
        return;
    make_empty(*tree, (*tree)->root);
    free(*tree);
    *tree = NULL;
}
Exemplo n.º 13
0
int main(void){
	
	char expn[STACK_SIZE + 1];
	
	make_empty();	
	
	for (;;){
		if (is_empty()){
			printf ("Enter an RPN expression: ");
			gets(expn);
			
			printf ("Value of expression: %d\n", 
				evaluate_RPN_expression(expn));
			
			make_empty();
			continue;
		}
		
	}
	
	return 0;
}
Exemplo n.º 14
0
int main(void)
{
	List L = (position)malloc(sizeof(struct Node));
	L = make_empty(L);

	insert_node(1, L, L);
	insert_node(2, L, L);
	insert_node(3, L, L);
	insert_node(4, L, L);

	print_list(L);
	return 0;
}
Exemplo n.º 15
0
void stmt(Token *tk)
{// statement
	make_empty();
	if 		(tk->id == If) 	  if_node(tk);
	else if (tk->id == While) while_node(tk);
	else if (tk->id == For)	  for_node(tk);
	else if (tk->id == Int)   int_node(tk);
	else if (tk->id == Ret)   return_node(tk);
	else if (tk->id == Prtf)  printf_node(tk);
	else if (tk->id == Stru)  struct_dec(tk);
	else if (tk->id == ';')   next(tk);
	else if (tk->id == '}')   return ;
	else { expr(tk); assembly(pop()); }
}
Exemplo n.º 16
0
int main(void){
	
	char ch;
	int op1, op2;

	make_empty();	
	
	for (;;){
		if (is_empty()){
			printf ("Enter an RPN expression: ");
		}
		scanf (" %c", &ch);
/*		printf ("ch: %c\n", ch);*/
		
		if (ch >= '0' && ch <= '9'){			
			push(ch - '0');			
		}else if (ch == '+' || ch == '-' || ch == '*' || ch == '/'){
			op2 = pop();
			op1 = pop();
/*			printf ("op1: %d\n", op1);*/
/*			printf ("op2: %d\n", op2);		*/
			switch (ch){
				case '+': push(op1 + op2); break;
				case '-': push(op1 - op2); break;
				case '*': push(op1 * op2); break;
				case '/': push(op1 / op2); break;
			}						
		}else if (ch == '='){
			printf ("Value of expression: %d\n", pop());
			make_empty();
			continue;
		}else{
			exit (EXIT_SUCCESS);
		}
	}
	
}
Exemplo n.º 17
0
int main(void) {

    int c, i, j, tmp, isbalanced;
    do {
        j = 0;
        printf("Enter your input that contains parenthesis or Q to quit:\n");
        while ((c = getchar()) != '\n') {
            j++;
            tmp = c;
            if(c == '(' || c == '<' || c == '[' || c == '{') {
                push(c);
                }
            if(c == ')' || c == '>' || c == ']' || c == '}') {
                if(c == ')' && pop() == '(') {
                    continue;
                    }
                else if(c == '>' && pop() == '<') {
                    continue;
                    }
                else if(c == ']' && pop() == '[') {
                    continue;
                    }
                else if(c == '}' && pop() == '{') {
                    continue;
                    }
                else {
                    top++;
                    isbalanced = 0;
                    }
                }
            }

        if(tmp == 'Q' && j ==1)
            return 0;
        if(top == 0)
            isbalanced = 1;
        else
            isbalanced = 0;        

        if(isbalanced == 1)
            printf("The input is balanced.\n");
        else
            printf("The input is not balanced.\n");
        make_empty();
    } 
    while (i >= 0);

    return 0;
}
Exemplo n.º 18
0
stack_t *
create_stack(int size)
{
	stack_t *s;

	if (size < MinStackSize)
		error();

	s = malloc(sizeof(stack_t));

	s->array = malloc(sizeof(int) * size);

	s->capacity = size;
	make_empty(s);

	return s;
}
int main(void)
{
    Stack s1, s2;
    int n;

    s1 = create();
    s2 = create();

    push(s1, 1);
    printf("Length of s1: %d\n", length(s1));
    push(s1, 2);
    printf("Length of s1: %d\n", length(s1));

    n = pop(s1);
    printf("Popped %d from s1\n", n);
    printf("Length of s1: %d\n", length(s1));
    push(s2, n);
    printf("Length of s2: %d\n", length(s2));
    n = pop(s1);
    printf("Popped %d from s1\n", n);
    printf("Length of s1: %d\n", length(s1));
    push(s2, n);
    printf("Length of s2: %d\n", length(s2));

    destroy(s1);

    while (!is_empty(s2)) {
        printf("Popped %d from s2\n", pop(s2));
        printf("Length of s2: %d\n", length(s2));
    }

    push(s2, 3);
    printf("Length of s2: %d\n", length(s2));
    make_empty(s2);
    if (is_empty(s2)) {
        printf("s2 is empty\n");
    } else {
        printf("s2 is not empty\n");
    }

    destroy(s2);

    return 0;
}
Exemplo n.º 20
0
Map<S,T>&
Map<S,T>::operator= (const Map<S,T>& m)
{
	if (this != &m) {
		make_empty();
		for (Mapiter<S,T> p (m); ++p; ) {
			// S s = p.key();	/* sidestep a cfront bug */
			// (*this) [s] = p.value();
			(*this) [p.key()] = p.value();
		}
//		iter_head=0;
//		mi_count = 0;
		/* march through the iterator list, setting each to vacuous */
		for (Mapiter<S,T> *ip = iter_head; ip; ip = ip->iter_next) {
			/* ip->m = this; */ /* unnecessary */
			ip->p = 0;
		}
   		remove_flag = 0;
	}
	return *this;
}
Exemplo n.º 21
0
void input(void) {
    int ch;

    printf("Enter an RPN expression: ");
    while (true) {
        ch = getchar();
        if (ch == '*' || ch == '/' || ch == '+' || ch == '-') {
            calculate(ch);
        } else if (ch == '=') {
            display();
        } else if (ch == ' ') {
            continue;
        } else if (ch == 'q') {
            make_empty();
        } else if (ch == '\n') {
            break;
        } else {
            ch -= 48;
            push(ch);
        }
    }
}
Exemplo n.º 22
0
void syntax(Token *tk)
{// syntax analyzer
	char *fun_name;
	int fun_pos;
	Func *fun = (Func *)malloc(sizeof(Func));
	while (tk->id) {
		make_empty();
		if (tk->id == Void || tk->id == Int) {
			fun->ret = tk->id;
			next(tk);
			if (tk->id == Id) {
				adjust_ebp();
				if (lookup(tk->beg, Fun, tk->line, false) < 0) {
					fun_pos = fun_insert(tk->beg);
					fun_name = (char *)malloc((strlen(tk->beg) + 1) * sizeof(char));
					strcpy(fun_name, tk->beg);
				} else PANIC("function redefinition", tk->line);
			} else PANIC("missing function name", tk->line);
		} else if (tk->id == Stru) { struct_def(tk);
		} else if (tk->id == '(') {
			ent();
			fun->argc = 0;
			argument_dec(tk, fun);
			if (tk->id == ')') fun_info_insert(fun_name, fun_pos, fun);
			else PANIC("illegal function declaration", tk->line);
		} else if (tk->id == '{') {
			next(tk);
			if (tk->id == '}') PANIC("expected statement", tk->line);
			while (tk->id != '}' && tk->id) {
				stmt(tk);
				next(tk);
			}
			lev();
			if (!tk->id) PANIC("expected right brace", tk->line);
		}
		next(tk);
	}
}
Exemplo n.º 23
0
void calculate(int operator) {
    int operand2 = pop();
    int operand1 = pop();

    switch ((char)operator) {
    case '+':
        push(operand1 + operand2);
        break;
    case '-':
        push(operand1 - operand2);
        break;
    case '*':
        push(operand1 * operand2);
        break;
    case '/':
        if (operand2 != 0) {
            push(operand1 / operand2);
        }
        break;
    default:
        printf("Bad operator!\n");
        make_empty();
    }
}
Exemplo n.º 24
0
void destroy(Stack s)
{
  make_empty(s);
  free(s);
}
Exemplo n.º 25
0
Winner::~Winner( ){

   make_empty();

}
Exemplo n.º 26
0
void destroy(Queue q) {
	make_empty(q);
	free(q);
}
Exemplo n.º 27
0
HStore * adeven_count_text_array( Datum* i_data, int n, bool * nulls )
{
    adeven_count_Array a;
    adeven_count_init_array( &a, 100 );
    AvlTree tree = make_empty( NULL );
    int i, j;

    for( i = 0; i < n; ++i )
    {
        if( ! nulls[i] )
        {
            bool found = false;
            size_t datum_len = VARSIZE( i_data[i] ) - VARHDRSZ;
            char * current_datum = ( char * ) palloc ( datum_len );
            memcpy( current_datum, VARDATA( i_data[i] ), datum_len );

            Position position = find( current_datum, datum_len, tree );
            if( position == NULL )
            {
                j = a.used;
                tree = insert( current_datum, datum_len, j, tree );
                adeven_count_insert_array( &a, current_datum, datum_len );
            }
            else
            {
                j = value( position );
            }

            a.counts[j] += 1;
        }
    }

    // save sort permutation to create pairs in order of ascending keys
    // we assume that postgres stores the pairs in that order
    int * perm = ( int * ) palloc ( a.used * sizeof( int ) );
    sort_perm( tree, perm );

    make_empty( tree );

    Pairs * pairs = palloc0( a.used * sizeof( Pairs ) );
    int4 buflen = 0;
    for( i = 0; i < a.used; ++i )
    {
        j = perm[i];
        if( a.array[j] != NULL )
        {
            size_t datum_len = a.sizes[j];
            int digit_num = adeven_count_get_digit_num( a.counts[j] );
            char * dig_str = palloc0(digit_num);
            sprintf( dig_str, "%d", a.counts[j] );
            a.counts_str[j] = dig_str;
            pairs[i].key = a.array[j];
            pairs[i].keylen =  datum_len;
            pairs[i].val = dig_str;
            pairs[i].vallen =  digit_num;
            pairs[i].isnull = false;
            pairs[i].needfree = false;
            buflen += pairs[i].keylen;
            buflen += pairs[i].vallen;
        }
    }
    HStore * out;
    out = hstorePairs( pairs, a.used, buflen );
    //adeven_count_free_array( &a );
    return out;
}
Exemplo n.º 28
0
/******************************************************************
*
*	aa_to_codon(sfp, aa_start, aa_stop)
*	generate a list of CodonVecotr to show the codons of an 
*	amino acid sequence
*	sfp: the Seq-feat for cds
*	aa_start: the start position of protein sequence
*	aa_stop the stop position of protein sequence
*
******************************************************************/
NLM_EXTERN ValNodePtr aa_to_codon(SeqFeatPtr sfp, Int4 aa_start, Int4 aa_stop)
{
  BioseqPtr bsp;

  Int4 frame_offset, start_offset;
  SeqLocPtr slp = NULL;
  SeqLocPtr cdloc;
  CdRegionPtr crp;
  Uint1 frame;

  Boolean is_end;			/**is the end for process reached?**/
  Int4 p_start=0, p_stop=0;		/**protein start & stop in defined
					corresponding CdRegion Seq-loc**/

  Int4 line_len;
  Int4 cur_pos;			/**current protein position in process**/
  Int4 cd_len;		/**length of the cDNA for the coding region**/

  Int2 i, j;
  Int2 k, n;
  CharPtr PNTR buf;

  Boolean is_new;		/**Is cur_pos at the begin of new Seq-loc?**/
  CharPtr temp;

  SeqPortPtr spp;
  Uint1 residue;

  Boolean end_partial;
  Int4 d_start, seq_pos;
  Int2 pos;

  ValNodePtr head= NULL;
  CodonVectorPtr cvp;
  Boolean prt_stop_codon;
  Uint2 exon;




   if(sfp->data.choice !=3)
	return NULL;

   crp = sfp->data.value.ptrvalue;
   if(!crp)
	return NULL;
   frame = crp->frame;
   cdloc = sfp->location;
   if(cdloc == NULL )
	return NULL;

   if(frame>0)
	frame_offset = frame-1;
   else
	frame_offset = 0;
   start_offset = frame_offset;

   prt_stop_codon = (aa_stop == SeqLocStop(sfp->product));
   line_len = (aa_stop - aa_start + 1) + 1;
					/* +1 for the possible partial start codon*/
   if(prt_stop_codon)/*can be either as a stop codon or partial stop*/
	++line_len;
   buf = MemNew((size_t)3 * sizeof(CharPtr));
   for(i =0; i<3; ++i)
	buf[i] = MemNew((size_t)(line_len + 1) * sizeof (Char));
		

   cur_pos= aa_start;
   cd_len = 0;
   is_end = FALSE;
   p_start = 0;
   slp = NULL;
   exon = 0;
   while(!is_end && ((slp = SeqLocFindNext(cdloc, slp))!=NULL))
   {
	++exon;
	cd_len += SeqLocLen(slp);
	end_partial = ((cd_len - start_offset)%3 != 0);
	p_stop = (cd_len - start_offset)/3 -1;
	if(end_partial)
	   ++p_stop;
	if(p_stop > aa_stop || (p_stop == aa_stop && !end_partial))
	{
	   p_stop = aa_stop;		/**check if the end is reached**/
	   is_end = TRUE;
	}

	if(p_stop >= cur_pos)	/*get the exon*/
	{
	   bsp = BioseqLockById(SeqLocId(slp));
	   if(bsp)
	   {
		is_new = (p_start == cur_pos);	/*start a new exon?*/
		cvp = MemNew(sizeof(CodonVector));
		cvp->sip = SeqIdDup(find_sip(bsp->id));
		cvp->strand = SeqLocStrand(slp);
		cvp->exonCount = exon;
		if(is_new)
		{
			if(frame_offset == 0)
				cvp->frame = 0;
			else
				cvp->frame = 3- (Uint1)frame_offset;
		}
		else
			cvp->frame = 0;
		if(cur_pos==0 && frame_offset > 0)	/*partial start codon*/
			cvp->aa_index = 0;
		else
			cvp->aa_index = 1;
		if(is_new)	/**special case of the first partial**/
		   d_start = SeqLocStart(slp);
		else
		{
		   if(frame_offset && p_start >0)
			++p_start;
		   d_start = SeqLocStart(slp) + 3*(cur_pos - p_start) + frame_offset;
		}
	    /**p_start is the start position of aa in the current Seq-loc
	       cur_pos is the current aa that is in process. The offset will
	       help to located the position on the DNA Seq-loc for translation
	       d_start is the position of the starting DNA in the coordinates
	       of DNA segment, used for mark the sequence
	       **/

		seq_pos = d_start - SeqLocStart(slp);	/**the pos in spp**/
		if(SeqLocStrand(slp)== Seq_strand_minus)
		   d_start = SeqLocStop(slp) - seq_pos;
		cvp->dna_pos = d_start;

		n = (Int2)cur_pos - (Int2)aa_start + cvp->aa_index;	/*position in buffer*/
		for(i =0; i<3; ++i)
			make_empty(buf[i], (Int2)line_len);
		spp = SeqPortNewByLoc(slp, Seq_code_iupacna);
		SeqPortSeek(spp, seq_pos, SEEK_SET);
		/**store the partial codons**/
		if(is_new && frame_offset > 0)
		{
		   k = (Int2)frame_offset;
		   while(k > 0)
		   {
			residue = SeqPortGetResidue(spp);
			temp = buf[3-k];	/**the position**/
			pos = n;
			temp[pos] = TO_LOWER(residue);
			--k;
		   }
		   ++n;
		   if(cur_pos!=0)
			++cur_pos;
		}


	     	/**load  the codons**/
		k =0;
		while((residue = SeqPortGetResidue(spp)) != SEQPORT_EOF && cur_pos <= p_stop)
		{
		   j= (Uint1)k%3;
		   temp = buf[j];
		   temp[n] = TO_LOWER(residue);
		   if(j ==2)
		   {		/**the last base**/
			++n;
		 	if(!prt_stop_codon|| !is_end) /*for the last codon*/
			/**prt_end controls to print the whole loc**/
		   	   ++cur_pos;
		   }
		   ++k;
		}	/**end of while**/

		SeqPortFree(spp);

		for(i =0; i<3; ++i)
		   cvp->buf[i] = StringSave(buf[i]);
		ValNodeAddPointer(&head, 0, (Pointer)cvp);

		BioseqUnlock(bsp);
	   }/*end of if(bsp)*/
	}/**end of if for matched intervals**/

	if(end_partial)
	    p_start = p_stop;
	else
	    p_start = p_stop +1;

	frame_offset = (cd_len - start_offset)%3;
	 if(frame_offset >0)
	    frame_offset = 3-frame_offset;

   }/**end of while(slp && !is_end) **/

   for(i=0; i<3; ++i)
	MemFree(buf[i]);
   MemFree(buf);

   return head;
}
Exemplo n.º 29
0
void stack_overflow(void) {
    printf("Expression is too complex!\n");
    make_empty();
}
Exemplo n.º 30
0
void stack_underflow(void) {
    printf("Not enough operands in expression!\n");
    make_empty();
}