鱼C论坛

 找回密码
 立即注册
查看: 1476|回复: 0

[技术交流] 队列的链式存储结构

[复制链接]
发表于 2018-11-5 19:09:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

#define ok 1
#define error 0

typedef int status;
typedef char elemtype;

typedef struct queue
{
    elemtype data;
    struct queue *next;
}queue, *queuelist;

typedef struct
{
    queuelist front;
    queuelist rear;
}linkqueue;

void init(linkqueue *q)
{
    q->front = q->rear= (queuelist)malloc(sizeof(queue));
    if(!q->front)
    {
        exit(0);
    }
    q->front->next = NULL;
}

status destroyqueue(linkqueue *q)
{
    while(q->front)
    {
        q->rear = q->front->next;
        free(q->front);
        q->front = q->rear;
    }
    return ok;
}

status enqueue(linkqueue *q, elemtype e)
{
    queuelist p;
    p=(queuelist)malloc(sizeof(queue));
    if(!p)
    {
        exit(0);
    }
    p->next = NULL;
    p->data = e;
    q->rear->next = p;
    q->rear = p;
    return ok;

}

status dequeue(linkqueue *q, elemtype *e)
{
    queuelist p;
    if(q->front == q->rear)
    {
        return error;
    }
    p = q->front->next;
    *e = p->data;
    q->front->next = p->next;
    if(q->rear == p)
    {
        q->rear = q->front;
    }
    free(p);
    return ok;
}
int main()
{
    linkqueue q;
    elemtype c;
    elemtype d;
    init(&q);
    printf("请输入一串字符,以#结束!\n");
    scanf("%c", &c);
    while('#' != c)
    {
        enqueue(&q, c);
        scanf("%c", &c);
    }
    while(q.front != q.rear)
    {
        dequeue(&q, &d);
        printf("%c", d);
    }

    return 0;
}


大佬们看看有什么不对的?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 22:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表