Example #1
0
void test_capacity_one_stack()
{
    Stack stk_instance = stack_new(1);
    Stack *stk = &stk_instance;

    StackResult result;

    assert(stack_empty(stk));
    assert(!stack_full(stk));

    stack_peek(stk, &result);
    assert(result.status == STACK_EMPTY);
    stack_pop(stk, &result);
    assert(result.status == STACK_EMPTY);

    stack_push(stk, 99, &result);
    assert(result.status == STACK_OK);
    assert(stack_full(stk));

    stack_push(stk, 222, &result);
    assert(result.status == STACK_FULL);
    stack_peek(stk, &result);
    assert(result.data == 99 && result.status == STACK_OK);

    stack_pop(stk, &result);
    assert(result.data == 99 && result.status == STACK_OK);
    assert(stack_empty(stk));
}
Example #2
0
void push_stack(Stack *s, char c)
{
  if (stack_full(s)) {
    printf("stack is full\n");
    return;
  }
  s->elements[s->top] = c;
  s->top++;
}
Example #3
0
void push(int value)
{
	if (stack_full()) {
        printf("Error: Stack is full, cannot push %d\n", value);
        return;
    } else {
        *top = value;
        top++;
    }
}
Example #4
0
/**
 *
 * on success return 0; return -1 if stack overflow.
 *
 */
int push(stack_t *s, elem_t e)
{
	if(stack_full(s))
	{
		return -1;
	}

	s->top++;
	*(s->base + s->top) = e;
	return 0;
}
Example #5
0
void push(int * top, node item)
{
	if(*top == STACKMAXSIZE)
	{
		stack_full();
		return;
	}

	*top = *top + 1;
	stack[*top] = item;
}
Example #6
0
int  stack_push(StackPtr s, EntryType item)
{
    int status = 0;   
    if( stack_full(s) ){  // 如果栈满,则返回失败标识(上溢)
        status = -1;
    }
    else{
        s->top++;                          // 首先修改栈顶指针
        s->entry[s->top] = item;  // 将数据元素放入栈顶
    }
    return status;
}
Example #7
0
static void push_file_timeline(FILELIST* flptr, const uint8 *title, size_t len,
                               sw_tl_extent extent)
{
  sw_tl_ref tl_ref ;

  HQASSERT(!stack_full(), "file timeline stack full");

  CHECK_TL_VALID(tl_ref = progress_start(title, len, extent)) ;

  if ( tl_ref != SW_TL_REF_INVALID ) {
    ++maxIndex;
    aReadFile[maxIndex].flptr  = flptr;
    aReadFile[maxIndex].tl_ref = tl_ref;
  }
}
/**
 * Solves the exrecise
 *
 * Exercise 10 from the 4th workbook
 * 
 * @param seq Sequence pointer
 * @param stack Stack pointer
 * @return void
 */
static void solve_exercise(seq_ptr *seq, stack_ptr *stack)
{
  char c; int aux = 0;
  if( ! seq_end(seq)) c = seq_read_first(seq);

  while( ! seq_end(seq) && ! stack_full(stack)) {
    if( ! isdigit(c)) {
      stack_push(stack, c);
    } else {
      aux = atoi(&c);
      while(aux > 0 && ! stack_empty(stack)) {
        stack_pop(stack);
        aux--;
      }
    }
    c = seq_read_next(seq);
  }

  seq_close(seq);
}
Example #9
0
/**
 * if the stack is about to overflow,this function will automatically increse the stack size.
 * on success return 0; return -1 if realloc() failed.
 */
int push_v2(stack_t *s, elem_t e)
{
	elem_t *newbase;

	if(stack_full(s))
	{
		newbase = (elem_t *)realloc(s->base,(s->depth + STACK_INCREMENT) *sizeof(elem_t));
		if(newbase == NULL)
			return -1;
		else
		{
			//free(s->base); /* the lib function will automaticlly free the old area. otherwise will occures: "double free or corruption" */
			s->base = newbase;
			s->depth += STACK_INCREMENT;
			printf("\n add %d new space. ",STACK_INCREMENT);
		}
	}
	s->top++;
	*(s->base + s->top) = e;
	return 0;
}
Example #10
0
/* Add a file (or the device file for a filter chain) to the top of the stack of
 * files whose read progress will be reported.
 *
 * This implements the same logic as the previous version that used the progress
 * PS device to report progress.
 */
static void set_read_file_progress(FILELIST* flptr)
{
  /* Total length including device name delimiter */
#define MAX_PS_FILENAME (1 + LONGESTDEVICENAME + 1 + LONGESTFILENAME)
  uint8 buff[MAX_PS_FILENAME + 1]; /* Include terminating NUL */
  sw_tl_extent tl_extent;
  Hq32x2 extent;
  DEVICELIST* dev;
  size_t len;
  int32 deverr;

  /* do nothing if already being reported */
  if (STACK_CONTAINS(flptr)) {
    return;
  }

  /* report filter chain device files */
  if ((dev = theIDeviceList(flptr)) == NULL ||
      (theIFlags(flptr) & FILTER_FLAG) != 0) {
    if (theIUnderFile(flptr) &&
        isIOpenFileFilterById(theIUnderFilterId(flptr), theIUnderFile(flptr))) {
      set_read_file_progress(theIUnderFile(flptr));
    }
    return;
  }

  /* catch filter chain device files already closed */
  if (!isIOpenFile(flptr)) {
    return;
  }

  /* if full end reporting last file added */
  if (stack_full()) {
    pop_file_timeline();
  }

  /* find file extent */
  if (!(*theIBytesFile(dev))(dev, theIDescriptor(flptr), &extent,
                             SW_BYTES_TOTAL_ABS)) {
    deverr = (*theILastErr(dev))(dev);
    if (!(deverr == DeviceNoError || deverr == DeviceIOError)) {
      (void)device_error_handler(dev);
      return;
    }
    tl_extent = SW_TL_EXTENT_INDETERMINATE;
  } else {
    tl_extent = (sw_tl_extent)Hq32x2ToDouble(&extent);
  }

  /*
   * Read-write files are too painful to try and track progress of, and
   * are not really that important, so don't bother trying.
   */
  if ( isIReadWriteFile(flptr) )
    return;

  /* add to top of stack if reporting */
  len = swncopyf(buff, sizeof(buff), (uint8*)"%%%s%%%s", theIDevName(dev),
                 theICList(flptr));

  push_file_timeline(flptr, buff, len, tl_extent);

  return;
}
Example #11
0
void stack_push(T t, int n)
{
    assert(!stack_full(t));
    *t->top++ = n;
}
Example #12
0
int stack_push(stack* s)
{
  ASSERT(!stack_full(s));
  return (int)(s->n++);
}