标题:关于链表死循环
取消只看楼主
gold244
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-3-12
结帖率:0
已结贴  问题点数:20 回复次数:1 
关于链表死循环
//新建了一个链表,但是为什么一直在死循环中。
#include<stdio.h>
#include<stdlib.h>
typedef struct position
{
    int n;
    struct position *next;
}pos;
char pos_num[15];
void Print_Link(pos *head)
{
    pos *last;
    printf("\n%d\t%d\n",&head,&(head->next));
    getchar();
    last=head->next;
    getchar();
    printf("%d\t%d\t%d\n",&(head),&(head->next),&(last));
    while(last)
    {
        printf("%d\t",last->n);//由于p和last的地址一直没有变动过,所以此处是死循环。
        last=last->next;
    }
    printf("\n");
}
void Creat_Link(pos *head);
main()
{
    int num=30;
    pos *head;
    Creat_Link(head);
    Print_Link(head);
}
void Creat_Link(pos *head)
{
    char i;
    pos *p=NULL,*last=NULL;
    head=(pos *)malloc(sizeof(pos));
    head->next=NULL;
    last=head;
    for(i=0;i<30;i++)
    {
        free(p);
        p=(pos *)malloc(sizeof(pos));
        p->n=i+1;
        printf("%d\t%d\t%d\n",p->n,&p,&(last));//这里建立的链表,打印出的地址字符显示p的位置和last的位置一直没变动过
        last->next=p;//但为什么会没有变动?
        last=p;
    }
    last->next=NULL;
}
搜索更多相关主题的帖子: position include 新建 
2015-03-12 10:02
gold244
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-3-12
得分:0 
回复 2楼 waterstar
还是不对,本来就没有free(p),因为一直显示在同一块内存操作,所以加上这句的,但最后还是死循环。
2015-03-12 13:58



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-442550-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.203874 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved