队
#include<stdio.h> #include<stdlib.h> typedef struct Node { int data; struct Node* next; }Node; Node* initqueue() { Node* Q = (Node*)malloc(sizeof(Node)); Q->next = NULL; Q->data = 0; return Q; }
进队
void enqueue(Node* Q,int data) { Node* q = Q; Node* node = (Node*)malloc(sizeof(Node)); node->data = data; int i; for (i = 0; i < Q->data; i++) { q = q->next; } node->next = q->next; q->next = node; Q->data++; }
队非空
int isempty(Node* Q) { if (Q->data == 0 || Q->next == NULL) { return 1; } else { return 0; } }
退出队
int dequeue(Node* Q) { if (isempty(Q)) { return -1; } else { Node* node = Q->next; int data = node->data; Q->next = node->next; free(node); Q->data--; return data; } }
输出队
void printfqueue(Node* Q) { Node* node = Q->next; while (node) { printf("%d->", node->data); node = node->next; } printf("NULL\n"); }
主函数
int main() { Node* Q = initqueue(); enqueue(Q, 1); enqueue(Q, 2); enqueue(Q, 3); enqueue(Q, 4); printfqueue(Q); int data = dequeue(Q); printf("data = %d\n", data); printfqueue(Q); return 0; }