Ejemplo n.º 1
0
int main()
{
    struct Stack * s = Stack_create(true);

    Stack_push(s, Obj_bool_create(true));
    Stack_push(s, Obj_char_create('A'));
    Stack_push(s, Obj_int_create(42));
    Stack_push(s, Obj_double_create(1234.5678));
    printf("POP %f\n", *(double*)Stack_pop(s));
    printf("POP %d\n", *(int*)Stack_pop(s));
    printf("TOP %c\n", *(char*)Stack_top(s));
    printf("TOP %c\n", *(char*)Stack_top(s));

    Stack_clear(s);

    printf("EMPTY? %d\n", (int)Stack_isEmpty(s));

    Stack_push(s, Obj_bool_create(false));
    printf("EMPTY? %d\n", (int)Stack_isEmpty(s));
    Stack_push(s, Obj_char_create('B'));
    printf("POP %c\n", *(char*)Stack_pop(s));
    Stack_push(s, Obj_int_create(666));
    printf("POP %d\n", *(int*)Stack_pop(s));
    Stack_push(s, Obj_double_create(321.654));
    printf("TOP %f\n", *(double*)Stack_top(s));

    Stack_delete(s);
    s = NULL;

    return EXIT_SUCCESS;
}
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
	int stack_capacity;

	if(argc < 2) {
		die("Stack capacity hasn't been specified as parameter.");
	} else {
		stack_capacity = atoi(*(argv + 1));
	}

	if (stack_capacity < 1) die("Stack size is too small.");

	struct Stack *st = Stack_create(stack_capacity);

	int push_value;
	int pop_value;
	
	push_value = 100;
	if(Stack_push(st, push_value)){
		printf("Stack overflow.\n");
	} else {
		printf("%d has been successfully pushed.\n", push_value);
	};
	
	push_value = 10;
	if(Stack_push(st, push_value)){
		printf("Stack overflow.\n");
	} else {
		printf("%d has been successfully pushed.\n", push_value);
	};

	push_value = 1;
	if(Stack_push(st, push_value)){
		printf("Stack overflow.\n");
	} else {
		printf("%d has been successfully pushed.\n", push_value);
	};

	if(Stack_pop(st, &pop_value)) {
		printf("Stack is empty.\n");	
	} else {
		printf("%d has been successfully popped.\n", pop_value);
	};

	if(Stack_pop(st, &pop_value)) {
		printf("Stack is empty.\n");	
	} else {
		printf("%d has been successfully popped.\n", pop_value);
	};

	if(Stack_pop(st, &pop_value)) {
		printf("Stack is empty.\n");	
	} else {
		printf("%d has been successfully popped.\n", pop_value);
	};

	Stack_destruct(st);

	return 0;
}
Ejemplo n.º 3
0
Archivo: IoLexer.c Proyecto: Akiyah/io
TEST_INLINE void IoLexer_popPosBack(IoLexer *self)
{
	intptr_t i = (intptr_t)Stack_pop(self->tokenStack);
	intptr_t topIndex = (intptr_t)Stack_top(self->tokenStack);

	if (i > -1)
	{
		List_setSize_(self->tokenStream, i + 1);

		if (i != topIndex) // ok to io_free token
		{
			IoToken *parent = IoLexer_currentToken(self);

			if (parent)
			{
				IoToken_nextToken_(parent, NULL);
			}
		}
	}

	self->current = Stack_pop(self->posStack);
#ifdef LEXER_DEBUG
	printf("back: "); IoLexer_print(self);
#endif
}
Ejemplo n.º 4
0
Archivo: IoLexer.c Proyecto: Akiyah/io
TEST_INLINE void IoLexer_popPos(IoLexer *self)
{
	Stack_pop(self->tokenStack);
	Stack_pop(self->posStack);
#ifdef LEXER_DEBUG
	printf("pop:	");
	IoLexer_print(self);
#endif
}
Ejemplo n.º 5
0
int main(void)
{
   struct Stack *psStack1;
   struct Stack *psStack2;
   int iSuccessful;

   /* Use a Stack object referenced by psStack1. */
   
   psStack1 = Stack_new();
   if (psStack1 == NULL) handleMemoryError();
   
   iSuccessful = Stack_push(psStack1, 1.1);
   if (! iSuccessful) handleMemoryError();
   
   iSuccessful = Stack_push(psStack1, 2.2);
   if (! iSuccessful) handleMemoryError();

   iSuccessful = Stack_push(psStack1, 3.3);
   if (! iSuccessful) handleMemoryError();

   while (! Stack_isEmpty(psStack1))
      printf("%g\n", Stack_pop(psStack1));

   /***********************************************/
   /* Cannot access the fields of *psStack1 here. */
   /***********************************************/

   Stack_free(psStack1);

   /* Use a Stack object referenced by psStack2. */
      
   psStack2 = Stack_new();
   if (psStack2 == NULL) handleMemoryError();
   
   iSuccessful = Stack_push(psStack2, 4.4);
   if (! iSuccessful) handleMemoryError();
   
   iSuccessful = Stack_push(psStack2, 5.5);
   if (! iSuccessful) handleMemoryError();

   iSuccessful = Stack_push(psStack2, 6.6);
   if (! iSuccessful) handleMemoryError();
   
   while (! Stack_isEmpty(psStack2))
      printf("%g\n", Stack_pop(psStack2));

   /***********************************************/
   /* Cannot access the fields of *psStack2 here. */
   /***********************************************/

   Stack_free(psStack2);

   return 0;
}
Ejemplo n.º 6
0
void cpu_cmp(CPU_t* This) {
    elem_t a = Stack_pop(This);
    elem_t b = Stack_pop(This);
    if (a > b) {
        This->cmp_flag = 1;
    } else if (a == b) {
        This->cmp_flag = 0;
    } else This->cmp_flag = -1;
    Stack_push(This, b);
    Stack_push(This, a);
}
Ejemplo n.º 7
0
void UnlambdaEval_runOnce(UnlambdaEval* self){
  Object *operand, *operator, *q, *r;
  Stack* stack;

  stack = UnlambdaEval_getStack(self);

  operand = Stack_pop(stack);
  operator = Stack_pop(stack);
  q = Stack_pop(stack);
  r = Unlambda_call(operator, operand);
  Stack_push(stack, r);
}
Ejemplo n.º 8
0
int CPU_div(CPU_t* cpu) {

    if ((cpu->stack_cpu->counter) >= 2) {

        int a = Stack_pop(cpu->stack_cpu);
        int b = Stack_pop(cpu->stack_cpu);
        Stack_push(cpu->stack_cpu, b/a);
        return 1;

    }
    else return 0;
}
Ejemplo n.º 9
0
HuffNode *Huffman_Tree_char (FILE *build)
{

  Stack *Stack_values = NULL;
  unsigned char command = fgetc(build);
  
  if (build == NULL)
  {
    return NULL;
  }
  
  do 
  {
    if (command == '1')
    {
      unsigned char val;
      val = fgetc (build);
      
      HuffNode * Tree_Node = Huffman_Tree_create(val);
      Stack_values = Stack_push(Stack_values, Tree_Node);
    }

    if (command == '0')
    {
      HuffNode *NextNode = Stack_values -> node;
      Stack_values = Stack_pop(Stack_values);
      
      if (Stack_values == NULL)
      {
	return NextNode;
      }
      
      else
      {
        HuffNode * Stack_Next = Stack_values -> node;
	
        Stack_values = Stack_pop(Stack_values);
	
        HuffNode * NewNode = malloc(sizeof(HuffNode));
	
        NewNode -> value = ' '; // doesn't matter
        NewNode -> right = NextNode;
        NewNode -> left = Stack_Next;
	
        Stack_values = Stack_push(Stack_values, NewNode);
      }
    }
    command = fgetc (build);
   } while (Stack_values != NULL);
   
  return NULL;
}
Ejemplo n.º 10
0
void cpu_je(CPU_t* This) {
    elem_t a = Stack_pop(This);
    elem_t b = Stack_pop(This);
    if (a > b) {
        This->cmp_flag = 1;
    } else if (a == b) {
        This->cmp_flag = 0;
    } else This->cmp_flag = -1;
    if (This->cmp_flag == 0) {
        cpu_jmp(This);
    }
    This->pc++;
}
Ejemplo n.º 11
0
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ A pseudo reti routine for scheduler, is called at end of every ISR.
+ Pops PC and SW from system stack and restores them to cpu
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
void Scheduler_reti(CPU_p cpu){
    //pseudo reti
   Node_p temp_node;
   temp_node = Stack_pop(cpu->sys_stack); 
   if(temp_node){
    cpu->sw = (unsigned int)temp_node->data;
    free(temp_node);
   }
   temp_node = Stack_pop(cpu->sys_stack);   
   if(temp_node){
    cpu->pc = (unsigned int)temp_node->data;
    free(temp_node);
   }
}
Ejemplo n.º 12
0
void DeleteUnlambdaEval(UnlambdaEval* self){
  Stack* stack;
  Object* obj;
  Memory* memory;

  stack = UnlambdaEval_getStack(self);
  obj = Stack_pop(stack);
  while(obj){
    DeleteObject(obj);
    obj = Stack_pop(stack);
  }

  memory = UnlambdaEval_getMemory(self);
  Memory_mark(memory, stack);
  Memory_sweep(memory);
}
Ejemplo n.º 13
0
/**
 *  Reverse the order of element
 *  Input:
 *		inputList is a list of institutions
 *		outputList is a list of institutions in reversed order
 *	Return:
 *		the number of element reversed
 */
