链表在C语言中,链表的构造是否正确?
我试图实现一个链表抽象,但是我遇到了问题。创建链表并向其中添加元素后。当我打印列表时,它只以无限循环的方式打印列表中的第一个元素,这意味着第一个元素链接到它自己,或者打印函数不正确。但是,我找不到问题,有人能帮忙吗 以下是列表摘要:链表在C语言中,链表的构造是否正确?,c,linked-list,singly-linked-list,C,Linked List,Singly Linked List,我试图实现一个链表抽象,但是我遇到了问题。创建链表并向其中添加元素后。当我打印列表时,它只以无限循环的方式打印列表中的第一个元素,这意味着第一个元素链接到它自己,或者打印函数不正确。但是,我找不到问题,有人能帮忙吗 以下是列表摘要: typedef struct _friend { char *firstname; char *lastname; char birthdate[9]; } friend; typedef struct _node { frien
typedef struct _friend {
char *firstname;
char *lastname;
char birthdate[9];
} friend;
typedef struct _node {
friend *value;
struct _node *next;
} node;
typedef struct _linkedlist {
node *head;
} linkedlist;
程序必须遵循这个抽象,因为它是更大的东西的一部分。
以下是应打印列表并将节点添加到列表开头的函数:
/* addHead
*
* This function takes two parameters - a linked list and a friend.
* This creates a node for the linked list and connects the friend to the
* node. Then it adds the node to the head of the linked list.
*/
void addHead(linkedlist *llist, friend *f)
{
// create a node and put the friend in it
node *n = (node *)malloc(sizeof(node));
n->value = f;
n->next = NULL;
// if the list is empty
if (llist == NULL)
{
// this link is the entire list
llist->head = n;
printf("adding friend to null list\n");
}
// if the list is not empty
else
{
// make the new link's next pointer point to
// the first link in the list
n->next = llist->head;
printf("adding %s to head\n", n->value->firstname);
// make the head pointer point to the new link
llist->head = n;
}
}
/*
* printList
*
* This steps down through each of the nodes in a linked list and
* prints out the information stored in the friend to which the node points.
* Instead of automatically printing to the screen, it prints to the
* file pointer passed in. If the programmer wants to print to the screen,
* he/she will pass in stdout.
*/
void printList(linkedlist *llist,FILE *fp)
{
node *n;
friend *f;
// for each node, print out the friend attached to it
for(n = llist->head; n != NULL ; n = llist->head->next)
{
// assign f to the friend of the right node
f = n->value;
// print the friend out
fprintf(fp,"%s %s: %s\n",
f->firstname, f->lastname, f->birthdate);
}
}
谢谢试试:
void printList(linkedlist *llist,FILE *fp)
{
node *n;
friend *f;
// for each node, print out the friend attached to it
for(n = llist->head; n != NULL ; n = n->next)
{
// assign f to the friend of the right node
f = n->value;
// print the friend out
fprintf(fp,"%s %s: %s\n",
f->firstname, f->lastname, f->birthdate);
}
}
printList
中的for
循环不太正确:
for(n = llist->head; n != NULL ; n = llist->head->next)
应改为:
for(n = llist->head; n != NULL ; n = n->next)
否则,从第二次迭代开始,n
每次都设置为相同的值
下面的内容与您遇到的问题无关,但我想我还是要提一下。在以下代码中:
if (llist == NULL)
{
// this link is the entire list
llist->head = n;
printf("adding friend to null list\n");
}
如果llist==NULL
,则llist->head=n
将出现故障
在当前签名为addHead()
的情况下,如果llist
为NULL
(除了打印错误消息和退出之外),您可以做的事情不多
如果您想检查
llist->head
是否为NULL,则无需执行此操作,因为else
块已经正确处理了该问题。应为n=n->next,否则每次只会得到下一个head 我已对您的程序执行了以下操作:
- 稍微修改了
结构。为方便起见,将firstname和lastname声明为数组李>friend
- 编写了一个调用其他函数的
main()
- 签入
addHead()时出错
- 添加了
函数,该函数创建create\u friend()
structfriend
- 添加了
以释放freeList()
'ed的内存malloc()
- 已更正打印函数中的循环错误
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _friend {
char firstname[10];
char lastname[10];
char birthdate[9];
} friend;
typedef struct _node {
friend *value;
struct _node *next;
} node;
typedef struct _linkedlist {
node *head;
} linkedlist;
void addHead(linkedlist *llist, friend *f)
{
node *n = NULL;
if (( n = (node *)malloc(sizeof(node))) == NULL) {
printf("unable to allocate memory \n");
exit(1);
}
n->value = f;
n->next = NULL;
if (llist == NULL) {
llist->head = n;
printf("adding friend to null list\n");
} else {
n->next = llist->head;
printf("adding %s to head\n", n->value->firstname);
llist->head = n;
}
return;
}
void printList(linkedlist *llist)
{
node *n;
friend *f;
if (llist->head == NULL) {
printf("Empty list \n");
return;
}
for(n = llist->head; n != NULL ; n = n->next) {
f = n->value;
printf("%s %s %d \n", f->firstname, f->lastname, f->birthdate);
}
return;
}
friend * create_friend(char *fn, char *ln, char *dob)
{
friend *fp = NULL;
if ((fp = malloc(sizeof(friend))) == NULL) {
printf("unable to allocate memory \n");
exit(1);
}
strcpy(fp->firstname, fn);
strcpy(fp->lastname, ln);
strcpy(fp->birthdate, dob);
return fp;
}
void freeList(linkedlist *llist)
{
node *cur = llist->head;
node *prev = cur;
friend *f;
while (cur != NULL) {
prev = cur;
cur = cur->next;
f = prev->value;
printf("freeing .. %s %s %d \n", f->firstname, f->lastname, f->birthdate);
free(prev->value);
free(prev);
}
return;
}
int main(void)
{
linkedlist ll;
friend *f;
ll.head = NULL;
f = create_friend("firstname1", "lastname1", "12345678");
addHead(&ll, f);
f = create_friend("firstname2", "lastname2", "12345678");
addHead(&ll, f);
f = create_friend("firstname3", "lastname3", "12345678");
addHead(&ll, f);
printList(&ll);
freeList(&ll);
ll.head = NULL;
printList(&ll);
return 0;
}
#包括
#包括
#包括
typedef结构{
charfirstname[10];
char lastname[10];
出生日期[9];
}朋友;
类型定义结构节点{
朋友的价值;
结构_节点*下一步;
}节点;
类型定义结构链接列表{
节点*头;
}链接列表;
void addHead(linkedlist*llist,friend*f)
{
node*n=NULL;
if((n=(node*)malloc(sizeof(node)))==NULL){
printf(“无法分配内存\n”);
出口(1);
}
n->值=f;
n->next=NULL;
if(llist==NULL){
llist->head=n;
printf(“将朋友添加到空列表\n”);
}否则{
n->next=llist->head;
printf(“将%s添加到头\n”,n->value->firstname);
llist->head=n;
}
返回;
}
无效打印列表(linkedlist*llist)
{
节点*n;
朋友*f;
如果(LIST->head==NULL){
printf(“空列表”);
返回;
}
对于(n=llist->head;n!=NULL;n=n->next){
f=n->数值;
printf(“%s%s%d\n”,f->firstname,f->lastname,f->birthdate);
}
返回;
}
好友*创建好友(char*fn,char*ln,char*dob)
{
friend*fp=NULL;
if((fp=malloc(sizeof(friend)))==NULL){
printf(“无法分配内存\n”);
出口(1);
}
strcpy(fp->firstname,fn);
strcpy(fp->lastname,ln);
strcpy(fp->出生日期,出生日期);
返回fp;
}
无效自由列表(linkedlist*llist)
{
节点*cur=llist->head;
节点*prev=cur;
朋友*f;
while(cur!=NULL){
prev=cur;
cur=cur->next;
f=上一个->值;
printf(“释放..%s%s%d\n”,f->firstname,f->lastname,f->birthdate);
自由(上一个->值);
免费(上);
}
返回;
}
内部主(空)
{
链接列表ll;
朋友*f;
ll.head=NULL;
f=创建朋友(“firstname1”、“lastname1”、“12345678”);
艾德海德(法兰西&法兰西);
f=创建朋友(“firstname2”、“lastname2”、“12345678”);
艾德海德(法兰西&法兰西);
f=创建朋友(“firstname3”、“lastname3”、“12345678”);
艾德海德(法兰西&法兰西);
打印列表(&ll);
自由列表&ll;
ll.head=NULL;
打印列表(&ll);
返回0;
}
希望这有帮助 您正在检查llist是否为NULL,但随后会立即写入它。您的意思是检查llist->head是否为null?您是否意识到您在
if(llist==null)
和else
块中执行的操作完全相同?