#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char name[20];
char tel[9];
struct node *next;
int num[10];
};
struct node *head,*p,*q;
struct node *create()
{
// struct node *head,*p,*q;
char name[20];
head=NULL;
printf("name:");
gets(name);
while (strlen(name)!=0)
{
p=(struct node *)malloc(sizeof(struct node));//开辟新结点
if(p==NULL)
{
printf("allocation failure\n");
exit(0);
}
strcpy(p->name,name);
printf("tel:");
gets(p->tel);
printf("num:");
gets(p->num);
p->next=NULL;
if(head==NULL)
head=p;
else
q->next=p;
q=p;//令p指向新的结点
printf("name:");
gets(name);
}
return head;
}
struct node getelem (int i)
{ int j;
struct node *p;
p=head;
j=1;
while((p!=NULL)&&j<i)//一直寻找到结点i
{
p=p->next;
++j;
}
if((p==NULL)||j>i)
{
printf("此结点没找到");
}
else {
printf("找到了");
printf("%s\n",p->name);
printf("%s\n",p->tel);
printf("%s\n",p->num);
}
}
void main()
{ char k;
struct node *head;
head=create();
printf("%d\n",head);
printf("输入想要查找结点的序号");
scanf("%d",&k);
getelem(k);
}
其中结点序号的输入只能输入从一开始的整数,插入功能还没完成,敬请期待
经验总结:1,结构体中的数组在输出时,无论是char [],int[] 还是double[]型都必须用printf("%s",p->char);输出 其中s代表string