三金的代码库 2020-08-08 09:39 采纳率: 0%
浏览 76
已采纳

单链表销毁时为什么会出现逆序输出

单链表销毁时为什么会出现逆序输出,按道理应该打印NULL 或者空的,
结果我输入1 2 3 0 后,它还有输出 ,输出321.。。。。。。
但是我的clear_list却可以实现,只是free 掉头结点之后就不行了,导致逆序输出。

#include"singlehead.h"

/*
creat_head:创建头结点

*/
headnode *creat_head()
{
    headnode *h=(headnode *)malloc(sizeof(*h));
    h->first=h->last=NULL;
    h->num=0;
    return h;
}
/*
connect_list:连接两个链表

*/

void connect_list(headnode *h1,headnode * h2)
{
    h1->last->next=h2->first;
    h1->num=h1->num+h2->num;
    h1->last=h2->last;
}
/*
insert_node:往有序的链表中插入一个节点,使之仍然有序
*/

void insert_node(headnode *h1)
{
    int d;
    scanf("%d",&d);
    Node *q=h1->first;
    Node *p=malloc(sizeof(*p));
    Node *qr=NULL;
    p->data=d;
    p->next=NULL;
    while(q)                        //插入排序
    {
        if(p->data<q->data)
            break;
        qr=q;
        q=q->next;
    }
    add(h1,p,q,qr);

}
/*
merge_two_list:把两个有序的链表合并成一个有序的链表
@h1
:第一个链表头结点
@h2:第二个链表的头结点
*/

void merge_two_list(headnode *h1,headnode * h2)
{
    connect_list(h1, h2);
    int i,j,temp;
    Node *p=h1->first;
    //printf("%s %d\n",__func__,__LINE__);
    //printf("%d\n",h1->num);
    for(i=0;i<h1->num-1;i++)                //利用冒泡排序进行数值交换
    {
        p=h1->first;
        for(j=0;j<h1->num-1;j++)
        {
            //printf("%d\n",p->data);
            if(p->data>p->next->data)
            {
                temp=p->data;
                p->data=p->next->data;
                p->next->data=temp;
            }
            p=p->next;
        }
    }
}
/*
find:找链表中值为x的所有节点
@x:要找的值
返回其下标
*/

int find(headnode *h1,elemtype x)
{
    Node *p=h1->first;
    int n=1;
    while(p)
    {
        if(p->data==x)
            return n;
        else
        {
            n++;
            p=p->next;
        }
    }
}
/*
delete_node:删除一个链表中值为x的所有节点
@x:要删除的值
*/

void delete_node(headnode *h1,elemtype x)
{
    Node *p=h1->first;
    Node *q=h1->first;
    Node *pn=h1->first;
    while(1)
    {
        p=pn;
        q=NULL;
        while(p)
        {
            if(p->data==x)
                break;
            q=p;
            p=p->next;
        }
        if(p!=NULL)
            pn=p->next;
        else if(p==NULL)
            return;

        if(p==h1->first)
        {

            h1->first=pn;
            printf("+++\n");
            free(p),p=NULL;
        }
        else if(p==h1->last)
        {
            h1->last=q;
            free(q),q=NULL;
            return ;
        }
        else
        {
            q->next=p->next;
            free(p),p=NULL;

        }

    }


}
/*
clear_list:清除一个链表
h1:目标链表头结点
*/
void clear_list(headnode *h1)
{
    Node *p=h1->first;
    Node *pn=NULL;
    printf("%p, %p, %d\n", h1->first, h1->last, h1->num);
    while(p)
    {
        //printf("%d",p->data);
        printf("1.%p, %p\n", p, p->next);
        pn=p->next;
        p->next=NULL;
        printf("2.%p, %p\n", p, p->next);
        free(p);                            
        //free(p);
        printf("3.%p, %p\n", p, p->next);
        p=pn;
    }
    printf("\n");
    h1->first=NULL;
    h1->last=NULL;
    h1->num=0;
    //free(h1);


}
/*
destroy:销毁一个链表
h1:目标链表头结点
*/

void destory_list(headnode * h1)
{
    clear_list(h1);     //先删除所有节点
    free(h1);           //再free头结点
}
void add(headnode *h,Node *p,Node * q,Node * qr)
{
    if(h->first==NULL)              //如果为空链表
    {
        h->first=h->last=p;
    }
    else
    {
        if(q==h->first)         //头插
        {
            p->next=q;
            h->first=p;
        }
        else if(q==NULL)        //尾插
        {
            qr->next=p;
            h->last=p;
        }
        else
        {
            p->next=q;
            qr->next=p;
        }
    }
    h->num++;

}
/*
init_list:创建有序链表

*/
headnode *init_list()
{
    headnode *h=creat_head();
    Node *q=h->first;
    Node *qr=NULL;
    int d;
    while(1)
    {
        scanf("%d",&d);
        if(0==d)
            break;
        Node *p=malloc(sizeof(*p));
        p->data=d;
        p->next=NULL;
        q=h->first;
        qr=NULL;
        while(q)                        //插入排序
        {
            if(p->data<q->data)
                break;
            qr=q;
            q=q->next;
        }
        add(h,p,q,qr);

    }
    return h;
}
/*
print_list:打印链表
@h:目标链表头结点
*/
void print_list(headnode *h)
{
    Node *p=h->first;
    while(p)
    {
        printf("%d ",p->data);
        p=p->next;

    }
    printf("\n");
}

int main(int argc,char *argv[])
{   
    int x,n;

    headnode *h1=init_list();
    //headnode *h2=init_list();
    //scanf("%d",&x);
    //delete_node(h1,x);
    //merge_two_list(h1,h2);
    //insert_node(h1);
    //connect_list(h1,h2);
    //clear_list(h1);
    //printf("%p, %p, %d\n", h1->first, h1->last, h1->num);
    //free(h1);
    destory_list(h1);                   //bug----->为什么free掉头结点后会逆序输出
    //n=find(h1,x);
    //printf("下标为:%d\n",n);
    //printf("%p, %p, %d\n", h1->first, h1->last, h1->num);
    print_list(h1);

}
  • 写回答

1条回答 默认 最新

  • dabocaiqq 2020-08-08 11:42
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 10月11日

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看