zachary280 2017-03-05 10:57 采纳率: 20%
浏览 775

C 指针问题求助大神,程序为链表的选择排序,我的方法为将最小的节点放到头节点之后

程序代码如下所示,问题在注释中

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

typedef struct Node
{
    int num;
    struct Node* next;

}node;

int main()
{
    node *head,*item;
    srand(time(0));
    head=(node*) malloc(sizeof(node));
    head->num=0;
    head->next=NULL;
    node *temp=(node*) malloc(sizeof(node));
    temp=head;
    for(int i=0;i<2;i++)
    {
        item=(node*) malloc(sizeof(node));
        item->num=rand()%100;
        item->next=NULL;
        temp->next=item;
        temp=item;
    }
    free(item);
    free(temp);
//    node* item;
//    item=head->next;
//    while(item!=NULL)
//    {
//        printf("%d,",item->num);
//        item=item->next;
//    }
//    printf("\n");
    node* min=(node*) malloc(sizeof(node));
//    node* temp=(node*) malloc(sizeof(node));
    min=head->next;
    item=head->next;
    while(item->next)
    {
        free(temp);
        for(temp=min;(temp->next)!=NULL;)  //调试过程中运行到这一步temp变的很奇怪是什么原因?调试截图在下面
        {
            if(temp->next->num<min->num)
            {
                min=temp;
            }
            temp=temp->next;
        }
        temp=min->next;
        min->next->next=temp->next;
        temp->next=head->next;
        head->next=temp;
        min=head->next;
        item=item->next;
    }
    item=head->next;
    while(item!=NULL)
    {
        printf("%d,",item->num);
        item=item->next;
    }
}

图片说明

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-03-05 10:55
    关注

    代码就不看了,检查下你的free是否free了不该释放的

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP