问题:
一、单链表的操作:
1、建立带头结点的单链表h;
2、输出单链表h中所有结点的数据域;
3、输入x,y在第一个结点x后插入结点y,若无结点x,则在表尾插入结点y;
4、输入k,删除单链表中所有的数据域为k的结点,并输出被删除结点的个数。
#include<stdlib.h>
typedel struct Node
{
int a;
struct Node *next}*LineNode;
main()
{
int d,i,x,y,k;
LineNode p,p1,head;
if(!(head=p=(LineNode)malloc(sizef(LineNode))))
return ERROR;
p->next=Null;
scanf(p-date);
printf("do you want to continue? otherwise enter 0");
scanf("%d",&d);
while(d)
{
if(!(p=p-next=(LineNode)malloc(sizef(LineNode))))
return ERROR;
scanf("%d",p->date);
printf("do you want to continue? otherwise enter 0");
scanf("%d",&d);
}
p-next=Null;
p=head;
for(i=1;p->next;i++)
{
printf("node %d date:%d",i,p->date);
p=p->next;
}
printf("node %d date:%d",i+1,p->date);
p=head;
printf("input x ang y");
scanf("%d%d"&x,&y);
for(;p->next;)
{
if(p->date==x)
{
if(!(p1=(LineNode)malloc(sizef(LineNode))))
return ERROR;
p1->date=y;
p1->next->p->next->next;
p->next=p1;
break;
}
p=p->next;
}
if(p->next==NULL)
{
if(!(p1=(LineNode)malloc(sizef(LineNode))))
return ERROR;
p1->date=y;
p->next=p1;
p1->next=NELL;
}
p=head;i=0;
printf("input the date k you want to delete\n");
scanf("%d",&k};
for(;1;)
{
if(head->date==k)
{
i++;
head=p=p->next;
}
else
break;
}
fpr(;1;)
{
if(p->next->date==k)
i++;
p->next=p->next->next;
p=p->next;
if(p->next==NULL)break;
}
printf("%d node had been deleted",i) ;
}