コード例 #1
0
ファイル: ec3.c プロジェクト: OlegIvanov/lcthw
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;
}
コード例 #2
0
ファイル: Institution.c プロジェクト: XxShenglixX/Institution
/**
 * 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 ;
}
コード例 #3
0
ファイル: stack_tests.c プロジェクト: txus/terrorvm
char *test_create()
{
  stack = Stack_create();
  mu_assert(stack != NULL, "Failed to create stack.");

  return NULL;
}
コード例 #4
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);		
}
コード例 #5
0
ファイル: teststack.c プロジェクト: lljllj122/cinterview
char *test_create()
{
    stack = Stack_create();
    mu_assert(stack != NULL, "creat: failed");

    return NULL;
}
コード例 #6
0
ファイル: main.c プロジェクト: RhinoDevel/MtStack
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;
}
コード例 #7
0
ファイル: ex17-2.c プロジェクト: AlBannaTechno/Tutorials
int main(int argc, char *argv[])
{
    struct Stack *stk = Stack_create();

    int i = 0;
    int value = 0;
    for (i = 0; i < MAX_SIZE; ++i) {
        printf("stack %d: %d\n", i, stk->data[i]);
    }
    printf("Stack size: %d\n", stk->cur_size);

    for (i = 0; i < 20; ++i) {
        if (Stack_push(stk, i+5)) {
            printf("Successfully added %d to stack.\n", i+5);
        }
        else {
            printf("Error: Stack overflow\n");
            break;
        }
    }

    printf("Stack size: %d\n", stk->cur_size);
    if (Stack_push(stk, 250)) {
        printf("Well, this shouldn't have happened, guess it broke when pushing.\n");
    }
    else {
        printf("Yay! Pushing didn't work!\n");
    }

    for (i = 0; i < 20; ++i) {
        value = Stack_pull(stk);
        if (value) {
            printf("Successfully removed %d from stack.\n", value);
        }
        else {
            printf("Error: stack underflow\n");
            break;
        }
    }

    printf("Stack size: %d\n", stk->cur_size);
    value = Stack_pull(stk);
    if (value) {
        printf("Well, this shouldn't have happened, guess it broke when pulling.\n");
    }
    else {
        printf("Yay! Pulling didn't work!\n");
    }

    Stack_delete(stk);
    return 0;
}
コード例 #8
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) ;	  
}
コード例 #9
0
ファイル: JsonStateInput.c プロジェクト: RhinoDevel/MtJson
struct JsonStateInput * JsonStateInput_create(char * const inStr, bool const inTakesOwnership)
{
    struct JsonStateInput * const retVal = malloc(sizeof *retVal);
    struct JsonStateInput const buf = (struct JsonStateInput)
        {
            .str = inStr,
            .len = strlen(inStr),
            .takesOwnership = inTakesOwnership,

            .i = 0,
            .stack = Stack_create(false),
            .root = NULL,
            .pos = NULL
        };

    assert(inStr!=NULL);
    assert(retVal!=NULL);

    memcpy(retVal, &buf, sizeof *retVal);

    return retVal;
}

void JsonStateInput_delete(struct JsonStateInput * const inJsonStateInput)
{
    assert(inJsonStateInput!=NULL);

    if(inJsonStateInput->takesOwnership)
    {
        free(inJsonStateInput->str);
    }
    if(inJsonStateInput->root!=NULL)
    {
        JsonEle_delete(inJsonStateInput->root);
    }
    Stack_delete(inJsonStateInput->stack);
    free(inJsonStateInput);
}
コード例 #10
0
ファイル: stack.c プロジェクト: brianium/adventures-inc
int main(int argc, char *argv[])
{
    struct Stack *stack = Stack_create(250);

    Stack_push(stack, "David");
    Stack_push(stack, "Raymond");
    Stack_push(stack, "Bryan");
    printf("length: %d\n", Stack_length(stack));
    printf("%s\n", Stack_peek(stack));
    char *popped = Stack_pop(stack);
    printf("The popped element is: %s\n", popped);
    printf("%s\n", Stack_peek(stack));
    Stack_push(stack, "Cynthia");
    printf("%s\n", Stack_peek(stack));
    Stack_clear(stack);
    printf("length: %d\n", Stack_length(stack));
    printf("%s\n", Stack_peek(stack));
    Stack_push(stack, "Clayton");
    printf("%s\n", Stack_peek(stack));

    Stack_destroy(stack);    
    return 0;
}
コード例 #11
0
ファイル: answer10.c プロジェクト: CindyCKL/ece264-fall2013
/**
 * 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]);
    }

}
コード例 #12
0
/**
 * @if conly
 * @memberof ASTNode_t
 * @endif
 */
LIBSBML_EXTERN
ASTNode_t *
SBML_parseFormula (const char *formula)
{
  long rule, state, action;

  ASTNode_t *node = NULL;

  FormulaTokenizer_t *tokenizer = NULL; 
  Stack_t            *stack     = NULL;
  Token_t            *token     = NULL;

  if (formula == NULL) return NULL;
  
  tokenizer = FormulaTokenizer_createFromFormula(formula);
  token     = FormulaTokenizer_nextToken(tokenizer);
  stack     = Stack_create(20);


  Stack_push(stack, (void *) START_STATE);

  while (1)
  {
    state  = (long) Stack_peek(stack);
    action = FormulaParser_getAction(state, token);

    if (action == ACCEPT_STATE)
    {
      node = Stack_peekAt(stack, 1);
      break;
    }

    else if (action == ERROR_STATE)
    {
      /**
       * Free ASTNodes on the Stack, skip the states.
       */
      while (Stack_size(stack) > 1)
      {
        Stack_pop(stack);
        ASTNode_free( Stack_pop(stack) );
      }

      node = NULL;
      break;
    }

    /**
     * Shift
     */
    else if (action > 0)
    {
      Stack_push( stack, ASTNode_createFromToken(token) );
      Stack_push( stack, (void *) action );

      Token_free(token);
      token = FormulaTokenizer_nextToken(tokenizer);
    }

    /**
     * Reduce
     */
    else if (action < 0)
    {
      rule  = -action;
      node  = FormulaParser_reduceStackByRule(stack, rule);
      state = (long) Stack_peek(stack);

      Stack_push( stack, node );
      Stack_push( stack, (void *) FormulaParser_getGoto(state, rule) );
    }
  }

  FormulaTokenizer_free(tokenizer);
  Stack_free(stack);
  Token_free(token);

  return node;
}