1 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
typedef struct node_type
{ int d;
struct node_type*next;
}node_type; //鏈點(diǎn)
typedef struct list_type
{ node_type*head;
node_type*tail;
int length;
}list_type;//鏈表
node_type *creat_list() //創(chuàng)建鏈表函數(shù) /*更改: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("輸入數(shù)值表:(輸入負(fù)值時(shí)結(jié)束,負(fù)值不予輸入)\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) //鏈表輸出函數(shù) /*更改: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的數(shù)據(jù) /*更改:del_node(node_type*head)前加void*/
{ int x;
node_type*last,*cur;
printf("請(qǐng)輸入要?jiǎng)h除的數(shù)據(jù)最小值(刪除數(shù)值大于該值的節(jié)點(diǎn)):"); /*更改:輸出內(nèi)容*/
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 回答
- 0 關(guān)注
- 103 瀏覽
添加回答
舉報(bào)