C语言删除节点

来源:百度知道 编辑:UC知道 时间:2024/05/20 01:31:09
#include<stdio.h>
typedef struct stu
{int data;
struct stu *next;
}aa;
aa *fun()
{ aa *t,*p,*h;
int c;
h=(aa*)malloc(sizeof(aa));
p=h;
printf("input:\n");
scanf("%d",&c);
while(c!=-1)
{
t=(aa*)malloc(sizeof(aa));
t->data=c;
p->next=t;
p=t;
printf("input:\n");
scanf("%d",&c);
}
t==NULL;
return h;
}

aa *del(aa *h)
{
aa *t,*p;
int n;
p=h;
printf("yao san de shu :\n");
scanf("%d,&n");
while(p->next!=NULL&&p->data!=n)
{ t=p;p=p->next; }
if(p==NULL) {h=p->next;}
else { t->next=p->next; }
return h;
}

main()
{
aa *head,*p;
head=fun();
del(head);
p=head;
while(p->next!=NULL)
{
p=p->next;
printf(" %d ",p->data);
}
}

aa *del(aa *h)
{
aa *t,*p;
int n;
p=h;
printf("yao san de shu :\n");
scanf("%d,&n");
while(p->next!=NULL&&p->data!=n)
{ t=p;p=p->next; }
if(p==NULL) {printf("Not found the data you want to delete.");return head;}
else { t->next=p->next;delete p; }
return h;
}

额。。
反正要删除要两个指针
1、P1一个指向要删除的上一个节点,P2一个指向要删除节点
2、把P1指向P2的next->
3、释放没用的空间

P1一个指向要删除的上一个节点,P2一个指向要删除节点
2、把P1指向P2的next->