#include <stdio.h> #include "queue.h" #include <stdlib.h> /** * 单向链(带头)式队列: 尾入头出 */ typedef struct LinkNode { LinkNode *next; int data; } LinkNode; typedef struct Queue { LinkNode *front; LinkNode *rear; } Queue; //初始化操作 void InitQueue(Queue *sq) { LinkNode *head = (LinkNode *) malloc(sizeof(LinkNode)); head->next = NULL; sq->front = head; sq->rear = head; } //入队 void Enqueue(Queue *q, int val) { if (q == NULL) { return; } LinkNode *p = (LinkNode *) malloc(sizeof(LinkNode)); p->data = val; p->next = NULL; q->rear->next = p; q->rear = p; if (q->front->next == NULL) { q->front->next = p; } } //出队 int Dequeue(Queue *sq) { if (sq == NULL) { return -1; } if (sq->front == sq->rear) { return -1; } LinkNode *p = sq
单向链(带头)式队列实现
最新推荐文章于 2023-05-01 23:52:57 发布