Description
#include "stdlib.h"
#include "stdio.h"
typedef struct items {
int data;
struct items *link; // points to next element
} ITEM;
typedef struct queue {
int size;
ITEM *front, *rear;
} QUEUE;
void initQueue(QUEUE *q) {
q->size = 0;
q->front = q->rear = NULL;
}
int queueIsEmpty(QUEUE *q) {
return q->front == NULL;
}
int queueLength(QUEUE *q) {
return q->size;
}
void addQueue(QUEUE *q, int y) {
ITEM * x = (ITEM *) malloc(sizeof(ITEM));
x->data = y;
x->link = NULL;
if (q->front == NULL)
q->front = x;
else
q->rear->link = x;
q->rear = x;
q->size++;
}
int deleteQueue(QUEUE *q) {
ITEM * x = q->front;
int rel = x->data;
x->link = q->front;
if (q->front == NULL)
q->rear = NULL;
q->size--;
free(x);
return rel;
}
void showQueue (QUEUE *q){
while (!queueIsEmpty(q)) {
printf("%d", deleteQueue(q));
if (q->front != q->rear)
printf("<-");
}
return;
}
void main() {
QUEUE q;
int i;
initQueue(&q);
addQueue(&q, 1);
addQueue(&q, 3);
addQueue(&q, 5);
addQueue(&q, 7);
deleteQueue(&q);
deleteQueue(&q);
addQueue(&q, 2);
addQueue(&q, 4);
deleteQueue(&q);
addQueue(&q, 6);
showQueue(&q);
return;
}
Input
Output
Tags