예제 #1
0
파일: queue.c 프로젝트: muzixing/c_project
queue_t * queue_create()
{
	queue_t * queue = (queue_t *)malloc(sizeof(queue_t));
	queue->in = stack_create();
	queue->out = stack_create();
	return queue;
}
예제 #2
0
파일: fifo.c 프로젝트: zhangst/exercise
struct fifo * fifo_create(int max_elements) {
    struct fifo * tmp = NULL;
    max_elements = (max_elements < MIN_ELEMENTS)?MIN_ELEMENTS:max_elements;

    tmp = malloc(sizeof(struct fifo));
    if (tmp == NULL) {
        fprintf(stderr, "%s:%d malloc error.\n",
                __func__, __LINE__);
        return NULL;
    }
    
    tmp->s[0] = stack_create(max_elements);
    if (tmp->s[0] == NULL) {
        fprintf(stderr, "%s:%d stack_create error.\n",
                __func__, __LINE__);
        free(tmp);
        return NULL;
    }
    tmp->s[1] = stack_create(max_elements);
    if (tmp->s[1] == NULL) {
        fprintf(stderr, "%s:%d stack_create error.\n",
                __func__, __LINE__);
        stack_dispose(tmp->s[0]);
        free(tmp);
        return NULL;
    }
    tmp->capacity = max_elements;
    tmp->size = EMPTY_SIZE;

    return tmp;
}
예제 #3
0
파일: 3_5.c 프로젝트: hmeng-19/CUtils
int main() {
	struct stack *s = stack_create();
	assert(is_empty(s));
	push(s, 5);
	push(s, 20);
	push(s, 1);
	push(s, 20);
	push(s, 12);
	push(s, 12);
	push(s, 12);
	push(s, 39);
	push(s, 20);

	//sort_stack(s);
	sort_stack_1(s);

	fprintf(stdout, "the sorted stack are:\n");
	while(!is_empty(s)) {
		fprintf(stdout, "%d\n", pop(s));
	}

	assert(is_empty(s));
	stack_destroy(s);
	return 0;
}
예제 #4
0
파일: 3_5.c 프로젝트: hmeng-19/CUtils
int sort_stack(struct stack *s) {
	struct stack *t = stack_create();

	while(!is_empty(s)) {
		int min = top(s), n = 0;
		int i;
		int c = 0;
		while(!is_empty(s)) {
			int k = pop(s);
			if(k < min) min = k;
			push(t, k);
			c++;
		}

		for(i=0; i<c; i++) {
			int k = pop(t);
			if(k == min) n++;
			else push(s, k);
		}

		for(i=0; i<n; i++) push(t, min);
	}

	while(!is_empty(t)) {
		push(s, pop(t));
	}
	stack_destroy(t);
	return 0;
}
예제 #5
0
static char *test_stack_create(void)
{
    struct stack *stack;
    stack = stack_create(sizeof(int));
    mu_assert("stack_create returned a NULL pointer\n", stack != NULL);
    return 0;
}
예제 #6
0
파일: test.c 프로젝트: royye62/mydemo
int main(void)
{
    int a[10];
    int i;
    int data;

    rand_a(a, 10);
    show_a(a, 10);

    stack_t *st = stack_create(sizeof(int), NULL);

    printf("====== push ======\n");
    for (i = 0; i < 10; i++)
        stack_push(st, &a[i]);

    printf("====== top ======\n");
    stack_top(st, &data);
    printf("top : %d\n", data);

    printf("====== pop ======\n");
    //while (!stack_isempty(st))
    {
        stack_pop(st, &data);
        printf("%d ", data);
    }
    putchar('\n');

    stack_destroy(st);

    return 0;
}
예제 #7
0
파일: test.c 프로젝트: royye62/mydemo
int main(void)
{
	int a[10];
	int i;
	stack_t *st;
	int data;

	rand_a(a, 10);
	show_a(a, 10);

	st = stack_create(sizeof(int), 10, NULL);

	printf("=== push ===\n");
	for (i = 0; i < st->max; i++ )
		stack_push(st, &a[i]);
	
	int top;
	stack_top(st, &top);
	printf("top is %d\n", top);

	printf("=== pop ===\n");
	while (!stack_isempty(st))
	{
		stack_pop(st, &data);	
		printf("%2d ", data);
	}
	putchar('\n');

	return 0;
}
int main (int argc, const char * argv[]) {	
	
	//Stack
	
	int numbers[NUMLEN] = {
		15, 10, 5, 100
	};
	
	int i;

	Stack *stack = stack_create();
	
	for(i = 0; i < NUMLEN; i++) {
		printf("pushing: %u\n", numbers[i]);
		stack_push(stack, numbers[i]);
	}
	
	printf("\n");

	stack_print(stack);

	while(i--) {
		unsigned int result = stack_pop(stack);

		if(UINT_MAX == result) {
			break;
		}
		
		printf("popping: %u\n", result);
	}
	
	stack_destroy(stack);
	
	//Queue
	
	Queue *queue = queue_create();
	
	for(i = 0; i < NUMLEN; i++) {
		printf("\nenqueueing: %u", numbers[i]);
		queue_enqueue(queue, numbers[i]);
	}
	
	queue_print(queue);
	
	while(i--) {
		unsigned int result = queue_dequeue(queue);
		
		if(UINT_MAX == result) {
			break;
		}
		
		printf("dequeuing: %u\n", result);
	}
	
	queue_print(queue);

	queue_destroy(queue);

	return 0;
}
예제 #9
0
파일: m_stack.c 프로젝트: proffK/iLab
stack* stack_expansion(stack* stk, int new_size){
	double* buf = NULL;
	int i = 0;	
	stack* new_stack = stack_create(new_size);
	buf = (double*) calloc (stk -> size, sizeof(double*));
		
	if (stack_is_valide(stk)) {
		if (buf) { 
		
			for (i = 0; i <= stk -> head; ++i) {
				buf[i] = stk -> data[i];
			}
			
			stack_delete(stk);
			
			i--;
			
			new_stack -> head = i;
 
			if (new_stack -> head + 1) do new_stack -> data[i] = buf[i]; while (--i + 1);
			
			free(buf);
			buf = NULL;
			
			return new_stack;
		}
		
		errno = ENOMEM;
		return stk;		
	}
		
	return 0;		
}
예제 #10
0
파일: stack.c 프로젝트: pavolzetor/adt
struct Stack * stack_push(struct Stack *stack, int item) {
    struct Stack *new_stack = stack_create();
    new_stack->item = item;
    new_stack->next = stack;

