结构体指针链表,编译报错,求指导!!
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
struct Family *get_person(void);//输入个人信息
bool related(struct Family *pmember1,struct Family *pmember2);//测试他爷爷、他奶奶
bool set_ancestry(struct Family *pmember1,struct Family *pmember2);
struct Date
{
int day;
int month;
int year;
};
struct Family
{
struct Date dob;
char name[20];
char father[20];
char mother[20];
struct Family *next;
struct Family *previous;
struct Family *p_to_pa;
struct Family *p_to_ma;
};
int main(void)
{
struct Family *first=NULL;
struct Family *current=NULL;
struct Family *last=NULL;
//struct Family *pmember1=NULL;
//struct Family *pmember2=NULL;
char more='\0';
for(;;)
{
printf("\nDo you want to details of a%s person (Y or N)?",first !=NULL?"nother":"");
scanf(" %c",&more);
if(tolower(more)=='n')
break;
current=get_person();
if(first==NULL)
{
first=current;
last=current;
}
else
{
last->next=current;
current->previous=last;
last=current;
}
current=first;
while(current->next !=NULL)
{
int parents=0;
last=current->next;
while(last !=NULL)
{
if(related(current,last))
if(++parents==2)
break;
last=last->next;
}
current=current->next;
}
}
/*current=first;
while(current->next !=NULL)
{
int parents=0;
last=current->next;
while(last !=NULL)
{
if(related(current,last))
if(++parents==2)
break;
last=last->next;
}
current=current->next;
}*/
current=first;
while(current !=NULL)//按顺序输出名字,日,月、年、父亲、母亲,及认爷爷,奶奶
{
printf("\n%s was born %d/%d/%d,and has %s and %s as parents.",current->name,current->dob.day,current->dob.month,current->dob.year,current->father,current->mother);
if(current->p_to_pa !=NULL)
printf("\n\t%s's birth date is %d/%d/%d",current->father,current->p_to_pa->dob.day,current->p_to_pa->dob.month,current->p_to_pa->dob.year);
if(current->p_to_ma !=NULL)
printf("\n\t%s's birth date is %d/%d/%d",current->mother,current->p_to_ma->dob.day,current->p_to_ma->dob.month,current->p_to_ma->dob.year);
current=current->next;
}
current=first;
while(current !=NULL)//释放内存
{
last=current;
current=current->next;
free(last);
}
system("pause");
return 0;
}
struct Family *get_person(void)
{
struct Family *temp;
temp=(struct Family*)malloc(sizeof(struct Family));
printf("\nEnter the name of the person:");
scanf("%s",temp->name);
printf("\nEnter %s's date of birth (day month year):",temp->name);
scanf("%d %d %d",&temp->dob.day,&temp->dob.month,&temp->dob.year);
printf("\nWho is %s's father?",temp->name);
scanf("%s",temp->father);
printf("\nWho is %s's mother?",temp->name);
scanf("%s",temp->mother);
temp->next=temp->previous=NULL;
temp->p_to_pa=temp->p_to_ma=NULL;
return temp;
}
bool set_ancestry(struct Family *pmenber1,struct Family *pmenber2)//测试A是否是B的爷爷/奶奶
{
struct Family *pmember1;
struct Family *pmember2;
if(strcmp(pmenber1->father,pmenber2->name)==0)
{
pmember1->p_to_pa=pmember2;
return true;
}
if(strcmp(pmember1->mother,pmenber2->name)==0)
{
pmember1->p_to_ma=pmember2;
return true;
}
else
return false;
}
bool related(struct Family *pmember1,struct Family *pmember2)
{
return set_ancestry(pmember1,pmember2)||set_ancestry(pmember2,pmember1);
}
...全文
相关推荐
数据结构算法习题答案带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点(注意不设头指针).docx
存储数据 struct QueueNode *next; // 指向下一个结点的指针} QueueNode;```这里定义了一个名为`QueueNode`的结构体,用来表示循环链表中的每一个结点。
C语言结构体的用法.ppt
,链表中的每个元素都是一个结构体变量,并且每个元素都指向下一个元素。
链表2.0源代码.txt
用C语言写的一个链表,大部分代码都有覆盖注释,可以在VS 2019上运行,无警告无报错,适合初学者研究结构体指针、文件读写等C语言基础设计
如何建立链栈与二叉链表.doc
链栈是一种特殊的线性数据结构,它以链表的形式实现,具有后进先出(LIFO)的特点。在C#编程中,虽然不是原生支持链栈,但可以通过定义链表节点结构体来创建链栈。
数据结构 链表 LinkList
双向链表:每个节点包含两个指针,一个指向前一个节点,另一个指向后一个节点。这使得在链表中的前后移动更加灵活。3. 循环链表:最后一个节点的指针会回指到链表的第一个节点,形成一个环状结构。


