moor薄幸 发表于 2018-11-5 19:09:41

队列的链式存储结构

#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;
}


大佬们看看有什么不对的?
页: [1]
查看完整版本: 队列的链式存储结构