    return new_stack;
}
예제 #11
0
int main () {
    struct Stack *stack;
    stack = stack_create (10);
    char *c = calloc (16, sizeof (char));
    char **str= calloc(1,sizeof(char*));
    int n;

    while(strcmp(c,"=")) {
        scanf("%s", c);
        if (strtol(c, str,  10)) 
            stack_push (stack, strtol(c, str, 10));
        else {
            if (!strcmp(c, "+"))
                 stack_push (stack, stack_pop(stack) + stack_pop (stack));
            if (!strcmp(c, "-")) {
                n = stack_pop(stack) - stack_pop (stack);    
                stack_push (stack, -n);
            }                                                                       
            if (!strcmp(c, "*"))    
                stack_push (stack, stack_pop(stack) * stack_pop (stack));                                                                   
        }       
    }
    printf("%d\n", stack_pop(stack));

    stack_destroy(stack);
    free (str);
    free (c);
    return 0;
}
예제 #12
0
파일: rpncalc.c 프로젝트: nalliso/purduecs
int main(int argc, char ** argv)
{
	if (argc < 2) {
		printf("Usage: rpncalc op1 op2 ...\n");
		exit(1);
	}
	
	STACK * s = stack_create();

	int i;
	char * c;
	for (i = 1; i < argc; i++) {
		if (is_operand(c = argv[i])) {
			if (stack_is_empty(s)) {
				printf("Elements remain in the stack.\n");
				exit(1);
			} else {
				compute_operation(s, c);
			}
		} else {
			stack_push(s, atof(c));
		}
	}
	
	double result = s->top->val;
	stack_pop(s);
	if(stack_is_empty(s)) {
		printf("%f\n", result);
	} else {
		printf("Elements remain in the stack.\n");
	}

	exit(0);
}
예제 #13
0
int main(int argc, char const *argv[])
{
	stack *stk = stack_create();
	int str;
	printf("Please input some thing:\n");
	while((str = getchar())) {
		if(str == 40 || str == 123 || str == 91 || str == 60) // (、{、[、<
			stk = stack_push(stk, (void *)str);
		if(str == 41 || str == 125 || str == 93 || str == 62) { // )、}、]、>
			if(stk->size == 0) {
				printf("Error bracket\n");
				exit(0);
			} else {
				Node *node = stack_pop(stk);
				int tp = (int)node->value;
				if((str == 41 && tp == 40) || (str == 125 && tp == 123) || (str == 93 && tp == 91) || (str == 62 && tp == 60)) {
					printf("Great! Right bracket typed.\n");
				} else {
					printf("Error bracket\n");
					//printf("str=%d, tp=%d\n", str, tp);
					exit(0);
				}
				free(node);
			}
		}
	}
	stack_release(stk);
	printf("Exit. Thank you.\n");
	return 0;
}
예제 #14
0
int
main()
{
	Stack *stack;
	char *line = NULL;
	size_t len = 0;
	ssize_t read;

	if ((stack = stack_create()) == NULL) {
		fprintf(stderr, "Cannot create stack!\n");
		exit(EXIT_FAILURE);
	}

	while ((read = getline(&line, &len, stdin)) != -1) {
		if ((stack_push(stack, strdup(line))) == NULL) {
			fprintf(stderr, "Cannot push onto stack!\n");
			exit(EXIT_FAILURE);
		}
	}

	free(line);

	while ((line = stack_pop(stack)) != NULL) {
		printf("%s", line);
	}

	stack_destroy(stack);
}
예제 #15
0
파일: main.c 프로젝트: GedRap/algorithms
/**
 * Demonstration of the stack implementation
 */
