int getNodePos(const DP head,const int score) //根据score来获取节点位置,获取满足条件的第一个节点位置
{
DP p=NULL;
int i=0;
if(0 == ifEmptyList(head) )return -1; //操作前进行一下检查,判断此表是否为空
for(i=1,p=head->next;p;p=p->next,i++)
{
if(score == p->score) return i; //如果找到,就反馈出当前位置
}
return -1;
}
int instNodeAfter(DP head,int score,int newscore) //根据score来插入新节点,只插入在第一个满足条件的节点后面
{
if(-1 ==getNodePos(head,score)) //检查列表中有没有此score的节点,没有就提示并返回
{
printf("the score %d not found\n",score);
return -1;
}
else //如果找到,就进行插入操作
{
instNode(head,getNodePos(head,score)+1,newscore);
return 1;
}
}
int modifyNode(DP head,int score,int newvalue) //将列表中指定值的节点修改为新值
{
DP p=NULL;
int res=-1;
if(0 == ifEmptyList(head) )return res; //操作前进行一下检查,判断此表是否为空
for(p=head->next;p;p=p->next) //遍历所有节点
{
if(score == p->score) //将满足条件的节点进行修改,并且打印
{
p->score=newvalue;
printf("(%d)",p->score);
res=0;
}
}
printf("\n");
return res;
}
int clearList(const DP head) //清空表并给出提示
{
DP p=head;
while(head->score)delNode(head,0); //使用之前定义的删除函数,进行循环删除,直到链表中元素个数为0
free(p); //将头节点一并销毁
printf("the list has been flushed\n");
return 0;
}
int main()
{
DP head=NULL;
//create a empty list
printf("create a emplty list\n");
head = createList(); //创建空链测试
printf("show list\n");
//show list
showList(head); //打印测试
//insert node to list
printf("insert 5 node into list\n");
instNode(head,100,70);
instNode(head,100,80);
instNode(head,100,50);
instNode(head,100,100);
instNode(head,100,50); //添加元素测试
showList(head);
//insert a node after the first node which socre is 50
printf("insert a node after the first node which socre is 50\n");
instNodeAfter(head,50,60); //根据score值插入测试
showList(head);
//delete nodes which score is 50
printf("delete nodes from list which score is 50\n");
while(-1 != getNodePos(head,50))delNode(head,getNodePos(head,50)); // 删除所有score为50的节点
showList(head);
//show node which score is above 75
printf("show nodes which score is above 75\n");
showNodesAbove(head,75); //节点过滤测试
showList(head);
//insert nodes on pos 30,5,3,1,-20
printf("insert nodes on pos 30,5,3,1,-20\n");
instNode(head,30,11);
instNode(head,5,22);
instNode(head,3,33);
instNode(head,1,44);
instNode(head,-20,55); //特殊位置插入测试
showList(head);
//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 nodes from list on pos 100,8,5,1,-1
printf("delete nodes on pos 100,8,5,1,-1\n");
delNode(head,100);
delNode(head,8);
delNode(head,5);
delNode(head,1);
delNode(head,-1); //特殊位置删除测试
showList(head);
//delete the record which below 60
printf("delete the record which below 60\n");
filterListBelow(head,60); //过滤删除测试
showList(head);
//modify the value of a node
printf("modify node score to 19 which score is 70\n");
modifyNode(head,70,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
show list
warning:empty list!
insert 5 node into list
(70)(80)(50)(100)(50)
insert a node after the first node which socre is 50
(70)(80)(50)(60)(100)(50)
delete nodes from list which score is 50
(70)(80)(60)(100)
show nodes which score is above 75
(80)(100)
(70)(80)(60)(100)
insert nodes on pos 30,5,3,1,-20
(55)(44)(70)(80)(33)(60)(100)(22)(11)
sort list by desc order
(100)(80)(70)(60)(55)(44)(33)(22)(11)
sort list by asc order
(11)(22)(33)(44)(55)(60)(70)(80)(100)
delete nodes on pos 100,8,5,1,-1
(33)(44)(60)(70)
delete the record which below 60
(60)(70)
modify node score to 19 which score is 70
(19)
(60)(19)
clear the list
the list has been flushed
emacs@ubuntu:~/c$
原文地址http://soft.dog/2016/12/14/data-structures-03/
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。