i多子妹师南 2016-11-13 15:48 采纳率: 0%
浏览 860

这个链表删除程序哪里有问题?

#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;
}

  • 写回答

3条回答 默认 最新

  • threenewbee 2016-11-13 16:19
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置