omega_du /*
单向链表
要求:完成Find()函数(位于第95行) */
#include <stdio.h>
#include <stdlib.h>
struct list
{
int data;
struct list *next;
};
typedef struct list Sqlist;
Sqlist* Create(Sqlist*); //创建
Sqlist* Output(Sqlist*); //输出
Sqlist* Find(Sqlist*,int); //查找
int main()
{
int n;
Sqlist *head,*p;
head = NULL;
head = Create(head);
Output(head);
printf("请输入要查找的结点:");
scanf("%d",&n);
getchar();
p=Find(head,n);
if (p!=NULL)
{
printf("第 %d 个结点的值为:%d\n\n",n,p->data);
}
else
{
printf("第 %d 个结点未找到!\n\n", n);
}
}
Sqlist* Create(Sqlist* head)
{
int i,n;
Sqlist *q,*s;
head = (Sqlist*)malloc(sizeof(Sqlist));
q = head;
printf("请输入单链表的长度:");
scanf("%d",&n);
getchar();
for(i=0;i<n;i++)
{
s=(Sqlist*)malloc(sizeof(Sqlist));
if(s!=NULL)
{
printf("请输入第 %d 个结点的值:",i+1);
scanf("%d",&s->data);
getchar();
q->next=s;
q=s;
}
else
i--;
}
q->next=NULL;
printf(" 建立链表,操作成功!\n\n");
return head;
}
Sqlist* Output(Sqlist* head)
{
Sqlist* q;
if(head==NULL)
printf(" 链表为空,操作失败!\n\n");
else
{
q=head->next;
while(1)
{
printf("%d ",q->data);
if(q->next==NULL)
break;
q=q->next;
}
printf("\n 输出完毕,操作成功!\n\n");
}
return head;
}
/*
查找函数
传入链及需要查找的节点,返回该节点的地址
*/
linklist * Find (head,key)
linklist * head;
datatype key;
{
linklist * p;
p = head -> next;
while (p!=NULL)
if(p->data!=key)
p-p->next;
else break;
rtturn 0;
}
Sqlist* Find(Sqlist* head,int n)
{
}