next up previous contents
Next: Queues: C++ queue2 Up: Queues: ADT Previous: Array Queues: queue.dat   Contents

Pointer Queues: queue1 [38]




\begin{picture}(340,134)(40,695)
\thicklines\put( 40,700){\framebox (40,40){}}
\...
... 95,695){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm prev}}}
\end{picture}

% gcc queue1 queue1.c                                  > tcc queue1.c
% queue1 queue1.dat
> h
h(elp; q(uit; e(nqueue x; D(equeue [NOT IMPLEMENTED]; d(isplay
h
> e 7 e 9 e 4 e 5 d

Queue as Doubly-Linked List: (front to rear)
+--+   +--+   +--+   +--+   
| 7|<->| 9|<->| 4|<->| 5|
+--+   +--+   +--+   +--+



Pointer Queues: queue1.c [39]

typedef struct node *node_ptr;
typedef struct queue_record *q_ptr;
struct node {
  element_type element;
  node_ptr next; 
  node_ptr prev; 
};
struct queue_record {
  node_ptr q_front;
  node_ptr q_rear;
  unsigned int q_size;
};
typedef node_ptr position;
typedef q_ptr QUEUE;
int is_empty(QUEUE Q) { return(Q->q_front == NULL); }
QUEUE create_queue() { 
  QUEUE Q;
  Q = (QUEUE) malloc(sizeof(struct queue_record));
  if (Q == NULL) printf("Out of space!\n");
  else {
    Q->q_front = NULL; Q->q_rear = NULL;
    Q->q_size = 0; 
  }
  return Q;
}


Arrays: queue1.c [40]
void enqueue(element_type x, QUEUE Q) {
  position tmp_cell;
  tmp_cell = (position) malloc(sizeof(struct node));
  if (tmp_cell == NULL) printf("Out of space!\n");
  else {
    tmp_cell->element = x;
    tmp_cell->next = NULL;
    tmp_cell->prev = Q->q_rear; 
    if (is_empty(Q))
      Q->q_front = tmp_cell;
    else
      Q->q_rear->next = tmp_cell;
    Q->q_rear = tmp_cell;
    Q->q_size++; 
}  }
element_type dequeue(QUEUE Q) { /* NOT IMPLEMENTED */ }
void display_queue(QUEUE Q) {
position pos; int i;
  pos = Q->q_front; 
  while (pos != NULL) {
    printf("%2d",pos->element);
    pos = pos->next;
  }
  printf("\n"); 
}



Ted Billard 2001-10-25