读书人

请帮小弟我看一下链表程序输出时有异

发布时间: 2013-08-01 15:23:18 作者: rapoo

请帮我看一下链表程序,输出时有错误。
下面的链表程序输出时会出现:
a
b
v
d
e
f

a b
v d
e f
屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯
屯屯屯
Press any key to continue

不知什么原因?请指点。

#include "stdafx.h"
#include <stdio.h>
#include "stdlib.h"
#include "string.h"

struct name
{
char firstname[SIZE];
char lastname[SIZE];
struct name *next;
};

int main(void)
{
struct name *current, *head = NULL, *track;

head = current = (struct name *)malloc(sizeof(struct name));
while(gets(current->firstname)!=NULL && current->firstname[0]!='\0' )
{
gets(current->lastname);
current->next = NULL;
track = current;
current = (struct name *)malloc(sizeof(struct name));
track->next = current;
}

current = head;
while(head != NULL)
{
printf("%s %s\n", current->firstname, current->lastname);
current = current->next;
}

return 0;
}

[解决办法]
    current = head;
while(head != NULL)
{
printf("%s %s\n", current->firstname, current->lastname);
current = current->next;
}

你这里是的判断条件是一个为真的。
改成while(current != NULL)
[解决办法]
在main函数中做了修改,已标明!


int main(void)
{
struct name *current, *head = NULL, *track;

head = current = (struct name *)malloc(sizeof(struct name));


while(gets(current->firstname) != NULL && current->firstname[0] != '\0' )
{
gets(current->lastname);
current->next = NULL;
track = current;
current = (struct name *)malloc(sizeof(struct name));
track->next = current;
}

//循环中多开辟了一个name,应该释放
free(track->next);
track->next = NULL;
//END 释放

current = head;
while(current != NULL) //改为current, head在下面循环中未操作,死循环
{
printf("%s %s\n", current->firstname, current->lastname);
current = current->next;
}

return 0;
}


[解决办法]
我给你改了Main函数这一段代码,你看一下,这样就不会多开辟一个Name!

int main(void)
{
struct name *current, *head = NULL, *track;

char arrName[SIZE];
int nCount = 0;

while(gets(arrName) != NULL && arrName[0] != '\0')
{
track = (struct name *)malloc(sizeof(struct name));
if (0 == (nCount++))
{
head = current = track;
}
else
{
current->next = track;
current = current->next;
}
current->next = NULL;
strcpy(track->firstname, arrName);
gets(track->lastname);
}

current = head;
while(current != NULL)
{
printf("%s %s\n", current->firstname, current->lastname);
current = current->next;
}

return 0;
}

------解决方案--------------------


要学会调适.
代码都是有大大小小的错误.
不要轻易相信任何一个printf,
曾经一个大师说过,不要用任何一个printf和cout.

[解决办法]

#include <stdio.h>
#include "stdlib.h"
#include "string.h"
#define SIZE 100
struct name
{
char firstname[SIZE];
char lastname[SIZE];
struct name *next;
};

int main(void)
{
struct name *current, *head = NULL, *track;

head = current = (struct name *)malloc(sizeof(struct name));
while(gets(current->firstname)!=NULL && current->firstname[0]!='\0' )
{
gets(current->lastname);
current->next = NULL;
track = current;
current = (struct name *)malloc(sizeof(struct name));
track->next = current;
}

current = head;
while(current != NULL) //head改成current
{
printf("%s %s\n", current->firstname, current->lastname);
current = current->next;
}

return 0;
}

linux GCC编译执行没问题。

读书人网 >C语言

热点推荐