List.h
#ifndef _LIST_H #define _LIST_H #include <stdio.h> #include <stdlib.h> #include <string.h> #define LIST_INIT_SIZE 100 #define LIST_INCRE_SIZE 10 #define OK 1 #define ERROR -1 typedef int ElemType; typedef int Status; typedef struct _LIST { ElemType *elem; int length; int listSize; }List,*PList; Status InitList(PList L); Status ListInsert(PList L,int i,ElemType e); Status ListDelete(PList L,int i,ElemType *e); Status LocateElem(List L,ElemType e); void MergerList(List La,List Lb,PList Lc); void PrintList(List L); #endif
List.c
#include "List.h"
Status InitList(PList L)
{
L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem)
{
printf("error to malloc\n");
exit(0);
}
L->length = 0;
L->listSize = LIST_INIT_SIZE;
return OK;
}
Status ListInsert(PList L,int i,ElemType e)
{
if((i < 1 )|| (i > L->listSize+1))
{
return ERROR;
}
if(L->length >= L->listSize)
{
ElemType * newbase = NULL;
//newbase = (ElemType *)realloc(L->elem,sizeof(ElemType) * LIST_INCRE_SIZE);//多谢@garbageMan的指正,此处确实存在问题,现已修改.
newbase = (ElemType *)realloc(L->elem,sizeof(ElemType) * (LIST_INCRE_SIZE+LIST_INIT_SIZE));
if(!newbase)
{
exit(0);
}
L->elem = newbase;
L->listSize += LIST_INCRE_SIZE;
}
ElemType *p,*q;
q = &(L->elem[i-1]);
for(p = &(L->elem[L->length-1]); p >= q; --p)
{
*(p+1) = *p;
}
*q = e;
L->length += 1;
return OK;
}
Status ListDelete(PList L,int i,ElemType *e)
{
if((i < 1 )|| (i > L->listSize+1))
{
return ERROR;
}
ElemType *p,*q;
p = &(L->elem[i-1]);
*e = *p;
q = L->elem + L->length - 1;
for(++p; p <= q; ++p)
{
*(p -1) = *p;
}
L->length -= 1;
return OK;
}
Status LocateElem(List L,ElemType e)
{
int i = 1;
ElemType *p = L.elem;
for(i = 0 ; i < L.length ; i++)
{
if(L.elem[i] == e)
{
return i;
}
}
return -1;
}
void MergerList(List La,List Lb,PList Lc)
{
ElemType *pa,*pb,*pc;
pa = La.elem;
pb = Lb.elem;
Lc->listSize = Lc->length = La.length + Lb.length;
pc = Lc->elem = (ElemType *)malloc(Lc->listSize * sizeof(ElemType));
if(!pc)
{
exit(0);
}
ElemType *pa_last,*pb_last;
pa_last = La.elem + La.length - 1;
pb_last = Lb.elem + Lb.length - 1;
while((pa <= pa_last) && (pb <= pb_last))
{
if(*pa <= *pb)
{
*pc++ = *pa++;
}
else
{
*pc++ = *pb++;
}
}
while(pa <= pa_last)
{
*pc++ = *pa++;
}
while(pb <= pb_last)
{
*pc++ = *pb++;
}
}
void PrintList(List L)
{
int i = 0;
printf("***********************************************************\n");
for(i = 0 ; i < L.length ; i++)
{
printf("%d\t",L.elem[i]);
if((i != 0) && (i % 10 == 0))
{
printf("\n");
}
}
printf("\n");
}
void DestroyList(PList L)
{
if(!L)
{
exit(0);
}
free(L->elem);
}
编译成动态库:gcc List.c -fPIC -shared -o LibList.so
测试程序:
main.c
#include "List.h" int main(int argc ,char** argv) { List La,Lb,Lc; InitList(&La); InitList(&Lb); InitList(&Lc); int i = 0; for(; i < 8; i++) { ListInsert(&La,i,i); } PrintList(La); for(i = 0 ; i < 10 ; i++) { ListInsert(&Lb,i,i*2 - 3); } PrintList(Lb); #if 1 ListDelete(&Lb,5,&i); printf("%d deleted\n",i); PrintList(Lb); i = LocateElem(Lb,13); printf("%d location is 13\n",i); #endif MergerList(La, Lb,&Lc); PrintList(Lc); DestroyList(&La); DestroyList(&Lb); DestroyList(&Lc); return 0; }
调用动态库,执行 gcc -o main_1 main_1.c ./LibList.so