链式队列

#include <stdio.h>
#include <stdlib.h>
//创建初始结构体
typedef struct Queue{
    int data;
    struct Queue *next;
}Queue;
//创建队首队尾指针
typedef struct {
    Queue *first;
    Queue *last;
}OP;
//创建队列
void Init(OP *p){
    p->first = p->last = NULL;
}
//创建节点
Queue *Creat(int x){
    Queue *p;
    p = (Queue *)malloc(sizeof(Queue));
    p->data = x;
    p->next = NULL;
    return p;
}
//入队
int Enter(OP *p, int x){
    Queue *t;
    t = Creat(x);
    if(p->first == NULL && p->last == NULL) {
        p->first = p->last = t;
        return 0;
    }
    else{
        p->last->next = t;
        p->last = t;
        return 1;
    }
}
//出队
int Leave(OP *p, int *px){
    Queue *t;
    if(p->first == NULL && p->last == NULL)
        return -1;
    else if(p->first == p->last){
        *px = p->first->data;
        t = p->first;
        p->first = p->last = NULL;
        free(t);
        return 0;
    }
    else{
        *px = p->first->data;
        t = p->first;
        p->first = t->next;
        free(t);
        return 1;
    }
}
//输出整个队列
void Print(OP *p){
    while(p->first){
        printf("%d\n",p->first->data);
        p->first = p->first->next;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容