Ejemplo n.º 1
0
int test_top ( void )
{
  Stack_t *sp = NULL;  
  Stack_t *sp1 = NULL;
  
  printf("*** test_top\n");

  sp = StackCreate(4);

  (void)push(sp,100);
  (void)push(sp,200);
  (void)push(sp,300);
  (void)push(sp,700);
  StackDump(sp, 0);
  
  printf("\tTest01 return 700 %d\n", top(sp));

  printf("\tTest02 return  -1 %d  NULL stack\n", top(NULL));  

  sp1 = StackCreate(4);  
  printf("\tTest02 return  -1 %d  Empty Stack\n", top(sp1));  

  (void)StackDestroy(sp);
  (void)StackDestroy(sp1);  

  printf("test_top - Ends\n");

  return 0;
}
Ejemplo n.º 2
0
CPU_t* CPU_Create ( int StackSize )
{
    CPU_t* NewCPU = ( CPU_t* ) calloc ( 1, sizeof ( CPU_t ) );
	if ( NewCPU == NULL )
    {
        CPU_Errno = ENOMEM;
        return NULL;
    }

    NewCPU->ProgramCounter = 0;

    NewCPU->CPUStack = StackCreate( StackSize );
    if ( NewCPU->CPUStack  == NULL )
    {
        CPU_Errno = ENOMEM;
        return NULL;
    }

    NewCPU->RAM = ( double* ) calloc ( MaxRAM, sizeof ( double ) );
    if ( NewCPU->RAM == NULL )
    {
        CPU_Errno = ENOMEM;
        return NULL;
    }

    NewCPU->CPURegister = ( double* ) calloc ( MaxRegisters, sizeof ( double ) );
    if ( NewCPU->CPURegister == NULL )
    {
        CPU_Errno = ENOMEM;
        return NULL;
    }

    CPU_Errno = 0;
    return NewCPU;
}
Ejemplo n.º 3
0
bool hasPathSum(struct TreeNode* root, int sum) {
	if(!root) return false;

	SumStackNode *stackTop = StackCreate(); 
	stackTop = StackPush(NULL, root, 0);

	while(!isStackEmpty(stackTop)){
		int summary = stackTop->currentSum;
		TreeNode *currentNode = stackTop->treeNode;
		stackTop = StackPop(stackTop);

		while(currentNode->left || currentNode->right){
			if(currentNode->left && currentNode->right){
				stackTop = StackPush(stackTop, currentNode->right, summary);
				currentNode->right = NULL;
			}
			
			currentNode = (currentNode->left)?currentNode->left:currentNode->right;
			summary += currentNode->val;
		}
		if(summary == sum) return true;
	}

	return false;    
}
Ejemplo n.º 4
0
Archivo: abp.c Proyecto: pbmartins/AlgC
int ABPMultSum (PtABPNode proot, int pvalue)
{
    PtABPNode node = proot;
    PtStack stack;
    int sum = 0;

    if (proot == NULL) {
        Error = ABP_EMPTY;
        return -1;
    }

    if ((stack = StackCreate(sizeof(PtABPNode))) == NULL) {
        Error = NO_MEM;
        return -1;
    }

    StackPush(stack, &node);

    while (!StackIsEmpty(stack)) {
        StackPop(stack, &node);
        if (!(node->Elem % pvalue))
            sum += node->Elem;

        if (node->PtRight != NULL)
            StackPush(stack, &(node->PtRight));
        if (node->PtLeft != NULL)
            StackPush(stack, &(node->PtLeft));
    }

    StackDestroy(&stack);
    Error = OK;
    return sum;
}
Ejemplo n.º 5
0
Archivo: abp.c Proyecto: pbmartins/AlgC
void ABPPostOrderRep (PtABPNode proot)  /* travessia em pós-ordem repetitiva - repetitive post-order traversal */
{
	PtABPNode Node = proot, Last = proot; PtStack Stack;

	if (proot == NULL) { Error = ABP_EMPTY; return;	}	/* arvore vazia - empty tree */
	if ((Stack = StackCreate (sizeof (PtABPNode))) == NULL) { Error = NO_MEM ; return; }

	while (1)	/* enquanto houver elementos para visitar - while there are nodes */
	{
			/* travessia pela esquerda até atingir a folha mais à esquerda - left traversal until the left most leaf */
		while (Node->PtLeft != NULL)
		{ StackPush (Stack, &Node); Node = Node->PtLeft; }

		/* se não tem subarvore direita ou já completou a sua travessia - without right subtree or already completed the traversal */
		while (Node->PtRight == NULL || Node->PtRight == Last)
		{
			printf ("%d ", Node->Elem);	/* imprimir o elemento - printing the element */
			Last = Node;	/* assinalar último elemento visitado - marking the last visited element */
					/* terminar quando não há mais elementos na pilha - terminate when there are no more elments in the stack */
			if (StackIsEmpty (Stack)) { StackDestroy (&Stack); Error = OK; return; }

			StackPop (Stack, &Node);	/* retirar o elemento anterior - retrieve the preview element */
		}

		StackPush (Stack, &Node);	/* recolocar o elemento na pilha - restore the element in the stack */
		Node = Node->PtRight;	/* iniciar travessia da subarvore direita - start the right subtree traversal */
	}
}
Ejemplo n.º 6
0
Archivo: abp.c Proyecto: pbmartins/AlgC
void ABPInOrderRep (PtABPNode proot)  /* travessia em in-ordem repetitiva - repetitive in-order traversal */
{
	PtABPNode Node = proot; PtStack Stack;

	if (proot == NULL) { Error = ABP_EMPTY; return;	} /* arvore vazia - empty tree */
	if ((Stack = StackCreate (sizeof (PtABPNode))) == NULL) { Error = NO_MEM ; return; }
	StackPush (Stack, &Node);	/* armazenar a raiz - storing the root */

	while (!StackIsEmpty (Stack))
	{
		if (Node != NULL)	/* não é um no externo - not an external node */
		{
			Node = Node->PtLeft;
			StackPush (Stack, &Node);	/* subarvore esquerda - left subtree */
		}
		else	/* é um no externo */
		{
			StackPop (Stack, &Node);	/* retirar e descartar o no externo - retrieve and ignore the external node */
			if (!StackIsEmpty (Stack))
			{
				StackPop (Stack, &Node);	/* recuperar o no anterior - retrieve and preview node */
				printf ("%d ", Node->Elem);	/* imprimir o elemento - printing the element */
				Node = Node->PtRight;	/* subarvore direita - right subtree */
				StackPush (Stack, &Node);
			}
		}
	}

	StackDestroy (&Stack);	/* destruir a pilha - releasing the stack */
    Error = OK;
}
Ejemplo n.º 7
0
Archivo: abp.c Proyecto: pbmartins/AlgC
unsigned int ABPSizeRep (PtABPNode proot)  /* cálculo do número de elementos repetitiva - repetitive number of nodes */
{
	PtABPNode Node = proot; PtStack Stack; unsigned int Number = 0;

	if (proot == NULL) { Error = ABP_EMPTY; return 0;	} /* arvore vazia - empty tree */

	if ((Stack = StackCreate (sizeof (PtABPNode))) == NULL) { Error = NO_MEM ; return 0; }
	StackPush (Stack, &Node);	/* armazenar a raiz - storing the root */

	while (!StackIsEmpty (Stack))	/* enquanto existirem nos - while there are nodes */
	{
		StackPop (Stack, &Node);	/* recuperar o no - retrieve the node */
		Number++;	/* contar o no - counting the node */

		/* armazenar a raiz da subarvore esquerda - storing the left subtree root */
		if (Node->PtLeft != NULL) StackPush (Stack, &Node->PtLeft);

		/* armazenar a raiz da subarvore direita - storing the right subtree root */
		if (Node->PtRight != NULL) StackPush (Stack, &Node->PtRight);
	}

	StackDestroy (&Stack);	/* destruir a pilha - releasing the stack */

    Error = OK;
	return Number;
}
Ejemplo n.º 8
0
/** Creates an instance of stacked memory.
 *
 *  @param StackedMemory Address of variable that, in case of success, receives
 *  address of new stacked memory object.
 *
 *  @return
 *  The function returns NTSTATUS value indicating success or failure of the operation.
 */
