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了不该释放的

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题