1、完成尾删函数
//尾删
int list_delete_tail(linkList *h)
{
if(NULL==h || list_empty(h)){
return -1;
}
list_delete_pos(h,h->len);
return 0;
}
2、完成按位置修改函数
//按位置修改
int list_update_pos(linkList *h,int pos,datatype value)
{
if(NULL==h || list_empty(h)){
return -1;
}
linkList *p=list_find_pos(h,pos);
if(NULL==p){
return -2;
}
p->data=value;
return 0;
}
3、完成按值修改函数
//按值修改
int list_update_value(linkList *h,datatype old_value,datatype new_value)
{
if(NULL==h || list_empty(h)){
return -1;
}
int flag=0;
while(!list_empty(h)){
h=h->next;
if(h->data==old_value){
h->data=new_value;
flag=1;
}
}
if(flag){
return 0;
}else{
return -2;
}
}
4、完成链表翻转函数
//链表翻转
void list_reverse(linkList *h)
{
if(NULL==h || list_empty(h)){
puts("翻转失败");
return ;
}
linkList *p=h->next;
linkList *q=NULL;
h->next=NULL;
while(p!=NULL){
q=p;
p=p->next;
q->next=h->next;
h->next=q;
}
}
5、测试main函数
#include <stdio.h>
#include "linklist.h"
int main(int argc, const char *argv[])
{
linkList *h=list_creat();
if(NULL==h){
return -1;
}
list_insert_tail(h,'h');
list_insert_tail(h,'e');
list_insert_tail(h,'l');
list_insert_tail(h,'l');
list_insert_tail(h,'o');
list_insert_tail(h,' ');
list_insert_tail(h,'w');
list_insert_tail(h,'o');
list_insert_tail(h,'r');
list_insert_tail(h,'l');
list_insert_tail(h,'d');
list_insert_tail(h,'*');
puts("原链表遍历如下:");
list_show(h);
list_delete_tail(h);
puts("尾删后遍历如下:");
list_show(h);
list_update_pos(h,1,'x');
list_update_pos(h,5,'a');
list_update_pos(h,h->len,'z');
puts("按位置修改后,遍历如下:");
list_show(h);
list_update_value(h,'x','h');
list_update_value(h,'a','o');
list_update_value(h,'z','d');
puts("按值修改后,遍历如下:");
list_show(h);
list_reverse(h);
puts("链表翻转后如下:");
list_show(h);
list_free(&h);
return 0;
}