亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么當第一個數大于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貢獻1828條經驗 獲得超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);
}


查看完整回答
反對 回復 2023-03-18
  • 1 回答
  • 0 關注
  • 105 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號