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

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

交互式爱情
浏览 64回答 1
1回答

30秒到达战场

#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);}
打开App,查看更多内容
随时随地看视频慕课网APP