
主程序
#include "fun.h"
int main(int argc, const char *argv[])
 {
     node_p L=create_head();//创建链表
     printf("########################链表的头插尾插\n");
     insert_head(L,45);//头插
     insert_head(L,45);
     insert_tail(L,45);//尾插
     insert_head(L,30);//头插
     insert_head(L,70);
     insert_tail(L,100);//尾插
     show_link(L);//输出链表
     printf("########################头删\n");
     del_head(L);//头删
     show_link(L);
     printf("########################尾删\n");
     del_tail(L);//尾删
     show_link(L);
     printf("########################按位置插入\n");
     insert_loc(L,2,100);//按位置插入
     show_link(L);
     printf("########################按位置删除\n");
     del_loc(L,4);//按位置删除
     show_link(L);
     printf("########################按值修改\n");
     chag_data(L,45,99);
     show_link(L);
     printf("########################按值查找返回地址\n");
     int m=30;
     if(find_link(L,m)!=NULL)
     {
         printf("%d的地址为%p\n",m,find_link(L,m));
     }
     else
         printf("查找失败\n");
     int n=2;
     printf("第%d个的位置的数据为:%d\n",n,find_data(L,n));
     printf("########################链表的反转\n");
     inver_link(L);//链表逆置
     show_link(L);
     printf("########################链表的区间反转\n");
     insert_tail(L,10);
     insert_tail(L,20);
     show_link(L);
     reversl(L,2,5);//链表区间反转
     show_link(L);
     printf("########################链表的释放\n");
     free_link(&L);//释放链表
     show_link(L);
     return 0;
 }
