/* queueimpl.c
  L. Stewart October 20, 1982  9:44 PM
  D. Swinehart June 27, 1982  4:49 PM Zero item->next in Enqueue!!!
  L. Stewart February 23, 1983  4:09 PM, CallDebugger
 */

#include "env.h"
#include "queue.h"
#include "ec.h"

extern int CallDebugger();
extern int IWDC();
extern int DWDC();
extern int Zero();

/* set both words of q structure to zero */
InitQueue(q)
  struct Queue *q;
  {
  Zero(q, lenQueue);
  };

/* returns item or nil (0) */
struct Element *Dequeue(q)
  struct Queue *q;
  {
  struct Element *item;
  IWDC();
  item = q->head;
  if (item) q->head = item->next;
  DWDC();
  return (item);
  };

Enqueue(q, item)
  struct Queue *q;
  struct Element *item;
  {
  if (item == 0) CallDebugger(ecQueue+1);
  IWDC();
  if (q->head) q->tail->next = item;
  else q->head = item;
  q->tail = item;
  item->next = 0;
  DWDC();
  };

/* returns true on success, else false */
int Unqueue(q, item)
  struct Queue *q;
  struct Element *item;
  {
  struct Element *p, *lastp;
  if (item == 0) CallDebugger(ecQueue+2);
  IWDC();
  p = q->head;
  lastp = (struct Element *) q;
  while (p) {
    if (p==item) {
      lastp->next = p->next;
      if (p->next == 0) q->tail = lastp; /* item was the tail */
      DWDC();
      return(true);
      };
    lastp = p;
    p = p->next;
    };
  DWDC();
  return(false);
  };