#include "stdio.h"
#include "stdlib.h"
typedef int datatype_t;
typedef struct node {
datatype_t data;
struct node *next;
} linknode_t;
typedef struct {
linknode_t *front;
linknode_t *rear;
} linkqueue_t;
linkqueue_t *linkqueue_create() {
linkqueue_t *queue = (linkqueue_t *) malloc(sizeof(linkqueue_t));
if (queue == NULL) {
exit(0);
}
queue->front = queue->rear = (linknode_t *) malloc(sizeof(linknode_t));
if (queue->front == NULL) {
exit(0);
}
queue->front->next = NULL;
return queue;
}
//入队
int linkqueue_enter(linkqueue_t *queue, datatype_t value) {
linknode_t *temp = (linknode_t *) malloc(sizeof(linknode_t));
if (temp == NULL) {
return -1;
}
temp->data = value;
temp->next = NULL;
queue->rear->next = temp;
queue->rear = temp;
return 1;
}
//判断队列是否为空
int linkqueue_empty(linkqueue_t *queue) {
return queue->front == queue->rear ? 1 : 0;
}
datatype_t linkqueue_out(linkqueue_t *queue) {
if (linkqueue_empty(queue)) {
return -1;
}
linknode_t *temp = queue->front;
queue->front = queue->front->next;
datatype_t value = queue->front->data;
free(temp);
temp = NULL;
return value;
}
int main() {
linkqueue_t *queue = linkqueue_create();
printf("入队%d\n", linkqueue_enter(queue, 11));
printf("入队%d\n", linkqueue_enter(queue, 12));
printf("入队%d\n", linkqueue_enter(queue, 13));
printf("入队%d\n", linkqueue_enter(queue, 14));
printf("入队%d\n", linkqueue_enter(queue, 15));
printf("%d\n", linkqueue_out(queue));
printf("%d\n", linkqueue_out(queue));
printf("%d\n", linkqueue_out(queue));
printf("%d\n", linkqueue_out(queue));
printf("%d\n", linkqueue_out(queue));
printf("%d\n", linkqueue_out(queue));
printf("%d\n", linkqueue_out(queue));
printf("%d\n", linkqueue_out(queue));
return 0;
}
运行效果