typedef int ElemType;
typedef struct node{
ElemType data;
struct node *next;
}listnode,*linklist;
linklist list_create();//创建链表
int list_tail_insert(linklist H,ElemType value);//尾插法
linklist list_get(linklist H, int pos);//查找指定位置的节点
int list_insert(linklist H ,ElemType value,int pos);//在指定位置插入
int list_show(linklist L);//遍历链表
int list_delete(linklist H,int pos);//删除指定节点
linklist list_free(linklist H);//释放malloc申请的节点
int list_reverse(linklist H);//反转链表
linklist list_adjmax(linklist H,ElemType *value);//求链表中两个相邻节点最大值,并返回前一个节点
int list_merge(linklist H1,linklist H2);//合并两个有序链表
linklist.c文件
#includetest.c文件#include #include"linklist.h" linklist list_create(){ linklist H; H=(linklist)malloc(sizeof(linklist)); if(H==NULL){ printf("malloc failedn"); return H; } H->data=0; H->next=NULL; return H; } int list_tail_insert(linklist H,ElemType value){ linklist p,q; //malloc if(((p=(linklist)malloc(sizeof(linklist)))==NULL)){ printf("malloc failed n"); return -1; } p->data=value; p->next=NULL; //find tail node q=H; while(q->next !=NULL){ q=q->next; } //insert q->next=p; return 0; } int list_show(linklist H){ linklist p; if(H==NULL){ printf("H is NULLn"); return -1; } p=H; while(p->next != NULL){ printf("%d ",p->next->data); p=p->next; } puts(" "); return 0; } linklist list_get(linklist H ,int pos){ linklist p; int i; if(H==NULL){ printf("H is NULLn"); return NULL; } if(pos==-1){ return H; } if(pos<-1){ printf("pos is invalidn"); return NULL; } p=H; i=-1; while(i next; if(p==NULL){ printf("pos is invalidn"); return NULL; } i++; } return p; } int list_insert(linklist H ,ElemType value,int pos){ linklist p; linklist q; if(H==NULL){ printf("H is NULLn"); return -1; } //new node p if((p=(linklist)malloc(sizeof(linklist)))==NULL){ printf("malloc failen"); return -1; } p->data=value; p->next=NULL; //local node q q=list_get(H,pos-1); if(q==NULL){ return -1; } p->next=q->next; q->next=p; return 0; } int list_delete(linklist H,int pos){ linklist p; linklist q; if(H==NULL){ printf("H is NULLn"); return -1; } p=list_get(H,pos-1); if(p==NULL){ return -1; } if(p->next==NULL){ printf("delete pos is invaild"); } q=p->next; p->next=q->next;//p->next=p->next->next printf("free:%dn",q->data); free(q); q=NULL; return 0; } linklist list_free(linklist H){ linklist p; if(H==NULL){ printf("H is nulln"); return NULL; } p = H; printf("free:n"); while(H !=NULL){ p=H; printf("%d ",p->data); H=H->next; free(p); } puts(""); return NULL; } int list_reverse(linklist H){ linklist p,q; if(H==NULL){ printf("H id nulln"); return -1; } if((H->next==NULL)||(H->next->next==NULL)){ return 0; } p=H->next->next; H->next->next=NULL; while(p!=NULL){ q=p; p=p->next; q->next=H->next; H->next=q; } return 0; } linklist list_adjmax(linklist H,ElemType *value){ linklist p,q,r; ElemType sum; if(H==NULL){ printf("H is NULLn"); return NULL; } if(H->next==NULL||H->next->next==NULL||H->next->next->next==NULL){ return H; } q=H->next; p=q->next; r=q; sum=q->data+p->data; while(p->next!=NULL){ p=p->next; q=q->next; if(sum data+p->data){ sum=q->data+p->data; r=q; } } *value=sum; return r; } int list_merge(linklist H1,linklist H2){ linklist q,p,r; if(H1==NULL||H2==NULL){ printf("H1 || H2 is NULLn"); return -1; } p=H1->next; q=H2->next; r=H1; H1->next=NULL; H2->next=NULL; while(p&&q){ if(p->data data){ r->next=p; p=p->next; r=r->next; r->next=NULL; }else{ r->next=q; q=q->next; r=r->next; r->next=NULL; } } if(p==NULL){ r->next=q; }else{ r->next=p; } return 0; }
#include#include"linklist.h" void test_get(); void test_insert(); void test_delete(); void test_free(); void test_reverse(); void test_adjmax(); void test_merge(); int main(){ // test_get(); // test_insert(); // test_delete(); // test_free(); // test_reverse(); // test_adjmax(); test_merge(); return 0; } void test_get(){ linklist H; linklist p; int value; H=list_create(); if(H==NULL){ return ; } printf("please input value, input -1 overn"); while(1){ scanf("%d",&value); if(value==-1){ break; } list_tail_insert( H, value); printf("input:n"); } list_show( H); p=list_get(H,4);//1 3 5 7 9 if(p!=NULL) printf("value=%dn",p->data); } void test_insert(){ linklist H; linklist p; int value; H=list_create(); if(H==NULL){ return ; } printf("please input value, input -1 overn"); while(1){ scanf("%d",&value); if(value==-1){ break; } list_tail_insert( H, value); printf("input:n"); } list_show( H); list_insert(H,100,2);//1 3 5 7 9 list_show(H); } void test_delete(){ linklist H; linklist p; int value; H=list_create(); if(H==NULL){ return ; } printf("please input value, input -1 overn"); while(1){ scanf("%d",&value); if(value==-1){ break; } list_tail_insert( H, value); printf("input:n"); } list_show( H); list_delete(H,-4); list_show(H); } void test_free(){ linklist H; linklist p; int value; H=list_create(); if(H==NULL){ return ; } printf("please input value, input -1 overn"); while(1){ scanf("%d",&value); if(value==-1){ break; } list_tail_insert( H, value); printf("input:n"); } list_show( H); printf("H=%pn",H); H=list_free(H); printf("H=%pn",H); list_delete(H,-4); list_show(H); list_free(H); } void test_reverse(){ linklist H; linklist p; int value; H=list_create(); if(H==NULL){ return ; } printf("please input value, input -1 overn"); while(1){ scanf("%d",&value); if(value==-1){ break; } list_tail_insert( H, value); printf("input:n"); } list_show( H); list_reverse(H); list_show(H); } void test_adjmax(){ linklist H; linklist ret; int value; int sum; H=list_create(); if(H==NULL){ return ; } printf("please input value, input -1 overn"); while(1){ scanf("%d",&value); if(value==-1){ break; } list_tail_insert( H, value); printf("input:n"); } list_show( H); ret=list_adjmax(H,&sum); printf("data=%d, sum=%dn",ret->data,sum); } void test_merge(){ linklist H1,H2; int a[]={1,3,5,8}; int b[]={2,4,6,7,9}; int i; H1=list_create(); H2=list_create(); for(i=0;i 这里每个功能都是独立的分开的,方便测试。



