#include
#include
typedef char data;/*方便修改链表的数据类型*/
/*链表的类型:链表的类型是链表一个单元的类型*/
typedef struct link
{
data i;/*链表数据类型是data,即cahr*/
struct link p;/定义一个结构体类型的指针,因为链表指针域中存放的指针地址
存放的内容是一个结构体*/
}linklist;/*linklist不是变量名,而是类型名*/
void creatlink(linklist *head)
{
data m;
int t=1;
linklist *h,*temp;
temp=head;
do{
printf("请输入第%d结点的数据:\n",t);
h=(linklist *)malloc(sizeof(linklist));
temp->p=h;
fflush(stdin);
scanf("%c",&m);
h->i=m;
t++;
temp=h;
}while(m!='a');
temp->p=NULL;
printf("您已输入完毕\n");
}
void display(linklist *head)
{
linklist *temp;
int t=1;
temp=head;
if(temp->p==NULL)
printf("该链表是空表\n");
while(temp->p!=NULL)
{
printf("第%d个结点数据是%c\n",t,temp->p->i);
temp->p=temp->p->p;
t++;
}
printf("链表已显示完毕\n");
}
void dellink(linklist *head,data d)
{
linklist *temp1,*temp2;
temp1=head->p;
temp2=head;
while(temp1!=NULL)
{
if(temp1->i==d)
{
temp2->p=temp1->p;
break;
}
else
{
temp2=temp1;
temp1=temp1->p;
}/*temp->p=temp->p->p这样做是不是仍然改变了链表,要想不改变是不是应该改变temp的值,使它不再指向
head*/
}
if(temp1==NULL)
printf("你逗我!请输入正确的数据好吗!!!\n");
else
printf("已删除该数据\n");
}
int main()
{
data w;
linklist *head;
head=(linklist *)malloc(sizeof(linklist));
creatlink(head);
printf("\n");
display(head);
printf("\n");
printf("请你输入要删除的数据:\n");
fflush(stdin);
w=getchar();
dellink(head,w);
return 0;
}