链队列

来源:互联网 发布:专业检测手机信号软件 编辑:程序博客网 时间:2024/05/22 13:16
#include <stdio.h>#include <malloc.h>typedef struct Node{    int data;    struct Node* next;}Node;typedef struct Link{    struct Node* rear;    struct Node* front;}Link;int init(Link* L){    L->front = (Node*)malloc(sizeof(Node));    if(L->front != NULL)    {        L->rear = L->front;        L->front->next = NULL;        return 1;    }    return 0;}int Enter(Link* L, int x){    Node* NewNode;    NewNode = (Node*)malloc(sizeof(Node));    if(NewNode != NULL)    {        NewNode->data = x;        NewNode->next = NULL;        L->rear->next = NewNode;        L->rear = NewNode;        return 1;    }    else return 0;}int Delete(Link* L, int* x){    Node* r;    r = L->front->next;    if(r == NULL)      //或L->front == L->rear        return 0;    *x = r->data;    L->front->next = r->next;    if(L->rear == r)        L->rear = L->front;    free(r);    return 1;}int main(){    Link L;    int x;    init(&L);    Enter(&L, 1);    Enter(&L, 2);    Enter(&L, 3);    Delete(&L, &x);    printf("%d\n", x);    Delete(&L, &x);    printf("%d\n", x);    Delete(&L, &x);    printf("%d\n", x);    return 0;}

0 0