源程序
#include "fun.h"
//创建头结点
 node_p create_head()
 {
     node_p L=(node_p)malloc(sizeof(node));
     if(L==NULL)
     {
         printf("节点创建失败\n");
         return NULL;
     }
     L->len=0;
     L->next=NULL;
     return L;
 }
 //创建新节点
 node_p create_new(typdata data)
 {
     node_p new=(node_p)malloc(sizeof(node));
     if(new==NULL)
     {
         printf("节点创建失败\n");
         return NULL;
     }
     new->data=data;
     new->next=NULL;
     return new;
 }
 //头插
 void insert_head(node_p L,typdata data)
 {
     if(L==NULL)
     {
         printf("入参为空,请检查\n");
         return;
     }
     node_p new=create_new(data);
     new->next=L->next;
     L->next =new;
     L->len++;
 }
 //判空
 int empty_link(node_p L)
 {
     if(L==NULL)
     {
         printf("入参为空,请检查\n");
         return -1;
     }
     return L->len==0;
 }
 //输出链表
 void show_link(node_p L)
 {
     if(L==NULL)
     {
         printf("入参为空,请检查\n");
         return;
     }
     if(empty_link(L))
     {
         printf("链表为空,无法输出\n");
         return;
     }
     node_p p=L->next;
     while(p!=NULL)
     {
         printf("%d->",p->data);
         p=p->next;
     }
     putchar(10);    
 }
 //尾插
 void insert_tail(node_p L,typdata data)
 {
     if(L==NULL)
     {
         printf("入参为空,请检查\n");
         return;
     }
     node_p new=create_new(data);
     node_p p =L;
     while(p->next!=NULL)
     {
         p=p->next;
     }
     p->next=new;
     L->len++;
 }
 //头删
 void del_head(node_p L)
 {
     if(L==NULL)
     {
         printf("入参为空,请检查\n");
         return;
     }
     if(empty_link(L))
     {
         printf("链表已空,无需删除\n");
         return;
     }
     node_p p=L->next;
     L->next=p->next;
     free(p);
     p=NULL;
     L->len--;
 }
 //尾删
 void del_tail(node_p L)
 {
     if(L==NULL)
     {
         printf("入参为空,请检查\n");
         return;
     }
     if(empty_link(L))
     {
         printf("链表已空,无需删除\n");
         return;
     }
     node_p p=L;
     while(p->next->next!=NULL)
     {
         p=p->next;
     }
     node_p S=p->next->next;
     p->next=NULL;
     free(S);
     S=NULL;
     L->len--;
 }
 //按位置插入
 void insert_loc(node_p L,int n,typdata data)
 {
     if(L==NULL)
     {
         printf("入参为空,请检查\n");
         return;
     }
     if(n<=0||n>L->len)
     {
         printf("无效位置\n");
         return;
     }
     node_p p=L;
     for(int i=1;i<n;i++)
     {
         p=p->next;
     }
     node_p new=create_new(data);
     new->next=p->next;
     p->next=new;
     L->len++;
 }
 //按位置删除
 void del_loc(node_p L,int n)
 {
     if(L==NULL)
     {
         printf("入参为空,请检查\n");
         return;
     }
     if(empty_link(L))
     {
         printf("链表已空,无需删除\n");
         return;
     }
     if(n<=0||n>L->len)
     {
         printf("无效位置\n");
         return;
     }
     node_p p=L;
     for(int i=1;i<n;i++)
     {
         p=p->next;
     }
     node_p flag=p->next;
     p->next=p->next->next;
     free(flag);
     flag=NULL;
     L->len--;
 }
 //按值查找返回地址
 node_p find_link(node_p L,typdata data)
 {
     if(L==NULL)
     {
         printf("入参为空,请检查\n");
         return NULL;
     }
     if(empty_link(L))
     {
         printf("链表已空,无需查找\n");
         return NULL;
     }
     node_p p=L;
     int flag=0;
     for(int i=1;i<=L->len;i++)
     {
         p=p->next;
         if(p->data==data)
         {
             flag=1;
             return p;
         }
     }
     if(flag==0)
     {
         
         return NULL;
     }    
 }
 //按值修改
 void chag_data(node_p L,typdata data,typdata data2)
 {
     if(L==NULL)
     {
         printf("入参为空,请检查\n");
         return;
     }
     if(empty_link(L))
     {
         printf("链表已空,无需修改\n");
         return ;
     }
     node_p p=L;
     int flag=0;
     for(int i=1;i<=L->len;i++)
     {
         p=p->next;
         if(p->data==data)
         {
             flag=1;
             p->data=data2;
             printf("表中%d修改后的值为:%d\n",data,data2);
         }
     }
     if(flag==0)
     {
         printf("没有对应的值要修改,修改失败\n");
     }    
 }
 //按位置查找返回值
 typdata find_data(node_p L,int n)
 {
     if(L==NULL)
     {
         printf("入参为空,请检查\n");
         return -1;
     }
     if(empty_link(L))
     {
         printf("链表已空,无需查找\n");
         return-1;
     }
     if(n<=0||n>L->len)
     {
         printf("无效位置\n");
         return -1;
     }
     node_p p=L;
     for(int i=1;i<=L->len;i++)
     {
         p=p->next;
         if(i==n)
         {
             return p->data;
         }
     }
 }
 //链表逆置
 void inver_link(node_p L)
 {
     if(L==NULL)
     {
         printf("入参为空,请检查\n");
         return ;
     }
     if(empty_link(L))
     {
         printf("链表已空,无需逆置\n");
         return;
     }
     node_p p=L->next->next;//保留第二个节点
     L->next->next=NULL;//第一个节点指向NULL
     node_p t;
     
     while(p!=NULL)
     {
         t=p->next;//保留p指向的下一个节点
         p->next=L->next;//头插法
         L->next=p;
         p=t;//下一个要插入的节点
     }
 }
 //释放链表
 void free_link(node_p *L)
 {
     if(*L==NULL)
     {
         printf("入参为空,请检查\n");
         return;
     }
     for(int i=1;i<=(*L)->len;i++)
     {
         del_head(*L);
     }
     free(*L);
     *L=NULL;
 }
 //链表指定区间反转
 void reversl(node_p L,int n,int m)
 {
     if(L==NULL)
     {
         printf("入参为空,请检查\n");
         return;
     }
     if(n<1||n>L->len||m<1||m>L->len||n>=m)
     {
         printf("无效区间\n");
         return;
     }
     node_p p=L;
     node_p t=L;
     for(int i=1;i<n;i++)//左区间
     {
         p=p->next;
     }
     for(int i=1;i<=m+1;i++)//右区间
     {
         t=t->next;
     }
     node_p q=p->next->next;//保存以p为头结点的第二个元素
     p->next->next=t;
     node_p f;
     while(q->next!=t->next)//区间反转逻辑
     {
         f=q->next;
         q->next=p->next;
         p->next=q;
         q=f;
     }
 }
头文件
#ifndef __FUN_H_
 #define __FUN_H_
#include <myhead.h>
typedef char typdata;
typedef struct node
 {
     union
     {
         typdata data;
         int len;
     };
     struct node * next;
}node,*node_p;
 node_p create_head();//创建头结点
 node_p create_new(typdata data);//创建新节点
 void insert_head(node_p L,typdata data);//头插
 int empty_link(node_p L);//判空
 void show_link(node_p L);//输出链表
 void insert_tail(node_p L,typdata data);//尾插
 void del_head(node_p L);//头删
 void del_tail(node_p L);//尾删
 void insert_loc(node_p ,int n,typdata data);//按位置插入
 void del_loc(node_p L,int n);//按位置删除
 node_p  find_link(node_p L,typdata data);//按值查找返回地址
 typdata find_data(node_p L, int n);//按位置查找返回值
 void inver_link(node_p L);//链表逆置
 void free_link(node_p *L);//释放链表
 void reversl(node_p L,int n,int m);//链表指定区间反转
 void chag_data(node_p L,typdata data,typdata data2);//按值修改
#endif

  


















