为了账号安全,请及时绑定邮箱和手机立即绑定

为什么当第一个数大于x需要删除时,没法删除,新手刚学,求大神指教

为什么当第一个数大于x需要删除时,没法删除,新手刚学,求大神指教

C
交互式爱情 2023-03-16 17:13:49

#include<stdio.h>
#include<malloc.h>
#include <stdlib.h> 
typedef struct node_type
{ int d;
struct node_type*next;
}node_type; //链点
typedef struct list_type
{ node_type*head;
node_type*tail;
int length;
}list_type;//链表

creat_list() //创建链表函数
{ node_type*new_node;
list_type list;

list.length=0;
list.head=NULL;
list.tail=NULL;
new_node=malloc(sizeof(struct node_type));
scanf("%d",&new_node->d);
while(new_node->d>=0)


if(list.length<=0)
{ new_node->next=NULL; //末尾指针初始化
list.head=new_node;
list.tail=new_node;
}
else
{ new_node->next=NULL; //末尾指针初始化
list.tail->next=new_node;
list.tail=list.tail->next;
}
list.length=list.length+1;

new_node=malloc(sizeof(struct node_type));
scanf("%d",&new_node->d);
}
return(list.head);
}
print_list(typedef struct node_type*head) //链表输出函数
{ node_type*p;
p=head;
while(p!=NULL)
{printf("%d ",p->d);
p=p->next;}
}
del_node(node_type*head) //删除链表中大于X的数据
{ int x;
node_type*last,*cur;
printf("请输入要删除的数据:");
scanf("%d",&x);
last=NULL;
cur=head;
while(cur!=NULL)
{ if(cur->d>x)
{ if(last==NULL)
{ head=cur->next;
cur=head;
}
else
{ last->next=cur->next;
cur=last->next;
}
}
else
{ last=cur;
cur=cur->next;
}
}
}
void main()
{ node_type*p;
p=creat_list();
del_node(p);
print_list(p);
}

查看完整描述

1 回答

?
30秒到达战场

TA贡献1577条经验 获得超6个赞

#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
typedef struct node_type
{ int d;
struct node_type*next;
}node_type; //链点
typedef struct list_type
{ node_type*head;
node_type*tail;
int length;
}list_type;//链表

node_type *creat_list() //创建链表函数 /*更改:creat_list()前加上node_type * */
{
node_type*new_node;
list_type list;

list.length=0;
list.head=NULL;
list.tail=NULL;
new_node=(struct node_type*)malloc(sizeof(struct node_type)); /*更改:malloc(sizeof(struct node_type))前加上(struct node_type*)*/
printf("输入数值表:(输入负值时结束,负值不予输入)\n"); /*添加:printf*/
scanf("%d",&new_node->d);
while(new_node->d>=0)
{

if(list.length<=0)
{ new_node->next=NULL; //末尾指针初始化
list.head=new_node;
list.tail=new_node;
}
else
{ new_node->next=NULL; //末尾指针初始化
list.tail->next=new_node;
list.tail=list.tail->next;
}
list.length=list.length+1;

new_node=(struct node_type*)malloc(sizeof(struct node_type)); /*更改:malloc(sizeof(struct node_type))前加上(struct node_type*)*/
scanf("%d",&new_node->d);
}
return(list.head);
}
void print_list(node_type *head) //链表输出函数 /*更改:print_list(node_type *head)前加void*/
{ node_type*p;
p=head;
while(p!=NULL)
{printf("%d ",p->d);
p=p->next;}
}
void del_node(node_type*head) //删除链表中大于X的数据 /*更改:del_node(node_type*head)前加void*/
{ int x;
node_type*last,*cur;
printf("请输入要删除的数据最小值(删除数值大于该值的节点):"); /*更改:输出内容*/
scanf("%d",&x);
last=NULL;
cur=head;
while(cur!=NULL)
{ if(cur->d>x)
{ if(last==NULL)
{ head=cur->next;
cur=head;
}
else
{ last->next=cur->next;
cur=last->next;
}
}
else
{ last=cur;
cur=cur->next;
}
}
}
void main()
{ node_type *p;
p=creat_list();
del_node(p);
print_list(p);
}


查看完整回答
反对 回复 2天前
  • 1 回答
  • 0 关注
  • 6 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信