int main() {
    Stack_Item *item1 = stack_item_create(1);
    Stack_Item *item2 = stack_item_create(3);
    printf("[DEBUG] Stack Item 2 at %p\n", item2);
     
    Stack *stack = stack_create(5);
    stack_dump(stack);

    stack_push(stack, item1);
    stack_push(stack, item2);

    printf("Stack after pushing\n");
    stack_dump(stack);
    
    Stack_Item *popped = stack_pop(stack);
    printf("[DEBUG] Popped item at %p (expected %p)\n", popped, item2);

    int popped_value = stack_item_get_val(popped);

    printf("[DEBUG] Expected to pop 3, popped %d\n", popped_value);

    printf("[DEBUG] Stack after popping\n");
    stack_dump(stack); 
    return 1;
}
예제 #16
0
파일: main.c 프로젝트: jereksel/DataTypes
int main()
{

	Stack *stack = stack_create();

	assertEqual(true, stack_empty(stack));


	stack_push(stack, integerWithNumber(0));
	stack_push(stack, integerWithNumber(1));
	stack_push(stack, integerWithNumber(2));
	stack_push(stack, integerWithNumber(3));
	stack_push(stack, integerWithNumber(4));


	assertEqual(false, stack_empty(stack));


	stack_push(stack, integerWithNumber(5));


	assertEqual(5, getFromStack(stack)->number);
	assertEqual(4, getFromStack(stack)->number);
	assertEqual(3, getFromStack(stack)->number);
	assertEqual(2, getFromStack(stack)->number);
	assertEqual(1, getFromStack(stack)->number);
	assertEqual(0, getFromStack(stack)->number);

	assertEqual(true, stack_empty(stack));


	return 0;
}
예제 #17
0
파일: main.c 프로젝트: laurofigueroa/EDyA1
int main() {
	Stack *pila1;
	pila1 = stack_create();
	
	stack_data(pila1)[0] = 2;
	stack_data(pila1)[1] = 8;
	stack_data(pila1)[2] = 14;
	stack_back(pila1) = 2;

	pila1 = stack_push(pila1,4);



	pila1 = stack_pop(pila1);
	stack_print(pila1);	

	int i;
	for(i = 0; i<=stack_back(pila1) && stack_back(pila1) != stack_maxstack(pila1) ; i++)
				stack_data(pila)[i] = 3;



	pila1 = stack_reverse(pila1);
	stack_print(pila1);

	stack_destroy(pila1);
	


}
예제 #18
0
int 
bracket_match(const char* buf, int buf_len)
{
  int i, ret = 0;
  char ch;
  stack_t s;
  
  if (NULL == buf || buf_len <= 0) {
    fprintf(stderr, "arguments invalid ...");
    exit(0);
  }

  s = stack_create(128);
  for (i = 0; i < buf_len; ++i) {
    ch = buf[i];

    if ('(' == ch || '[' == ch)
      stack_push(s, ch);
    else if (')' == ch) {
      if (stack_empty(s) || '(' != stack_pop(s))
        break;
    }
    else if (']' == ch) {
      if (stack_empty(s) || '[' != stack_pop(s))
        break;
    }
    else
      break;
  }
  if (stack_empty(s))
    ret = 1;
  stack_delete(&s);

  return ret;
}
예제 #19
0
int main(void)
{
	int i;
	int* data[10];
	Stack stack;

	// 10 test data
	for (i = 0; i< 10; i++)
	{
		data[i] = (int*)malloc(sizeof(int));
		*data[i] = i;
	}

	stack = stack_create(debug_free);

	//push data pointer
	for (i = 0;i < 10 ;i++)
	{
		stack_push(stack,data[i]);
		stack_debug(stack);
	}
	

	//pop data pointer
	for (i = 0; i < 8 ;i++)
	{
		stack_pop(stack,&data[i]);
		free(data[i]);
		stack_debug(stack);
	}

	stack_destroy(stack);

	return 0;
}
예제 #20
0
파일: test.c 프로젝트: chengw1005/alg_ex
void test_stack()
{
    printf("\ntesting stack\n");
    
    int c = 16;
    struct stack* s = stack_create(c);

    int num = 200;
    for (int i = 0; i < num; ++i) {
        stack_push(s, i);
    }

    while (!stack_empty(s)) {
        printf("%d ", stack_pop(s));
    }

    printf("\nresize the stack and test it again\n");

    for (int i = 0; i < num; ++i) {
        stack_push(s, i);
    }

    stack_resize(s, 128);
    for (int i = 0; i < num; ++i) {
        stack_push(s, i);
    }

    while (!stack_empty(s)) {
        printf("%d ", stack_pop(s));
    }

    stack_destroy(s);
}
예제 #21
0
void    data_cache_init()
{
    if(NULL == data_cache_storage)
    {
        data_cache_storage = stack_create();
    }
}
예제 #22
0
/*
 * 1-not cycle  0-cycle  -1-error
 * */
