#include list.h //创建链表 node_p create_link_list() { node_p H (node_p)malloc(sizeof(node)); if(HNULL) { printf(空间申请失败\n); return NULL; } H-len0; H-nextNULL; return H; } //创建一个新节点 node_p create_new_point(datatype data) { node_p new(node_p)malloc(sizeof(node)); if(newNULL) { printf(空间申请失败\n); return NULL; } new-datadata; return new; } //头插 void insert_head(node_p H,datatype data) { if(HNULL) { printf(入参为空请检查\n); return; } node_p newcreate_new_point(data); new-nextH-next; H-nextnew; H-len; } //判空 int empty(node_p H) { if(HNULL) { printf(参数列表为空\n); return -1; } return H-nextNULL?1:0; } //输出 void show(node_p H) { if(HNULL) { printf(入参为空请检查\n); return; } if(empty(H)) { printf(参数列表为空\n); return; } node_p pH-next; while(p!NULL) { printf(%d-,p-data); pp-next; } printf(NULL\n); } //头删 void dele_head(node_p H) { if(HNULL) { printf(入参为空请检查\n); return; } if(empty(H)) { printf(参数列表为空\n); return; } node_p deleH-next; H-nextdele-next; free(dele); H-len--; } //尾插 void insert_tail(node_p H,datatype data) { if(HNULL) { printf(入参为空请检查\n); return; } node_p pH-next; while(p-next!NULL) { pp-next; } node_p newcreate_new_point(data); p-nextnew; new-nextNULL; H-len; } //尾删 void dele_tail(node_p H) { if(HNULL) { printf(入参为空请检查\n); return; } if(empty(H)) { printf(参数列表为空\n); return; } node_p pH;//从头结点开始 while(p-next-next!NULL) { pp-next; } node_p delep-next; p-nextNULL; free(dele); H-len--; } //按位置插入 void insert_pos(node_p H,int pos,datatype data) { if(HNULL) { printf(入参为空请检查\n); return; } if(pos1 || posH-len1) { printf(位置不合理\n); return; } node_p pH; for(int i0;ipos-1;i) { pp-next; } node_p newcreate_new_point(data); new-nextp-next; p-nextnew; H-len; } //按位置删除 void dele_pos(node_p H,int pos) { if(HNULL) { printf(入参为空请检查\n); return; } if(empty(H)) { printf(参数列表为空\n); return; } if(pos1 || posH-len1) { printf(位置不合理\n); return; } node_p pH; for(int i0;ipos-1;i) { pp-next; } node_p delep-next; p-nextdele-next; free(dele); H-len--; } //按位置修改 void change_pos_v(node_p H,int pos,datatype data) { if(HNULL) { printf(入参为空请检查\n); return; } if(empty(H)) { printf(参数列表为空\n); return; } if(pos1 || posH-len1) { printf(位置不合理\n); return; } node_p pH; for(int i0;ipos;i) { pp-next; } p-datadata; } //按位置查找返回值 int pos_value(node_p H,int pos) { if(HNULL) { printf(入参为空请检查\n); return -1; } if(empty(H)) { printf(参数列表为空\n); return -2; } if(pos1 || posH-len1) { printf(位置不合理\n); return -3; } node_p pH; for(int i0;ipos;i) { pp-next; } return p-data; } //按值查找返回位置 int value_pos(node_p H,datatype data) { if(HNULL) { printf(入参为空请检查\n); return -1; } if(empty(H)) { printf(参数列表为空\n); return -2; } node_p pH; for(int i1;p!NULL;i) { pp-next; if(p-datadata) return i; } } //链表逆置 #if 0 void inver_link_list(node_p H) { if(HNULL) { printf(入参为空请检查\n); return; } if(empty(H)) { printf(参数列表为空\n); return; } node_p pH-next-next; H-next-nextNULL; node_p np-next; while(p!NULL) { p-nextH-next; H-nextp; pn; if(n!NULL) { n-next; } } } //释放 void free_list(node_p H) { if(HNULL) { printf(入参为空请检查\n); return; } if(empty(H)) { printf(参数列表为空\n); return; } node_p pH-next; while(p!NULL) { pp-next; free(p); } } #endif#ifndef __LIST_H__ #define __LIST_H__ #include stdio.h #include string.h #include stdlib.h typedef int datatype; typedef struct node { union { datatype data; int len; }; struct node* next; }node,*node_p; node_p create_link_list(); node_p create_new_point(datatype data); void insert_head(node_p H,datatype data); int empty(node_p H); void show(node_p H); void dele_head(node_p H); void insert_tail(node_p H,datatype data); void dele_tail(node_p); void insert_pos(node_p,int pos,datatype data); void dele_pos(node_p,int pos); void change_pos_v(node_p H,int pos,datatype data); int pos_value(node_p H,int pos); int value_pos(node_p H,datatype data); void free_list(node_p H); void inver_link_list(node_p H); #endif#include list.h int main(int argc, const char *argv[]) { node_p Hcreate_link_list(); insert_head(H,20); insert_head(H,30); insert_head(H,40); insert_head(H,50); insert_head(H,60); insert_head(H,70); insert_head(H,80); insert_head(H,90); show(H); printf(--------\n); dele_head(H); dele_head(H); show(H); printf(--------\n); insert_tail(H,100); insert_tail(H,200); show(H); printf(--------\n); dele_tail(H); dele_tail(H); show(H); printf(--------\n); insert_pos(H,3,999); insert_pos(H,4,888); show(H); printf(--------\n); dele_pos(H,2); dele_pos(H,3); show(H); printf(--------\n); change_pos_v(H,2,333); show(H); printf(--------\n); int rec_valpos_value(H,2); printf(rec_val%d\n,rec_val); int posvalue_pos(H,20); printf(pos%d\n,pos); printf(--------\n); // inver_link_list(H); // show(H); // printf(--------\n); // free_list(H); // HNULL; return 0; }