#include
#include
#include
typedef struct Node //创建链表
{
int data;
struct Node *next;
}List;
void InputList(List *); //初始化函数声明
int Inserta(List *,int); //查找或添加声明
main()
{
int d;
List *h;
h=(List *)malloc(sizeof(List));
InputList(h); //初始化函数
while(1) //循环查找或添加
{
printf("请输入要查找的点:n");
scanf("%d",&d); //要查找的结点值域为d
Inserta(h,d);
}
return 0;
}
void InputList(List *H)
{
int i,n,a;
List *p,*q;
p=H;
printf("请输入结点数量:n");
scanf("%d",&n);
//H->data=n; //把结点数量保存在头结点里,此程序可有可无
for(i=0;idata=a;
q->next=NULL;
p->next=q;
p=q;
}
}
int Inserta(List *H,int d)
{
List *p,*q;
p=H->next; //p指向首元结点
while(p->next!=NULL) //当p的指针域不为空时 当结束该while循环时,p指向末节点,但没有遍历末节点
{
if(p->data==d) //不遍历末节点有一个好处,可以让 p 刚好指向末节点,方便后序插入
{
printf("存在n");
return 0;
}
else
p=p->next; //p指向下一个结点
}
if(p->data==d) //因为末节点未被遍历,补充末节点的遍历
{
printf("存在n");
}
else
{
printf("不存在,尾部加入该新结点n"); //在末节点 p 后插入新结点
q=(List *)malloc(sizeof(List));
q->data=d;
q->next=NULL;
p->next=q;
}
return 0;
}