链队列..

时间:2022-12-27 12:56:05


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct Node {
int data;
struct Node* next;
}Node;

typedef struct Queue {
Node head, *tail;
int length;
}Queue;

Node* getNewNode(int val) {
Node* p = (Node*)malloc(sizeof(Node));
p->data = val;
p->next = NULL;
return p;
}

Queue* init_queue() {
Queue* q = (Queue*)malloc(sizeof(Queue));
q->head.next = NULL;
q->tail = &q->head;
q->length = 0;
return q;
}

void clear_node(Node* node) {
if (node == NULL) return;
free(node);
return;
}

void clear(Queue* q) {
if (q == NULL) return;
Node* p = q->head.next, * temp;
while (p != NULL) {
temp = p->next;
clear_node(p);
p = temp;
}
free(q);
return;
}

int empty(Queue* q) {
return q->length == 0;
}
int push(Queue* q, int val) {
if (q == NULL) return 0;
Node *temp = getNewNode(val);
q->tail->next = temp;
q->tail = temp;
q->length += 1;
return 1;
}

int pop(Queue* q) {
if (q == NULL)return 0;
if (empty(q)) return 0;
Node* temp = q->head.next;
q->head.next = temp->next;
clear_node(temp);
q->length -= 1;
if (q->length == 0) q->tail = &q->head;
return 1;
}

int front(Queue* q) {
if (q->head.next == NULL) return 0;
return q->head.next->data;
}

void output(Queue* q) {
if (q == NULL) return;
printf("Queue(%d):[", q->length);
for (Node* p = q->head.next; p != NULL; p = p->next) {
p != q->head.next && printf(",");
printf("%d", p->data);
}
printf("]\n");
return q;
}
int main() {
srand(time(0));
Queue* q = init_queue();
#define MAX_OP 20
for (int i = 0; i < MAX_OP; i++) {
int op = rand() % 4;
int val = rand()%100;
switch (op) {
case 0:
case 1:
case 2: {
printf("push %d to the Queue = %d\n", val, push(q, val));
}break;
case 3: {
if (!empty(q)) {
printf("pop %d from the Queue = ", front(q));
printf("%d\n",pop(q));
}
}break;
}
output(q), printf("\n");
}
#undef MAX_OP
clear(q);
return 0;
}