int topsort(mgraph * mg) {
	linkstack s;

	s = stack_create();
	if (s == NULL) {
		return -1;
	}
}
예제 #23
0
testcase testcase_create(uint num_dominos) {
	uint i;
	testcase testcase = malloc(sizeof(testcase_t));
	testcase->num_dominos = num_dominos;
	testcase->drops = malloc(num_dominos*sizeof(stack));
	for(i=0; i<num_dominos; i++) {
		testcase->drops[i] = stack_create();
	}
	return testcase;
}
예제 #24
0
파일: stack.c 프로젝트: lukesandberg/Regex
stack* stack_reverse(stack* stk)
{
	stack* nstk = stack_create();
	list_entry* e = stk->top;
	while(e != NULL)
	{
		stack_push(nstk, e->val);
		e = e->next;
	}
	return nstk;
}
예제 #25
0
int main()																		//MAIN
{																				//
struct stack * s1; 																//deklaracja, ¿e s1 to typ stack  
s1 = stack_create(1);															//tworzy stos
	
//z->dane=111;
//s1->data = 11;
//printf("aaa");  
stack_delete(s1); 																//usuniêcie s1 \\\\\\podaje zmienn¹
system("pause");  																//przytrzymanie programu
return 0;																		//zwrócenie 0
}
예제 #26
0
static char *test_stack_pop(void)
{
    struct stack *stack;
    void *data;

    mu_assert("stack_pop returned non-NULL on NULL\n", stack_pop(NULL) == NULL);

    stack = stack_create(sizeof(int));
    data = stack_pop(stack);
    mu_assert("stack_pop did not return NULL on empty\n", data == NULL);
    return 0;
}
예제 #27
0
파일: main.c 프로젝트: jrocketfingers/asp
int main(void)
{
    unsigned short preffered_stack_size;
    unsigned short input = 10000;
    unsigned int value;

    printf("========== STATIC ARRAY STACK IMPLEMENTATION ==========\n");
    printf("=== Input your prefered stack size (< %d): ", MAX_STACK);
    scanf("%d", &preffered_stack_size);

    stack_t *stack = stack_create(preffered_stack_size);

    render_menu();

    while(input != 0)
    {
        printf("=== Choose an option: ");

        scanf("%d", &input);

        switch(input) {
            case 1:
                printf("=== Value to be pushed: ");
                scanf("%d", &value);
                push(stack, value);
                break;
            case 2:
                printf("=== Value: %d\n", pop(stack));
                break;
            case 3:
                printf("=== Value %d\n", peek(stack));
                break;
            case 4:
                printf("=== Stack usage: %d\n", memused(stack));
                break;
            case 5:
                printf("=== Stack size: %d\n", stack_size(stack));
                break;
            case 9:
                render_menu();
                break;
            case 0:
                break;
            default:
                printf("Wrong choice!\n");
                break;
        }
    }

    free(stack);

    return 0;
}
예제 #28
0
/**
 * Remove the item at position 'key/Len' as well as all the empty
 * nodes that are on the way.
 *
 * Parameters:
 * - iSingle, if 1 remove a single key otherwise, remove the key and
 *   all the keys under.
 */
