Пример #1
0
/* 测试用例 */
int main (void) {
	STACK stack;
	stack_init (&stack);
	int i;
	for (i = 0; i < 10; ++i)
		stack_push (&stack, i);
	printf ("%u\n", stack_size (&stack));
	while (! stack_empty (&stack))
		printf ("%d ", stack_pop (&stack));
	printf ("\n");
	/*
	 * 输入任意行字符串,按!结束输入,
	 * 将之前输入的字符串逆向打印
	 */
	char text[1024];
	for (;;) {
		gets (text);
		if (! strcmp (text, "!"))
			break;
		stack_push (&stack, (int)strcpy (malloc (
		(strlen(text)+1)*sizeof(text[0])),text));
	}
	while (! stack_empty (&stack)) {
		puts ((char const*)stack_top (&stack));
		free ((void*)stack_pop (&stack));
	}
	stack_deinit (&stack);
	return 0;
}
Пример #2
0
int
AdtInit(int verbose, struct cfg *cfg, char *args[])
{
	char buf[0xFFFF];
	struct allocator *suba;
	struct stack s;
	struct linkedlist l;
	struct varray va;

	if ((suba = suba_init(buf, 0xFFFF, 1, 0)) == NULL ||
			stack_init(&s, 0, suba) == -1 ||
			linkedlist_init(&l, 0, suba) == -1 ||
			varray_init(&va, sizeof(int), suba) == -1) {
		AMSG("");
		return -1;
	}

	linkedlist_add(&l, "two");
	stack_push(&s, "two");
	linkedlist_add(&l, "three");
	stack_push(&s, "one");
	varray_get(&va, 444);
	stack_push(&s, "three");
	varray_get(&va, 4);
	varray_get(&va, 44);
	linkedlist_add(&l, "one");

	if (varray_deinit(&va) != 0 ||
			linkedlist_deinit(&l, NULL, NULL) != 0 ||
			stack_deinit(&s, NULL, NULL) != 0) {
		AMSG("");
		return -1;
	}

	tcase_printf(verbose, "done ");

	cfg = NULL;
	args[0] = NULL;
    return 0;
}
Пример #3
0
SORT_RET_E sort_quick(void *pv_array,
                      unsigned int ui_ele_num,
                      unsigned int ui_ele_size,
                      SORT_DIRECTION_E e_dir,
                      SORT_RET_E (*pfn_compare)(const void *pv_ele1, const void *pv_ele2))
{
   SORT_RET_E e_ret = eSORT_FAILURE;
   STACK_RET_E e_stack_ret = eSTACK_FAILURE;
   STACK_HDL hdl_stack = NULL;
   INDEX_PAIR_X *px_indices = NULL; 
   void *pv_data = NULL;
   unsigned int ui_stack_size = 0;
   unsigned int ui_partition_index = 0;
   unsigned int ui_low_index = 0;
   unsigned int ui_high_index = 0;

   if((NULL == pv_array)||
      (0 == ui_ele_num)||
      (0 == ui_ele_size)||
      (NULL == pfn_compare))
   {
      e_ret = eSORT_INVALID_ARG;
      goto LBL_SORT_QUICK_RET;
   }

   px_indices  = (INDEX_PAIR_X *)malloc(sizeof(*px_indices));
   if(NULL == px_indices)
   {
      e_ret = eSORT_MEM_FAILURE;
      goto LBL_SORT_QUICK_RET;
   }

   px_indices->ui_low = 0;
   px_indices->ui_high = ui_ele_num - 1;

/*TBD:Improve implementation of MAX_STACK_SIZE*/
   e_stack_ret = stack_init(&hdl_stack,
                            MAX_STACK_SIZE,
                            free,
                            sort_quick_index_pair_debug);
   if(eSTACK_SUCCESS != e_stack_ret)
   {
      e_ret = eSORT_FAILURE;
      goto LBL_SORT_QUICK_RET;
   }

   e_stack_ret = stack_push(hdl_stack,
                            px_indices);
   if(eSTACK_SUCCESS != e_stack_ret)
   {
      e_ret = eSORT_FAILURE;
      goto LBL_SORT_QUICK_RET;
   }
   ui_stack_size = 1;

   while(0 != ui_stack_size)
   {
      pv_data = NULL;
      e_stack_ret = stack_pop(hdl_stack,
                              &pv_data);
      if((eSTACK_SUCCESS != e_stack_ret)||
         (NULL == pv_data))
      {
         e_ret = eSORT_FAILURE;
         goto LBL_SORT_QUICK_RET;
      }
      px_indices = (INDEX_PAIR_X *)pv_data;
      e_ret = sort_quick_partition(pv_array,
                                   ui_ele_size,
                                   px_indices->ui_low,
                                   px_indices->ui_high,
                                   e_dir,
                                   pfn_compare,
                                   &ui_partition_index);
      if(eSORT_SUCCESS != e_ret)
      {
         e_ret = eSORT_FAILURE;
         goto LBL_SORT_QUICK_RET;
      }

      ui_low_index = px_indices->ui_low;
      ui_high_index = px_indices->ui_high;

      if(NULL != px_indices)
      {
         free(px_indices);
         px_indices = NULL;
      }

      if((ui_partition_index - ui_low_index) > 1)
      {
         px_indices  = (INDEX_PAIR_X *)malloc(sizeof(*px_indices));
         if(NULL == px_indices)
         {
            e_ret = eSORT_MEM_FAILURE;
            goto LBL_SORT_QUICK_RET;
         }
         px_indices->ui_low = ui_low_index;
         px_indices->ui_high = ui_partition_index -1;

         e_stack_ret = stack_push(hdl_stack,
                                  px_indices);
         if(eSTACK_SUCCESS != e_stack_ret)
         {
            e_ret = eSORT_FAILURE;
            goto LBL_SORT_QUICK_RET;
         }
      }

      if((ui_high_index - ui_partition_index ) > 1)
      {
         px_indices  = (INDEX_PAIR_X *)malloc(sizeof(*px_indices));
         if(NULL == px_indices)
         {
            e_ret = eSORT_MEM_FAILURE;
            goto LBL_SORT_QUICK_RET;
         }
         px_indices->ui_low =  ui_partition_index +1;
         px_indices->ui_high = ui_high_index;

         e_stack_ret = stack_push(hdl_stack,
                                  px_indices);
         if(eSTACK_SUCCESS != e_stack_ret)
         {
            e_ret = eSORT_FAILURE;
            goto LBL_SORT_QUICK_RET;
         }
      }
      e_stack_ret = stack_size(hdl_stack,
                               &ui_stack_size);
      if(eSTACK_SUCCESS != e_stack_ret)
      {
         e_ret = eSORT_FAILURE;
         goto LBL_SORT_QUICK_RET;
      }
   }
   
   e_ret = eSORT_SUCCESS;
LBL_SORT_QUICK_RET:
   if(NULL != hdl_stack)
   {
      e_ret = stack_deinit(&hdl_stack);
      hdl_stack = NULL;
   }
   return e_ret;
}
Пример #4
0
/**Function********************************************************************

  Synopsis    [Destroys a stack instance]

  Description [The memory used by the Stack will be freed.
  Note: memory occupied by the elements is not freed! It is the user
  responsibility.]

  SideEffects []

  SeeAlso     []

******************************************************************************/
void Stack_destroy(Stack_ptr self)
{
  STACK_CHECK_INSTANCE(self);
  stack_deinit(self);
  FREE(self);
}
Пример #5
0
Файл: qs.c Проект: zxwbj/danei
/* 释放剩余节点并恢复到初始状态 */
void queue_deinit (QUEUE* queue) {
	stack_deinit (&queue->is);
	stack_deinit (&queue->os);
}