Ejemplo n.º 1
0
int main()
{
   stackType stack;
   printf("Initializing stack.\n");
   initializeStack(&stack);

   printf("Pushing: %d\n", 3);
   stackPush(&stack, 3);

   printf("Popping...");
   int x = stackPop(&stack);
   if (x == 3)
     printf("success\n");
   else
     printf("********** ERROR\n");

   printf("Pushing: %d\n", 4);
   stackPush(&stack, 4);
   printf("Pushing: %d\n", 5);
   stackPush(&stack, 5);
   printf("Popping...");
   x = stackPop(&stack);
   if (x == 5)
     printf("success\n");
   else
     printf("********** ERROR\n");

   printf("Pushing: %d\n", 6);
   stackPush(&stack, 6);
   printf("Pushing: %d\n", 7);
   stackPush(&stack, 7);

   printf("Stack contents (should be 7,6,4):\n");
   printStack(&stack);
}
Ejemplo n.º 2
0
Stack* createStack()
{
	Stack* s;
	s = (Stack*) malloc(sizeof(Stack));
	initializeStack(s);
	return(s);
}
Ejemplo n.º 3
0
// This one is public so main can use it.
void initialize()
{
 instrTable.instructionCount = 0; 
 initializeTable(&symbolTable);
 initializeTable(&jumpTable);
 initializeStack(&stack);
}
int evaluate(ParseTreeNode *p_root){
    ParseTreeNode *result_node;
    int val;

    initializeStack();
    initializeQueue(); /* So that the tree will not be changed */

    postOrderTraversal(p_root, &evaluatorHelper);

    result_node = pop();
    val = *(result_node->m_token->m_data_container->m_int);

    /* Free the resources */
    while(!isQueueEmpty()){
        result_node = dequeue();

        free(result_node->m_token->m_data_container);
        result_node->m_token->m_data_container = NULL;

        free(result_node->m_token);
        result_node->m_token = NULL;

        free(result_node);
        result_node = NULL;
    }

    return val;
}
Ejemplo n.º 5
0
Stack::Stack (int size)
{
	maxStackSize = size;
	initializeStack ();

	//allocate array
	//it starts from 0 not 1
	list = new int [maxStackSize - 1];
}
Ejemplo n.º 6
0
/*!
  Creates a stack of the given \a size for the coroutine.
  The memory is owned by the Coroutine object and will be deleted on destruction.

  Calling this function is only valid when in the NotStarted state.

  \sa setStack()
*/
void Coroutine::createStack(int size)
{
    Q_ASSERT(_status == NotStarted);

    if (_stackData)
        free(_stackData);

    _stackData = malloc(size);
    initializeStack(_stackData, size, &entryPoint, &_stackPointer);
}
Ejemplo n.º 7
0
/*!
  Initializes the area given by \a memory and \a size to serve as the stack
  for the coroutine. The Coroutine object does not take ownership.

  Calling this function is only valid when in the NotStarted state.

  \sa createStack()
*/
void Coroutine::setStack(void *memory, int size)
{
    Q_ASSERT(_status == NotStarted);

    if (_stackData) {
        free(_stackData);
        _stackData = 0;
    }

    initializeStack(memory, size, &entryPoint, &_stackPointer);
}
Ejemplo n.º 8
0
int main ()
{
  /* set up stack variable */
  stringStack myStack;
  initializeStack (&myStack);
 
  printf ("program to follow status of string variables during processing\n");
  printf ("progress commentary      a     b     c     d     e     f     g     h\n");

  char a [6] = "apple";
  char b [6] = "beans";
  char c [6] = "corn";
  printf ("initialize a, b, c  ");
  printf (" %5s %5s %5s\n", a, b, c); 

  /* push a, b, c on stack */
  push (&myStack, a);
  push (&myStack, b);
  push (&myStack, c);

  /* pop stack 3 times */
  char * d = pop (&myStack);
  char * e = pop (&myStack);
  char * f = pop (&myStack);
  printf ("pop to get d, e, f  ");
  printf (" %5s %5s %5s %5s %5s %5s\n", a, b, c, d, e, f); 

  /* change a, e */
  strcpy (a, "fruit");
  strcpy (e, "peas");
  printf ("change a, e         ");
  printf (" %5s %5s %5s %5s %5s %5s\n", a, b, c, d, e, f);

  /* create new strings g, h and push on stack */ 
  char g [6] = "Beet";
  char h [6] = "Olive";
  push (&myStack, g);
  push (&myStack, h);

  printf ("push new g, h       ");
  printf (" %5s %5s %5s %5s %5s %5s %5s %5s\n", a, b, c, d, e, f, g, h);

  /* change g */
  strcpy (g, "Dill");
  printf ("change g            ");
  printf (" %5s %5s %5s %5s %5s %5s %5s %5s\n", a, b, c, d, e, f, g, h);

  return 0;
}
void prefixExprToParseTree(ParseTreeNode *p_root, Token *expr[], int p_size){
    int i = 0;
    ParseTreeNode *current_node = p_root;
    Token *token;

    initializeStack();

    for(;i<p_size;i++){
        token = *(expr + i);

        if(0 == strcmp("op", token->m_type)){
                current_node->m_token = token;
                push(current_node);

                if(NULL == current_node->m_left_child)
                    current_node->m_left_child = createParseTreeNode(NULL);

                current_node = current_node->m_left_child;

        }
        else if(0 == strcmp("num", token->m_type)){
                int *val = malloc(sizeof(int));
                *val = atoi(token->m_data_container->m_char);

                current_node->m_token = token;
                current_node->m_token->m_data_container->m_int = val;

                current_node = pop();

                if(NULL == current_node)
                    break;

                if(NULL == current_node->m_right_child)
                    current_node->m_right_child = createParseTreeNode(NULL);

                current_node = current_node->m_right_child;
        }
    }
}
Ejemplo n.º 10
0
void fit(short* individual, int len,
         float* params, int numParams,
         float* X, int numVars,
         double* y, int numPoints){

  eval_data fdata;
  fdata.X = X;
  fdata.individual = individual;
  fdata.len = len;
  fdata.num_p = numParams;
  fdata.num_x = numVars;
  
  Stack S;
  initializeStack(&S, (float*)malloc(sizeof(float)*len));
  fdata.S = &S;

  LMstat lmstat;
  levmarq_init(&lmstat);

  levmarq(numParams, params,
          numPoints, y, NULL,
          &func, &grad, &fdata,
          &lmstat);
}
Ejemplo n.º 11
0
int main(){
  
  //short individual[len] = {-1, 0, -4};
  short individual[len] = {CONST_ID, CONST_ID, 0, MUL_ID, ADD_ID, CONST_ID, 0, MUL_ID, 0, MUL_ID, ADD_ID};
  float x[numVars] = {2};
  float params[numParams] = {1,2,3};
  Stack S; float stackArray[11];
  initializeStack(&S, stackArray);
  printf("%g\n", eval(individual, len, x, params, &S));
  return 0;
/*   float gen_params[numParams] = {1}; */
/*   double y[numPoints]; float X[numPoints]; */
/*   for (int i=0; i<numPoints; i++){ */
/*   X[i] = i; */
/*   y[i] = gen_params[0]*X[i]; */
/* } */
/*   float params[numParams] = {10}; */
/*   fit(individual, len, params, numParams, X, numVars, y, numPoints); */
/*   for(int i=0; i<numParams; i++){ */
/*   printf("%g ", params[i]); */
/* } */
/*   printf("\n"); */
/*   return 0; */
}
Ejemplo n.º 12
0
int main() {
	Stack *s;
	int i, removed, returned, e=5;

	s = createStack();
	initializeStack(s);
	for (i=0; i<10; i++)
	{
		if(push(s, 5+i+1) == true)
		{
		printf("O valor %d foi inserido!\n", 5+i+1);
		}
		else
		{
		printf("O valor %d não pôde ser inserido.\n", 5+i+1);
		}
	}

	for (i=0; i<7; i++)
	{
		if (pop(s, &removed)==true)
		{
			printf("Topo removido com sucesso, o valor era: %d\n", removed);
		}
		else
		{
			printf("Não foi possível remover o topo\n");
		}
	}

	if (top(s, &returned) == true)
	{
		printf("O valor do topo é: %d\n", returned);
	}
	else
	{
		printf("Não há nada no topo.\n");
	}
	
	printStack(s);

	if (containsStack(s, &e) == false)
	{
		printf("O valor 5 não está presente na pilha.\n");
	}
	else
	{
		printf("O valor 5 está presente na pilha.\n");
	}

	e++;
	if (containsStack(s, &e) == false)
	{
		printf("O valor 6 não está presente na pilha.\n");
	}
	else
	{
		printf("O valor 6 está presente na pilha.\n");
	}

	printf("Tamanho da pilha: %d\n",sizeStack(s));

	if (isEmptyStack(s) == true)
	{
			printf("A pilha está vazia.\n");
	}
	else
	{
		printf("A pilha não está vazia.\n");
	}
return(0);	
}