Exemple #1
0
void main(void) 
{
    int k;
    Queue* q = qCreate(10);
    for (int i = 0; i < 16; ++i) {
        qEnqueue(q, i);
    }
    while (!qEmpty(q)) {
        printf("%d ", qDequeue(q));
        printf(" (size left = %d)\n",  qSize(q));
    }
    for (int i = 0; i < 16; ++i) {
        qEnqueue(q, i);
        printf("(size = %d)\n",  qSize(q));
    }

    Stack s;
    stackCreate(&s, 128);

    for (int i = 0; i < 255; ++i) {
        stackPush(&s, i);
    }

    while (!stackEmpty(&s)) {
        printf("%d ", stackTop(&s));
        stackPop(&s);
    }
}
Exemple #2
0
/* Removes the element on top of the stack */
void stackPop(Stack *stack) {
    int qsize = qSize(stack->q);
    int buf[qsize];

    int i = 0;
    while (!qEmpty(stack->q)) {
        buf[i++] = qDequeue(stack->q);
    }

    for (i = 0; i < qsize -1; ++i) {
        qEnqueue(stack->q, buf[i]);
    }
}
Exemple #3
0
/* Get the top element */
int stackTop(Stack *stack) {
    int qsize = qSize(stack->q);
    int buf[qsize];

    int i = 0;
    while (!qEmpty(stack->q)) {
        buf[i++] = qDequeue(stack->q);
    }

    for (i = 0; i < qsize; ++i) {
        qEnqueue(stack->q, buf[i]);
    }

    return buf[i - 1];
}
Exemple #4
0
/**
 * This function crosses a connected component
 * and calculates the number of black boxes.
 *
 * @param i Current y coordinate
 * @param j Current x coordinate
 * @param matrix Binary matrix
 * @param mark Matrix of marks
 *
 * @return Number of black boxes
 */
void crossCC(int y,
	     int x,
	     t_cc_elt *elt,
	     t_matrix *matrix,
	     char **mark)
{
  int i, j, kmin, kmax, pmin, pmax, first; int pix_count;
  int xtmp, ytmp; t_coordinate *coord, *res;
  t_box_coordinate *minmax; t_queue **q;
  pix_count = 1;
  q = NULL;
  q = (t_queue **)wcalloc(1, sizeof(t_queue *));
  xtmp = x; ytmp = y;
  minmax = wcalloc(1, sizeof(t_box_coordinate));
  minmax->xmin = x; minmax->xmax = x;
  minmax->ymin = y; minmax->ymax = y; first = 1;
  do
    { if (!first)
	{ res = qDequeue(q);
	  if (res != NULL)
	    { xtmp = res->x;
	      ytmp = res->y; } }
      kmin = ytmp-1;
      if (kmin < 0) kmin = 0;
      kmax = ytmp+1;
      if (kmax >= matrix->nbrows) kmax = matrix->nbrows - 1;
      for (i=kmin; i <= kmax; i++)
	{ pmin = xtmp-1;
	  if (pmin < 0) pmin = 0;
	  pmax = xtmp+1;
	  if (pmax >= matrix->nbcols) pmax = matrix->nbcols - 1;
	  for (j=pmin; j <= pmax; j++)
	    { if ((matrix->data[i][j] == 1) && (mark[i][j] == 'o'))
		{ coord = wmalloc(sizeof(t_coordinate));
		  coord->x = j;
		  coord->y = i;
		  qEnqueue(q, coord);
		  mark[i][j] = 'x';
		  updateMinMax(minmax, j, i);
		  pix_count++; } } }
      first = 0; }
  while (*q != NULL);
  elt->coord = *minmax; elt->nbpix = pix_count; qDelete(q); wfree(q);
}
Exemple #5
0
/* Push element x onto stack */
void stackPush(Stack *stack, int element) {
    qEnqueue(stack->q, element);
}