int Institution_reverse(LinkedList *inputList, LinkedList *outputList)
{
	Institution *institute;
	Stack *stack = Stack_create();

	do
	{
		institute = List_removeHead(inputList);
		if(institute != NULL)
		{
			//printf("Name1 : %s \n", institute->name);
			Stack_push(stack, institute);
		}
		else
			printf("Address : %p \n", institute);

		}while(institute != NULL);

	do
	{
		institute = Stack_pop(stack);
		if(institute != NULL)
		{
			//printf("Name : %s \n", institute->name);
			List_addTail(outputList, institute);
		}
		else
			printf("Address : %p \n", institute);

	}while(institute != NULL);		
}
Ejemplo n.º 14
0
char *test_pop_push()
{
    int i;
    for (i = 0; i < NUMS; i++) {
        Stack_push(stack, tests[i]);
        mu_assert(Stack_peek(stack) == tests[i], "wrong value");
    }

    mu_assert(Stack_count(stack) == NUMS, "wrong count");

    StackNode *v = NULL;

    for (v = stack->top; v != NULL; v = v->prev) {
        debug("Val: %s", (char *)v->data);
    }

    for (i = NUMS -1; i >= 0; i--) {
        char *val = Stack_pop(stack);
        mu_assert(val == tests[i], "wrong value");
    }

    mu_assert(Stack_count(stack) == 0, "wrong count after poping");

    return NULL;
}
Ejemplo n.º 15
0
static inline uint32_t get_unmapped_ID()
{
        uint32_t *ID_ptr = Stack_pop(um->unmapped_IDs);
        uint32_t ID = *ID_ptr;
        FREE(ID_ptr);
        return ID;
}
Ejemplo n.º 16
0
int Institution_select(LinkedList *inputList, LinkedList *outputList, void *criterion, int(*compare)(void *, void*)){
	int i;
	Institution *tempInstitution;
	int counter = 0;
	if(inputList->head==NULL)
	{
		return 0;
	}
	//Look for institution which is the particular type
	while((tempInstitution = (Institution*)List_removeHead(inputList)) !=NULL)
	{
		//Compare the institution with the institution type
		if(compare(tempInstitution,criterion))
		{	
			Stack_push(&stack,tempInstitution);
			counter++;
			
		}
	}
	for(i=0;i<counter;i++)
	{
		tempInstitution=Stack_pop(&stack);
		List_addTail(outputList,tempInstitution);
	}
	return counter;
	
	
}
Ejemplo n.º 17
0
/* 
 * Input:   Mem_T structure, unsigned integer
 * Output:  returns the index at which the segment was stored.
 * Purpose: Adds a segment to memory with numWords words. 
 */
