请帮我看一下链表程序,输出时有错误。
下面的链表程序输出时会出现:
a
b
v
d
e
f
a b
v d
e f
屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯
屯屯屯A
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;
}
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;
}
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;
}
#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;
}