/*
 *  lndir:  The C Program!
 *
 *  Copyright (c) 1993 by Salvatore Valente <svalente@athena.mit.edu>
 *
 *  You can freely distribute this program.  There is no warranty.
 *  See the file "COPYING" for more information.
 *
 */

#include "queue.h"

#define NULL ((Pointer) 0)
extern Pointer malloc (int bytes);
extern Pointer realloc (Pointer ptr, int bytes);
extern void free (Pointer ptr);

#define BLOCK 10

Queue q_new (void)
{
  Queue q;

  q = (struct queue *) malloc (sizeof (struct queue));
  q->alloced = BLOCK;
  q->size = 0;
  q->ptrs = (Pointer *) malloc (sizeof (Pointer) * BLOCK);
  q->ptrs[0] = NULL;
  return q;
}

void q_push (Queue q, Pointer data)
{
  if (q->size + 1 >= q->alloced) {
    q->alloced += BLOCK;
    q->ptrs = (Pointer *) realloc (q->ptrs, sizeof (Pointer) * q->alloced);
  }
  q->ptrs[q->size] = data;
  q->size++;
  q->ptrs[q->size] = NULL;
}

Pointer q_pop (Queue q)
{
  Pointer data;
  int x;

  if (! q->size) return NULL;
  data = q->ptrs[0];
  for (x = 0; x < q->size; x++)
    q->ptrs[x] = q->ptrs[x + 1];
  q->size--;
  return data;
}

Pointer q_peek (Queue q, int index)
{
  if (index >= q->size) return NULL;
  return q->ptrs[index];
}

int q_size (Queue q)
{
  return q->size;
}