int map_segment(Mem_T memory, unsigned numWords)
{
        unsigned segIndex;
        Seg_T segment = Seg_new(numWords);

        if(!(Stack_is_empty(memory->reusable_indices))){
                segIndex = Stack_pop(memory->reusable_indices);
                Mem_arr_put(memory->segment_seq, segment, segIndex);
        }

        else{ 
                segIndex = Mem_arr_length(memory->segment_seq);
                memory->segment_seq = Mem_arr_addhi(memory->segment_seq, segment);
        }        

        return segIndex;

        // if(!(Stack_is_empty(memory->reusable_indices))){
        //         segIndex = Stack_pop(memory->reusable_indices);
        // }
        // else{
        //         segIndex = Mem_arr_length(memory->segment_seq);
        // }
        // memory->segment_seq = Mem_arr_put(memory->segment_seq, segment, segIndex);
        // return segIndex;

}
Ejemplo n.º 18
0
/**
	Question 1
**/
int Institution_reverse(LinkedList *inputList, LinkedList *outputList){
	int i;
	Institution *tempInstitution;
	int counter =0 ; 
	
	
	if(inputList->head ==NULL)
	{
		return 0;
	}
	while ((tempInstitution = (Institution*)List_removeHead(inputList)) !=NULL)
	{
		//push Element 1
		Stack_push(&stack,tempInstitution);
		counter ++;
	}
	
	for(i=0;i<counter;i++)
	{
		tempInstitution = Stack_pop(&stack);
		List_addTail(outputList,tempInstitution);
		
		
	}
	return counter;
}
Ejemplo n.º 19
0
uint32_t get_unmapped_ID(UM_state um)
{
        uint32_t *ID_ptr = Stack_pop(um->unmapped_IDs);
        uint32_t ID = *ID_ptr;
        FREE(ID_ptr);
        return ID;
}
Ejemplo n.º 20
0
/**
 * Reverse the order of element
 * Input :
 *		inputList	is a list of institution
 *		outputList	is a list of institution in reversed order
 *
 * Return :
 *		the number of element reversed
 */
