读书人

为啥这个链表步也不能实现

发布时间: 2013-09-15 19:58:13 作者: rapoo

为什么这个链表步也不能实现?
下面这个程序是创造一个小链表,我输入数字进去,但似乎没有进入子函数操作,怎么回事呢?在这个程序中我知道在 主函数的struct student head;与子函数的struct student *current, *prev;前面加上static可以实现输出。但是我不明白为什么我不加这个statci就似乎没有进入子函数,因为一步也没有输出。

我的问题主要是问为什么似乎没有执行子函数?因为我随便输入一个数就终止了。

12
Press any key to continue




#include "console.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object


struct student
{
int age;
struct student *next;
};

void Initialize(struct student *head);
void Create(int item, struct student **head,struct student **prev);
void Show(struct student *head);

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
struct student *head,*prev;
//static struct student *head;
int num;
head=NULL;
while(scanf("%d", &num))
{
if (0==num){break;}
Create(num, &head,&prev);
}

Show(head);
scanf("%d", &num);
return 0;
}

void Initialize(struct student *head)
{
head=NULL;
}

void Create(int item, struct student **head,struct student **prev)
{
struct student *current;
//static struct student *current, *prev;
current = (struct student *)malloc(sizeof(struct student));
current->age = item;
current->next = NULL;
if((*head) == NULL)
{
(*head) = current;
(*prev) = current;
}
else
{
(*prev)->next = current;
(*prev) = current;
}
}

void Show(struct student *head)
{
struct student *current;
current = head;
while(1)
{
printf("%d ", current->age);
if (current->next==NULL)
{break;}
current = current->next;
}
printf("\n");
}


[解决办法]
1)void Initialize(struct student *head) { head=NULL; }
这个函数,什么也没有做,
因为这是值传递,指针的值传递,不能改变指针本身,只能改变指针所指对象
改成 head->next =NULL 是可以的,不过要求 head不能是Null ,也不能是野指针
C,C++:
void Initialize(struct student **head) { *head=NULL; }
是可以的
C++:
void Initialize(struct student *&head) { head=NULL; }
也是可以的

因为,写了一个错误的,Initialize,所以连带着,这个Create也出错了
其实那个Initialize完全没有必要


[解决办法]
bug1:
这样的操作等于没有操作

Initialize(head);


void Initialize(struct student *head)
{
head = NULL;
}



bug2:
由bug1,head依旧是随机值,if不成立,上来就是执行else部分,prev又是局部变量,没有经过if部分,它也是肮脏的值(无效)因此,链表建立也就空中楼阁

if((*head) == NULL)
{
(*head) = current;
prev = current;
}
else
{
prev->next = current;
prev = current;
}


bug3:
malloc,free要配对使用,你向系统借了资源,没有主动还

如还有问题,请引用通知我!
[解决办法]

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


// The one and only application object


struct student
{
int age;
struct student *next;
};

void Initialize(struct student *head);
void Create(int item, struct student **head,struct student **prev);
void Destory(struct student **head);

void Show(struct student *head);

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
struct student *head,*prev;
//static struct student *head;

int num;
//Initialize(&head);
head=NULL;

while(scanf("%d", &num))
{
if (0==num){break;}
Create(num, &head,&prev);
}

Show(head);
scanf("%d", &num);

Destory(&head);
return 0;
}

void Initialize(struct student **head)
{
*head=NULL;
}
void Destory(struct student **head){

if(!head)return ;
struct student *p=*head, *q;

while(p)
{
q = p;p = p->next ;
free(q);
}
*head =NULL;
};
void Create(int item, struct student **head,struct student **prev)
{
struct student *current;
//static struct student *current, *prev;
current = (struct student *)malloc(sizeof(struct student));
current->age = item;
current->next = NULL;
if((*head) == NULL)


{
(*head) = current;
(*prev) = current;
}
else
{
(*prev)->next = current;
(*prev) = current;
}
}

void Show(struct student *head)
{
struct student *current;
current = head;
while(1)
{
printf("%d ", current->age);
if (current->next==NULL)
{break;}
current = current->next;
}

printf("\n");
}


这样修改后,一点问题也没有呀;
你这个代码,本来,就是只打算,创建一次链表,最后那个输入,是为了,使程序不会一闪而过,做的处理.
运行结果,不输入 0,不会结束输入,输入0 就会输出链表

1 2 3 4 5 6 7
89 120 234 55 0
1 2 3 4 5 6 7 89 120 234 55
最后,输入一个数据,程序结束

读书人网 >C语言

热点推荐