aa1480897947 发表于 2021-11-30 17:38:53

请问队列链式存储入列的函数里面最后为什么不能释放p

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef char Elemtype;
typedef struct Qnode
{
        Elemtype data;
        structQnode *next;
}Qnode, * queueprt;
typedef struct
{
        queueprt front, read;
}qutelist;

void createlist(qutelist* s)
{
        s->front = s->read = (queueprt)malloc(sizeof(Qnode));
        if (!s->front)
        {
                exit(0);
        }
        s->front->next = NULL;
}

void push(qutelist* s, Elemtype e)
{
        queueprt p;
        p = (queueprt)malloc(sizeof(Qnode));
        if (p == NULL)
        {
                exit(0);
        }
        p->data = e;
        p->next = NULL;
        s->read->next = p;
        s->read = p;
free(p)//这里加释放掉p就会出错
}
void pop(qutelist* s, Elemtype* e)
{
        queueprt p;
        p = (queueprt)malloc(sizeof(Qnode));
        if (s->front == s->read)
        {
                exit(0);
        }
        p = s->front->next;
        *e = p->data;
        s->front->next = p->next;
       
        if (s->read == p)
        {
                s->read = s->front;
        }
        free(p);
}
void freelist(qutelist* s)
{
        while (s->front)
        {
                s->read = s->front->next;
                free(s->front);
                s->front = s->read;
        }
}
int nulllist(qutelist s)
{
        if (s.front == s.read)
        {
                return 1;
        }
        else
                return 0;
}
void main()
{
        qutelist s;
        Elemtype c;
        createlist(&s);
        scanf_s("%c", &c);
        while (c != '#')//输入一串字符,以#号结束
        {
                push(&s, c);
                scanf_s("%c", &c);
        }
        getchar();
        pop(&s, &c);
        while (!nulllist(s))
        {
                printf("%c", c);
                pop(&s, &c);
        }
        printf("%c", c);
}

aa1480897947 发表于 2021-11-30 17:39:29

求助求助!!!
页: [1]
查看完整版本: 请问队列链式存储入列的函数里面最后为什么不能释放p