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 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题