NTSTATUS StackedMemoryCreate(POOL_TYPE PoolType, PUTILS_STACK *StackedMemory)
{
   PUTILS_STACK tmpStackedMemory = NULL;
   NTSTATUS status = STATUS_UNSUCCESSFUL;
   DEBUG_ENTER_FUNCTION("PoolType=%u; StackedMemory=0x%p", PoolType, StackedMemory);

   *StackedMemory = NULL;
   status = StackCreate(PoolType, &tmpStackedMemory);
   if (NT_SUCCESS(status))
      *StackedMemory = tmpStackedMemory;

   DEBUG_EXIT_FUNCTION("0x%x, *StackedMemory=0x%p", status, *StackedMemory);
   return status;
}
stk_stack* RBEnumerate(rb_red_blk_tree* tree, void* low, void* high) {
  stk_stack* enumResultStack;
  rb_red_blk_node* nil=tree->nil;
  rb_red_blk_node* x=tree->root->left;
  rb_red_blk_node* lastBest=nil;

  enumResultStack=StackCreate();
  while(nil != x) {
    if ( 1 == (tree->comp_func(x->key,high)) ) { /* x->key > high */
      x=x->left;
    } else {
      lastBest=x;
      x=x->right;
    }
  }
  while ( (lastBest != nil) && (1 != tree->comp_func(low,lastBest->key))) {
    StackPush(enumResultStack,lastBest);
    lastBest=TreePredecessor(tree,lastBest);
  }
  return(enumResultStack);
}
Ejemplo n.º 10
0
Archivo: abp.c Proyecto: pbmartins/AlgC
int ABPEvenOrderSum (PtABPNode proot)
{
    PtABPNode node = proot;
    PtStack stack;
    int order = 0, sum = 0;
    
    if (proot == NULL) {
        Error = ABP_EMPTY;
        return -1;
    }

    if ((stack = StackCreate(sizeof(PtABPNode))) == NULL) {
        Error = NO_MEM;
        return -1;
    }

    StackPush(stack, &node);

    while(!StackIsEmpty(stack)) {
        if (node != NULL) {
            node = node->PtLeft;
            StackPush(stack, &node);
        } else {
            StackPop(stack, &node);
            if (!StackIsEmpty(stack)) {
                StackPop(stack, &node);
                if (order++ & 1)
                    sum += node->Elem;
                node = node->PtRight;
                StackPush(stack, &node);
            }
        }
    }

    StackDestroy(&stack);
    Error = OK;
    return sum;
}
Ejemplo n.º 11
0
STKSTACK
RbEnumerate (RBTREE tree, RBKEY low, RBKEY high)
{
	STKSTACK enumResultStack=0;
	RBNODE nil=tree->nil;
	RBNODE x=tree->root->left;
	RBNODE lastBest=nil;

	enumResultStack=StackCreate();
	while(nil != x) {
		if ( 0 < (TreeCompare(tree, x->key, high)) ) { /* x->key > high */
			x=x->left;
		} else {
			lastBest=x;
			x=x->right;
		}
	}
	while ( (lastBest != nil) && (0 >= TreeCompare(tree, low,lastBest->key))) {
		StackPush(enumResultStack,lastBest);
		lastBest=RbTreePredecessor(tree,lastBest);
	}
	return(enumResultStack);
}
Ejemplo n.º 12
0
Archivo: abp.c Proyecto: pbmartins/AlgC
void ABPPreOrderRep (PtABPNode proot)  /* travessia em pré-ordem repetitiva - repetitive pre-order traversal */
{
	PtABPNode Node = proot; PtStack Stack;

	if (proot == NULL) { Error = ABP_EMPTY; return;	} /* arvore vazia - empty tree */
	if ((Stack = StackCreate (sizeof (PtABPNode))) == NULL) { Error = NO_MEM ; return; }

	StackPush (Stack, &Node);	/* armazenar a raiz - storing the root */
	while (!StackIsEmpty (Stack))	/* enquanto existirem nos - while there are nodes */
	{
		StackPop (Stack, &Node);	/* recuperar o no - retrieve the node */
		printf ("%d ", Node->Elem);	/* imprimir o elemento - printing the element */

				/* colocar a raiz da subarvore direita, caso exista - storing the right subtree root if it exists */
		if (Node->PtRight != NULL) StackPush (Stack, &Node->PtRight);

				/* colocar a raiz da subarvore esquerda, caso exista - storing the left subtree root if it exists */
		if (Node->PtLeft != NULL) StackPush (Stack, &Node->PtLeft);
	}

	StackDestroy (&Stack);	/* destruir a pilha - releasing the stack */
    Error = OK;
}
Ejemplo n.º 13
0
int main(int argc, char* argv[], char** envp)
{

	int i = 0;
	int count = 20;
	char *s1 = "abcdefg";
	char *s2 = "g";
	size_t len1 = 0;
	size_t n = 3;
	char dest[20];

	node_t *node1 = NULL;
	node_t *node2 = NULL;
	node_t *node3 = NULL;
	node_t *node4 = NULL;
	node_t *node5 = NULL;
	
	char *t = NULL;	
	
	stack_t *stack = NULL;
	size_t n1 = 1;
	size_t n2 = 2;
	size_t n3 = 3;
	size_t n4 = 4;
	size_t n5 = 5;
	
	/*__________  RecFibonacci  __________*/
	printf("\n[%s %s %d]RecFibonacci\n", __FILE__, __func__, __LINE__);
	
	for (i = 0; i < count; ++i)
	{
		printf("RecFibonacci(%i):%i\n", i , RecFibonacci(i));
	}
	
	/*__________  RecStrlen  __________*/
	printf("\n[%s %s %d]RecStrlen\n", __FILE__, __func__, __LINE__);
	
	len1 = RecStrlen(s1);
	printf("len1:%lu\n", len1);
	
	/*__________  RecStrcmp  __________*/
	printf("\n[%s %s %d]RecStrcmp\n", __FILE__, __func__, __LINE__);
	
	printf("RecStrcmp(s1, s2): %i\n", RecStrcmp(s1, s2));
	
	
	/*__________  RecStrncmp  __________*/
	printf("\n[%s %s %d]RecStrncmp\n", __FILE__, __func__, __LINE__);
	
	printf("RecStrncmp(s1, s2, n): %i\n", RecStrncmp(s1, s2, n));


	/*__________  RecStrstr  __________*/
	printf("\n[%s %s %d]RecStrstr\n", __FILE__, __func__, __LINE__);
	
	printf("RecStrstr(s1, s2):%s\n", RecStrstr(s1, s2));
	
	/*__________  RecStrcpy  __________*/
	printf("\n[%s %s %d]RecStrcpy\n", __FILE__, __func__, __LINE__);
	t = RecStrcpy(dest, s1);
	printf("RecStrcpy(dest, s1):%s		expected result:%s\n", t, dest);
	
	
	/*__________  RecStrcat  __________*/
	printf("\n[%s %s %d]RecStrcat\n", __FILE__, __func__, __LINE__);
	
	printf("RecStrcat(dest, s1):%s		expected result:%s\n", 
			RecStrcat(dest, s1), 						"abcgefgabcdefg");
	
	/*__________  RecSlistFlip  __________*/
	printf("\n[%s %s %d]RecSListFlip\n", __FILE__, __func__, __LINE__);
	
	node5 = SListCreateNode((void*)5, NULL);
	node4 = SListCreateNode((void*)4, node5);
	node3 = SListCreateNode((void*)3, node4);
	node2 = SListCreateNode((void*)2, node3);
	node1 = SListCreateNode((void*)1, node2);
	
	printf("SListCount(node1):%lu\n", SListCount(node1));
	
	RecSListFlip(node1);
	
	printf("SListCount(node1):%lu\n", SListCount(node5));
	
	SListFreeAll(node5);
	
	/*__________  Compere  __________*/
	printf("\n[%s %s %d]Compere\n", __FILE__, __func__, __LINE__);
	
	printf("Compere(&n1, &n2):%i		expected result:1\n", Compere(&n1, &n2));
	
	printf("Compere(&n1, &n1):%i		expected result:1\n", Compere(&n1, &n2));
	
	printf("Compere(&n2, &n1):%i		expected result:0\n", Compere(&n2, &n1));
	/*_________________________  END Compere _______________________*/
	
	
	/*________________________________________________________________*/
	printf("\n[%s %s %d]RecStackSort\n", __FILE__, __func__, __LINE__);
	/*_________________________  RecStackSort  _______________________*/
	
	stack = StackCreate(sizeof(size_t), 5);
	assert(stack);
	
	StackPush(stack, &n3);
	StackPush(stack, &n2);
	StackPush(stack, &n5);
	StackPush(stack, &n4);
	StackPush(stack, &n1);

	RecStackSort(stack, &Compere, sizeof(size_t));
	
	for( ; StackSize(stack); StackPop(stack))
	{
		printf("StackPeek(stack):%lu\n", *(size_t*)StackPeek(stack));
	}
	
	StackDestroy(stack);	
	
	
	return(0);
}
Ejemplo n.º 14
0
/* compile all rules */
EtalisExecTree* buildExecTree()
{
    printf("--- Generating Rule Tree ...\n");
    EtalisExecTree* tree = calloc(1,sizeof(EtalisExecTree));
    tree->size=3; /* TODO (hafsi#4#): fixme */ /*if more than one rule, find out how many complex events*/
    tree->exec=print_event;
    tree->complexEvents = (EtalisExecNode*)calloc(tree->size,sizeof(EtalisExecNode));

    EtalisBatch* temp_batch = (EtalisBatch*)malloc(sizeof(EtalisBatch));

    int i=0;

    static predicate_t p;
    term_t _args_binary_event_rule = PL_new_term_refs(3);
    atom_t name;
    int temp_arity;

    if ( !p )
        p = PL_predicate("binary_event_rule", 3, NULL);

    qid_t qid = PL_open_query(NULL, PL_Q_NORMAL, p, _args_binary_event_rule);

    while(PL_next_solution(qid) != FALSE)
    {

        EtalisEventNode* temp_event = tree->complexEvents+i; /* next complex event */
        EtalisExecNode* temp_operator =(EtalisExecNode*)malloc(sizeof(EtalisExecNode));
        memset(temp_operator,0,sizeof(EtalisExecNode));

        assert( temp_event != NULL && temp_operator != NULL);

        temp_event->parentNode=NULL;          /*a complex event does not have a parent*/
        temp_event->childNode=temp_operator;
        temp_event->trigger=_cep_print_event; /* by default, triggering a complx event would print it */

        temp_operator->parentEvent=temp_event;

        temp_batch->batchSize=1;
        temp_batch->nodes=temp_operator;


        /*get label*/
        PL_get_name_arity(_args_binary_event_rule, &name, &temp_arity);
        strcpy(temp_batch->label,PL_atom_chars(name));
        /*get complex event*/
        PL_get_name_arity(_args_binary_event_rule+1, &name, &temp_arity);
        strcpy(temp_event->event.name,PL_atom_chars(name));
        temp_event->event.arity = temp_arity;

        /*get rule*/
        construct_rule(temp_batch,_args_binary_event_rule+2);

        /* init a stack for each event*/

        /* query the tree in the depth */
        EtalisEventNode* temp_event_index = temp_operator->leftChild;
        for (temp_event_index = temp_operator->leftChild;temp_event_index->childNode != NULL;temp_event_index = temp_event_index->childNode->leftChild)
        {
            temp_event_index->eventStack = StackCreate();
            if(temp_event_index->parentNode->op_type == binary)
                temp_event_index->parentNode->rightChild->eventStack = StackCreate();
        }
        /* Create stack for leaf nodes*/
        temp_event_index->eventStack = StackCreate();
            if(temp_event_index->parentNode->op_type == binary)
                temp_event_index->parentNode->rightChild->eventStack = StackCreate();



        /* build argument logical models */

/*
        if(temp_operator->has_condition == ETALIS_TRUE)
            {
                    ;
            build_args_map(temp_operator,_args_binary_event_rule+1,_args_binary_event_rule+2);

            }
            else
            {

            build_args_map(temp_operator,_args_binary_event_rule+1,_args_binary_event_rule+2);
            }

*/

        /*print the rule*/ /* only if debugging */

        #ifdef DEBUG
        /*print_rule(temp_event);*/
        #endif

        /*add to event hash*/
        addToEventHash(temp_operator);


        i++; /*next rule*/
    };

    PL_close_query(qid);

    /*from the rules build the tree*/


    printf("--- Done!\n");
    return tree;
}
Ejemplo n.º 15
0
int test_swap ( void )
{
  Stack_t *sp = NULL;  
  Stack_t *sp1= NULL;
  Stack_t *sp2 = NULL;
  Stack_t *sp3 = NULL;
  Stack_t *sp4 = NULL;
  Stack_t *sp5 = NULL;
  Stack_t *sp6 = NULL;
  Stack_t *sp7 = NULL;
  Stack_t *sp8 = NULL;  
  Stack_t *sp9 = NULL;
  
  printf("test_swap - create <int> stack\n");

  sp = StackCreate(4);
  sp1= StackCreate(4);
  
  (void)push(sp,100);
  (void)push(sp,200);
  (void)push(sp,300);
  (void)push(sp,700);

  (void)push(sp1,101);
  (void)push(sp1,201);
  (void)push(sp1,301);
  (void)push(sp1,701);

  (void)StackDestroy(sp);
  printf("\tTest01 swap - same size, but stack freed -1 = %d\n", swap(sp,sp1));

  /*
   * Test02 positive swap - same size
   */
  sp2 = StackCreate(4);
  sp3 = StackCreate(4);

  (void)push(sp2,1);
  (void)push(sp2,2);
  (void)push(sp2,3);
  (void)push(sp2,4);
  printf("\tTest02 - before sp2\n");
  StackDump(sp2,0);

  (void)push(sp3,5);
  (void)push(sp3,6);
  (void)push(sp3,7);
  (void)push(sp3,8);
  printf("\tTest02 - before sp3\n");
  StackDump(sp3,0);

  printf("Test02 swap same size, but ok 0 = %d\n", swap(sp2,sp3));
  printf("\tTest02 - after sp2\n");
  StackDump(sp2,0);
  printf("\tTest02 - after sp3\n");
  StackDump(sp3,0);

   /*
    * Test04 positive swap - src size is less than dst, will need to increase src
    */
   sp6 = StackCreate(2);  /* src */
   sp7 = StackCreate(4);  /* dst */

   (void)push(sp6,1);
   (void)push(sp6,2);
   printf("\tTest04 - before sp6, size %d\n", size(sp6));
   StackDump(sp6,0);

   (void)push(sp7,3);
   (void)push(sp7,4);
   (void)push(sp7,5);
   (void)push(sp7,4);
   printf("\tTest04 - before sp7, size %d\n", size(sp7));
   StackDump(sp7,0);

   printf("Test04 swap dst size less than src size, but ok 0 = %d\n", swap(sp6,sp7));
   printf("\tTest04 - after sp6, size %d\n", size(sp6));
   StackDump(sp6,0);
   printf("\tTest04 - after sp7, size %d\n", size(sp7));
   StackDump(sp7,0);

   /*
    * Test05 positive swap - dst size is less than src, will need to increase dst
    */
   sp8 = StackCreate(2);  /* dst */
   sp9 = StackCreate(4);  /* src */

   (void)push(sp8,101);
   (void)push(sp8,201);
   printf("\tTest05 - before dst sp8, size %d\n", size(sp8));
   StackDump(sp8,0);

   (void)push(sp9,301);
   (void)push(sp9,401);
   (void)push(sp9,501);
   (void)push(sp9,401);
   printf("\tTest05 - before src sp9, size %d\n", size(sp9));
   StackDump(sp9,0);
   
   printf("Test05 swap dst size less than src size, but ok 0 = %d\n", swap(sp9,sp8));
   printf("\tTest05 - after dst sp8, size %d\n", size(sp8));
   StackDump(sp8,0);
   printf("\tTest05 - after src sp9, size %d\n", size(sp9));
   StackDump(sp9,0);
   
   printf("test_swap - Ends\n");

   StackDestroy(sp );
   StackDestroy(sp1);
   StackDestroy(sp2);
   StackDestroy(sp3);
   StackDestroy(sp4);
   StackDestroy(sp5);
   StackDestroy(sp6);
   StackDestroy(sp7);
   StackDestroy(sp8);
   StackDestroy(sp9);

   return 0;
}
Ejemplo n.º 16
0
foreign_t
make_buffer(void){
event_stack = StackCreate();
return (event_stack) ? TRUE : FALSE;
}