我正在尝试实现链表抽象,但遇到了问题。一旦我创建了链接列表并向其中添加元素。当我打印列表时,它只以无限循环的方式打印其中的第一个元素,这意味着第一个元素链接到它自己,或者打印函数不正确。但是,我找不到问题,有人能帮忙吗?
以下是列表抽象:
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 listn");
}
// 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 headn", 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: %sn",
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 listn");
}
如果llist == NULL
,则llist->head = n
将断开。
使用addHead()
的当前签名,如果llist
是NULL
(除了打印错误消息并退出),您就无能为力了。
如果您想检查llist->head
是否为NULL,则不需要这样做,因为else
块已经正确处理了这一点。
尝试:
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: %sn",
f->firstname, f->lastname, f->birthdate);
}
}
我对您的程序做了以下操作:
- 对CCD_ 11结构进行了轻微的修饰。为方便起见,已将firstname和lastname声明为数组
- 编写一个调用其他函数的
main()
addHead()
中的错误检查- 添加了创建
friend
结构体的create_friend()
函数 - 添加
freeList()
以释放malloc()
’ed的内存 - 更正了打印函数中的循环错误
所以它开始了。。
#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 listn");
} else {
n->next = llist->head;
printf("adding %s to headn", 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;
}
希望这能有所帮助!
应该是n=n->下一个,否则每次都会得到下一个头部。