int Institution_reverse(LinkedList *inputList,LinkedList *outputList)
{
	int counter = 0, firstCount = 1 ;
	Institution *input ,*output ;

	Stack *newStack = Stack_create();
	
	do
	{
		input = (Institution *)List_removeHead(inputList);
		if(firstCount == 1 && input == NULL)
			{
				outputList = NULL ;
				return 0;
			}
		else 
			firstCount = 0 ;
		if(input!= NULL)	
			Stack_push(newStack,input);
	}while (input != NULL);
	
	do
	{
		output = (Institution *)Stack_pop(newStack);
		if(output !=NULL)
			{
				List_addTail(outputList,output);
				counter ++ ;
			}
	}while (output != NULL);
	

	
	return counter ;
}
Ejemplo n.º 21
0
Archivo: w04.c Proyecto: Faur/C_basics
int main(){

	int rc, num_arg, key, i;
	char command[WORD_SIZE];
	struct Stack *head = NULL;

	rc = get_ints(1, &num_arg);
	check(rc == 0, "Couldn't read num_arg");

	for(i = 0; i < num_arg; i++){
		rc = read_word(WORD_SIZE, command);
		check(rc == WORD_SIZE-1, "Didn't read key properly; i = %d", rc);

		if(! strcmp(command, "PU")){
			rc = get_ints(1, &key);
			debug("key = %d", key);
			head = Stack_push(key, head);
			check_mem(head);
		} else if(! strcmp(command, "PO")){
			Stack_print_head(head);
			head = Stack_pop(head);
		}else {
			printf("command = '%s'.", command);
			log_err("main: bad 'command' argument,");
		}
		
	}

	debug("Stack_destroy_all(head)");
	Stack_destroy_all(head);
	return 0;
error:
	if(head) Stack_destroy_all(head);
	return 1;
}
Ejemplo n.º 22
0
AP_T pop(void) {
	if (!Stack_empty(sp))
		return Stack_pop(sp);
	else {
		Fmt_fprint(stderr, "?stack underflow\n");
		return AP_new(0);
	}
}
Ejemplo n.º 23
0
//This function reads the char-based input file and then creates a huffman tree.
// It Returns a structure of HuffNode type.
HuffNode *Huff_CharRead(char * filename)
{
    int command = 0;
    FILE *fptr = NULL;
    fptr = fopen(filename,"r");
    if(fptr == NULL)
    {
	return NULL;
    }
    
    Stack *st = NULL;
    while((command = fgetc(fptr)) != EOF)// This loop opens the file till its end.
    {
	
	if(command == '1')// THis statement checks whether the command is 1 or not.
	{
	    command = fgetc(fptr);
	    if(command == EOF)
	    {
		
		return NULL;
	    }
	    st = Stack_push(st,HuffNode_create(command));
	}
	if(command == '0') 
	{
	    HuffNode * A = st -> node;
	    st = Stack_pop(st);
	    if (st == NULL)
	    {
		return A;// Tree is complete here 
	    }
	    else
	    {
		HuffNode * B = st -> node;
		st = Stack_pop(st);
		HuffNode * par = malloc(sizeof(HuffNode));
		par -> value = ' '; // doesn't matter
		par -> right = A;
		par -> left = B;
		st = Stack_push(st, par);
	    }
	}
    }
    return NULL;
}
Ejemplo n.º 24
0
int cpu_mov(CPU_t* This, unsigned int dest, unsigned int source) {
    if (source) {
        This->R[dest] = This->R[source];
        return 2;
    } else {
        This->R[dest] = Stack_pop(This);
        return 1;
    }
}
Ejemplo n.º 25
0
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Destructor for stack structure 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
void Stack_destructor(Stack_p stack){
    if(stack->head != NULL){
        Node_p temp = Stack_pop(stack);
        Stack_destructor(stack);
        Node_destructor(temp);
        return;
    }
    free(stack);    
}
Ejemplo n.º 26
0
void free_IDs(Stack_T unmapped_IDs)
{
	uint32_t *ID;
	while (!Stack_empty(unmapped_IDs)) {
		ID = Stack_pop(unmapped_IDs);
		FREE(ID);
	}

	Stack_free(&unmapped_IDs);
}
Ejemplo n.º 27
0
int runnable(void){
  Object *operand, *operator, *q;
  Stack* stack;
  Memory* m;

  m = World_getMemory(getWorld());
  stack = (Stack*)(Memory_NthEntry(m, 4)->fTarget); /*FIXME */

  operand = Stack_pop(stack);
  if(!operand){
    return false;
  }
  if(!operand->fProc){
    Stack_push(stack, operand);
    return false;
  }
  operator = Stack_pop(stack);
  if(!operator){
    Stack_push(stack, operand);
    return false;
  }
  if(!operator->fProc){
    Stack_push(stack, operator);
    Stack_push(stack, operand);
    return false;
  }

  q = Stack_pop(stack);
    Stack_push(stack, operator);
    Stack_push(stack, operand);
  if(!q){
    return false;
  }
  if(q->fProc){
    Stack_push(stack, operator);
    Stack_push(stack, operand);
    Stack_push(stack, q);
    return false;
  }
  return true;
}
Ejemplo n.º 28
0
/**
 * Sort 'array' of length 'len' using Donald Knuth's "StackSort"
 *
 * To do this, you must implement the following pseudo-code:
 * (1) Maintain a 'write_index'. This is where you'll write values to
 *     as you sort them. It starts off as zero.
 * (2) Initialize an empty stack
 * (3) For each input value 'x':
 *     (3.a) While the stack is nonempty and 'x' is larger than the 
 *           front 'value' on the stack, pop 'value' and:
 *           (3.a.i) Write 'value' to array[write_index], and 
 *                   increment write_index.
 *     (3.b) Push x onto the stack.
 * (4) While the stack is nonempty, pop the front 'value' and follow
 *     step (3.a.i).
 *
 * The output should be a sorted array if, and only if, the array
 * is stack-sortable. You can find files full of stack-sortable and
 * stack-unsortable arrays in the 'expected' folder.
 */
