Chained queue operation set
typedef struct lnode *list;
struct lnode {
int data;
list nextnode;
};
struct qnode {
list front, gear;
int Maxsize;
};
typedef struct qnode *qlist;
Create an empty queue
qlist makeq() {
qlist q = (qlist)malloc(sizeof(struct qnode));
q->front = NULL;
q->gear = NULL;
return q;
}
int isempty(qlist q) {
return (q->front == NULL);
}
Insert delete operation
void insertq(qlist q, int x) {
list temp = (list)malloc(sizeof(struct lnode));
temp->data = x;
temp->nextnode = NULL;
if (isempty(q)) q->front = q->gear = temp;
else {
q->gear->nextnode = temp;
q->gear = temp;
}
}
int deleteq(qlist q) {
list frontcell;
int frontnum;
if (isempty(q)) return -1;
else {
frontcell = q->front;
if (q->front == q->gear) {
q->front = NULL;
q->gear = NULL;
}
else {
q->front = q->front->nextnode;
}
frontnum = frontcell->data;
}
free(frontcell);
return frontnum;
}