int radix_tree_remove(gds_radix_tree_t * tree, uint32_t key,
		      uint8_t key_len, int iSingle)
{
  gds_stack_t * stack= stack_create(tree->key_len);
  uint8_t uLen= key_len;
  _radix_tree_item_t ** ptree_item= &tree->root;
  int iEmpty;
  
  while (uLen > 0) {
    if (*ptree_item == NULL)
      return -1;
    if (key & (1 << (tree->key_len-(key_len+1-uLen)))) {
      if ((*ptree_item)->right != NULL) {
	stack_push(stack, ptree_item);
	ptree_item= &(*ptree_item)->right;
      } else
	return -1;
    } else {
      if ((*ptree_item)->left != NULL) {
	stack_push(stack, ptree_item);
	ptree_item= &(*ptree_item)->left;
      } else
	return -1;
    }
    uLen--;
  }
  if ((*ptree_item == NULL) || ((*ptree_item)->data == NULL))
    return -1;

  /* Keep information on the current key's emptiness. The key is
     considered empty if it has no child and has no item. */
  iEmpty= (((*ptree_item)->left == NULL)
	   && ((*ptree_item)->right == NULL));

  radix_tree_item_destroy(ptree_item, tree->fDestroy, iSingle);

  /* If the current item is empty (no key below, go up towards the
     radix-tree's root and clear keys until a non-empty is found. */
  while (iEmpty && (stack_depth(stack) > 0)) {
    ptree_item= (_radix_tree_item_t **) stack_pop(stack);

    /* If the key is empty (no child and no item), remove it. */
    if (((*ptree_item)->left == NULL) &&
	((*ptree_item)->right == NULL) &&
	((*ptree_item)->data == NULL)) {
      radix_tree_item_destroy(ptree_item, tree->fDestroy, 1);
    } else
      break;
  }
  stack_destroy(&stack);
  return 0;
}
예제 #29
0
int main(void)
{
    Stack stk = stack_create();
    printf("%d\n", stack_size(stk));
    stack_push(stk, 4);
    printf("%d\n", stack_size(stk));
    printf("%d\n", stack_top(stk));
    stack_pop(stk);
    printf("%d\n", stack_size(stk));
    stack_destroy(stk);

    return 0;
}
예제 #30
0
void container_initialize(void)
{
	if(containerInitialized == 0)
	{
		containerInitialized = 1;
		returnAddressStack = stack_create();
		//containerTable = (container *)malloc(size * sizeof(container));
	}
	else 
	{
		exit(printf("\n\n Runtime ERROR ('initializeContainers' called twice"));		
	}
}