c语言链表删除第一个节点,链表删除第一个节点或者插入在第一个节点出问题...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

我想了一个小时也看不出来 ,求大神帮忙 ,指点 。必粉!!!

#include

#include

#define LEN sizeof(struct node)

struct node{

int no;

struct node *next;

};

struct node *create()

{

struct node *head,*p1,*p2;

int n=0;

head=NULL;

p1=p2=(struct node *)malloc(LEN);

scanf("%d",&p1->no);

while(p1->no!=0)

{

n=n+1;

if(n==1)

head=p1;

else

p2->next=p1;

p2=p1;

p1=(struct node *)malloc(LEN);

scanf("%d",&p1->no);

}

p2->next=NULL;

free(p1);

return head;

}

struct node *delete(struct node *head,int x)

{

struct node *p1,*p2;

p1=head;

while(x!=p1->no&&p1->next!=NULL)

{

p2=p1;

p1=p1->next;

}

if(x==p1->no)

{

if(p1==head)

head=p1->next;

else

p2->next=p1->next;

free(p1);

}

return head;

}

void display(struct node *head)

{

struct node *p;

if(head!=NULL)

{

p=head;

do

{

printf("%d-->",p->no);

p=p->next;

}while(p!=NULL);

}

}

struct node *insert(struct node *head,struct node *x)

{

struct node *p1,*p2,*p0;

p1=head;

p0=x;

if(head==NULL)

{printf("11");

head=p0;

p0->next=NULL;

}

p2=p1;

while(p1->nono&&p1->next!=NULL)

{

p2=p1;

p1=p1->next;

}

if(p2->next!=NULL)

{

if(head==p1)

{

head=p0;

printf("%d",head->no);

p0->next=p1;

}

else

{ printf("11");

p2->next=p0;

p0->next=p1;

}

}

else

{ printf("11");

p1->next=p0;

p0->next=NULL;

}

return head;

}

int main()

{

struct node *head,*x;

x=(struct node *)malloc(LEN);

x->no=5;

head=create();

display(head);

printf("----------------\n");

delete(head,4);

insert(head,x);

display(head);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值