void stackSort(int * array, int len)
{
  int windex = 0;
  int readindex = 0;

  Stack * tube  = Stack_create() ;

  while(readindex < len)
    {
       /* ListNode *lb  = malloc(sizeof(ListNode)) ; */
       /* lb->value = array[readindex] ; */
       /* lb->next = NULL ; */

      if( tube->list == NULL )
	{
	  Stack_push(tube,array[readindex]) ;
	  /* tube->list = malloc(sizeof(ListNode))  ; */
	  /* tube->list->value = array[readindex] ; */
	  /* tube->list->next = NULL ; */
	}
      else
	{
	  while(tube->list != NULL && tube->list->value < array[readindex])
	    {

	      array[windex] = Stack_pop(tube) ;
	      windex ++ ;
	    }
	  Stack_push(tube , array[readindex]) ;
	}
      readindex ++ ;
     
    }

  while(tube->list != NULL)
    {
      array[windex] = Stack_pop(tube) ;
      windex ++ ;
    }
  Stack_destroy(tube) ;	  
}
Ejemplo n.º 29
0
char* StrList_makePrint(StrList* list, SimbolTable* simbols) {
  assert(list != NULL);
  assert(simbols != NULL);

  char* printfBuild = (char*) malloc(sizeof(char)*200);
  char* percentage;
  char variables[100] = "";
  NodeLink* actual = Stack_pop(list->stack);

  strcpy(printfBuild, "\"");

  while (actual != NULL) {
    if ( strcmp(actual->type, "variable") == 0) {
      Variable* variable = SimbolTable_find(simbols, actual->value);

      if (variable != NULL) {
        strcat(variables, actual->value);
        strcat(variables, ",");
        percentage = StrList_percentage(variable->type);
        strcat(printfBuild, percentage);
      }
    } else { // its a raw string
      strcat(printfBuild, actual->value);
    }
    free(actual);
    actual = Stack_pop(list->stack);
  }

  strcat(printfBuild, "\",");
  strcat(printfBuild, variables);

  size_t size = strlen(printfBuild);

  printfBuild[size-1] = '\0';

  return printfBuild;
}
Ejemplo n.º 30
0
/**
 * Sort 'array' of length 'len' using Donald Knuth's "StackSort"
 *
 * To do this, you must implement the following pseudo-code:
 * (1) Maintain a 'write_index'. This is where you'll write values to
 *     as you sort them. It


 starts off as zero.
 * (2) Initialize an empty stack
 * (3) For each input value 'x':
 *     (3.a) While the stack is nonempty and 'x' is larger than the 
 *           front 'value' on the stack, pop 'value' and:
 *           (3.a.i) Write 'value' to array[write_index], and 
 *                   increment write_index.
 *     (3.b) Push x onto the stack.
 * (4) While the stack is nonempty, pop the front 'value' and follow
 *     step (3.a.i).
 *
 * The output should be a sorted array if, and only if, the array
  * is stack-sortable. You can find files full of stack-sortable and
 * stack-unsortable arrays in the 'expected' folder.
 */
void stackSort(int * array, int len)
{
  int write_index = 0;
  int i = 0;
  int number = 0;
  int value = 0;
  Stack * newstack = Stack_create();

  for(i = 0; i<len; i++)
    {
       number = array[i];
      //   printf("number %d\n", number);
      
      while((newstack->list != NULL) && (number > (newstack->list->value)))
	{
	  value =  Stack_pop(newstack);
	  //if(value>0)
	    array[write_index] = value;
	      (write_index)++;
	  Stack_push(newstack, number); 
	}
      Stack_push(newstack, number);
    }
 
while(newstack->list != NULL)
	  {
	    value = Stack_pop(newstack);
	    array[write_index] = value;
	    (write_index)++;
	  }

  for(i=0; i<len; i++)
    {
      printf("%d\n", array[i]);
    }

}