Ejemplo n.º 1
0
int main(int argc, char *argv[]){
    struct Stack *stack = Stack_new(10);
    int i=0;

    Stack_push(stack, 5);
    Stack_push(stack, 50);
    Stack_push(stack, 1000);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);

    for(i=0; i < stack->pos; i++){
        printf("Popped %d from the stack (pos %d)\n", stack->store[i], i);
    }
    Stack_destroy(stack);
    return 0;
}
Ejemplo n.º 2
0
char *test_destroy()
{
  mu_assert(stack != NULL, "Failed to make stack #2");
  Stack_destroy(stack);

  return NULL;
}
Ejemplo n.º 3
0
void die(char *message, struct Stack *stack){
    if(errno){
        perror(message);
    } else {
        printf("ERROR: %s\n", message);
    }
    Stack_destroy(stack);
    exit(1);
}
Ejemplo n.º 4
0
void rb_free(rb_node_t **root)
{
  stack S=NULL;
  S=InitStack(S);
  rb_node_t *p=NULL;
  p = *root;
  Push(S, p);
  while(!StackEmpty(S))
  {

    while(p){
      if(p->lflag != 1 )
      {
        p->lflag = 1;
        if(p->left){
                    Push(S, p->left);
                    p = p->left;
        }
      }else if(p->rflag != 1 )
      {
        p->rflag = 1;
                if(p->right){
                    Push(S, p->right);
                    p = p->right;
                }

      }
      if(p->lflag == 1 && p->rflag == 1){
                break;
      }
    }
    p = Pop(S);
		rb_node_free(&p);	
    if(StackEmpty(S)) {
      break;
    }
    p = GetTop(S);
  }

	*root = NULL;

	Stack_destroy(&S, NULL);
}
Ejemplo n.º 5
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.º 6
0
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;
}
Ejemplo n.º 7
0
void StrList_destroy(StrList* list) {
  assert(list != NULL);
  Stack_destroy(list->stack);
  free(list);
  list = NULL;
}
Ejemplo n.º 8
0
char *test_destroy()
{
    mu_assert(stack != NULL, "destroy: failed");
    Stack_destroy(stack);
    return NULL;
}