int filterList(const STUP head,int score) //删除掉小于指定分数的记录
{
STUP p=NULL,q=NULL;
if(0 != ifEmptyList(head) )return -1; //操作前进行一次检查,判断此表是否为空
for(p=head,q=p->next;q;) //遍历所有节点
{
if(q->score < score) //删除掉满足条件的节点
{
p->next=q->next;
free(q);
q=p->next;
head->score--; //及时更新元素个数
}
else
{
p=p->next;
q=p->next;
}
}
return 0;
}
int getNode(const STUP head,const int id) //获取表中指定ID元素的值
{
STUP p=NULL;
int res=-1;
if(0 != ifEmptyList(head) )return res; //操作前进行一次检查,判断此表是否为空
for(p=head->next;p;p=p->next) //遍历所有节点
{
if(id == p->ID) //打印满足条件的节点信息
{
printf("(%03d,%d)",p->ID,p->score);
res=0;
}
}
printf("\n");
return res;
}
int modifyNode(STUP head,int id,int score) //将表中指定ID的元素修改为新值
{
STUP p=NULL;
int res=-1;
if(0 != ifEmptyList(head) )return res; //操作前进行一次检查,判断此表是否为空
for(p=head->next;p;p=p->next) //遍历所有节点
{
if(id == p->ID) //将满足条件的节点分数进行更新,并且打印
{
p->score=score;
printf("(%03d,%d)",p->ID,p->score);
res=0;
}
}
printf("\n");
return res;
}
int clearList(const STUP head) //清空表
{
STUP p=head;
while(head->score)delNode(head,0); //使用之前定义的删除函数,进行循环删除,直到链表中元素个数为0
free(p); //将头节点一并销毁
printf("the list has been flushed\n");
return 0;
}
int main()
{
STUP head=NULL;
//create a empty list
printf("create a emplty list\n");
head = createList(); //创建链表测试
//insert node to list
printf("insert 5 node into list\n");
instNode(head,1,40,100);
instNode(head,2,55,100);
instNode(head,3,90,100);
instNode(head,4,55,100);
instNode(head,5,95,100); //添加元素测试
//show list
printf("show list\n");
showList(head); //打印测试
//insert node to specil postion
printf("insert nodes on pos 1,7,3,20,-5\n");
instNode(head,6,75,1);
instNode(head,7,50,7);
instNode(head,8,50,3);
instNode(head,9,90,20);
instNode(head,10,80,-5); //特殊位置插入测试
showList(head);
//search node from list which score above 85
printf("search nodes which score above 85\n");
searchNode(head,85); //搜索测试
//get node from list
printf("get node by id which is 8\n");
getNode(head,8); //查询元素测试
//sore list desc
printf("sort list by desc order\n");
sortListDesc(head); //降序排序测试
showList(head);
//sort list asc
printf("sort list by asc order\n");
sortListAsc(head); //升序排序测试
showList(head);
//delete node from list
printf("delete three node on pos 20,10,5,1,-4\n");
delNode(head,20);
delNode(head,10);
delNode(head,5);
delNode(head,1);
delNode(head,-4); //分别取五个有代表性的位置进行删除测试
showList(head);
//delete the record which below 60
printf("delete the record which below 60\n");
filterList(head,60); //过滤测试
showList(head);
//modify the value of a node
printf("modify node score to 19 which id is 10\n");
modifyNode(head,10,19); //修改值测试
showList(head);
//clear the list
printf("clear the list\n");
clearList(head); //清表测试
return 0;
}
编译执行
emacs@ubuntu:~/c$ alias gtc
alias gtc='gcc -Wall -g -o'
emacs@ubuntu:~/c$ gtc toblog.x toblog.c
emacs@ubuntu:~/c$ ./toblog.x
create a emplty list
insert 5 node into list
show list
(001,40)(002,55)(003,90)(004,55)(005,95)
insert nodes on pos 1,7,3,20,-5
(010,80)(006,75)(001,40)(008,50)(002,55)(003,90)(004,55)(005,95)(007,50)(009,90)
search nodes which score above 85
(003,90)(005,95)(009,90)
get node by id which is 8
(008,50)
sort list by desc order
(005,95)(003,90)(009,90)(010,80)(006,75)(002,55)(004,55)(007,50)(008,50)(001,40)
sort list by asc order
(001,40)(008,50)(007,50)(004,55)(002,55)(006,75)(010,80)(009,90)(003,90)(005,95)
delete three node on pos 20,10,5,1,-4
(007,50)(004,55)(006,75)(010,80)(009,90)
delete the record which below 60
(006,75)(010,80)(009,90)
modify node score to 19 which id is 10
(010,19)
(006,75)(010,19)(009,90)
clear the list
the list has been flushed
emacs@ubuntu:~/c$
原文地址
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。