数据结构第二章之线性表C实现
这是我毕业两年后重新学习数据结构的第二章学习笔记,这里就不多说线性表的结构,其中参考了网上一些关于头文件的声明,直接上代码,不知道的可以G一下:
1.一些数据类型和宏定义
#ifndef _TYPE_H_
#define _TYPE_H_
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
#endif
#define _TYPE_H_
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
#endif
2.主要是宏文件和函数定义
#ifndef _LINE_H_
#define _LINE_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "type.h"
#define INIT_SIZE 20
#define INCREMENT 10
typedef struct LineArray
{
ElemType *elem;
int length;
int listsize;
}Sqlist;
Status InitList(Sqlist &);
Status DestroyList(Sqlist &);
Status InsertList(Sqlist &L,int pos,ElemType e);
Status CopyList(Sqlist *L,char *s);
Status DeleteList(Sqlist *L,int pos);
Status FindElem(Sqlist *L,char );
Status UnionList(Sqlist **L,Sqlist *R);
#endif
#define _LINE_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "type.h"
#define INIT_SIZE 20
#define INCREMENT 10
typedef struct LineArray
{
ElemType *elem;
int length;
int listsize;
}Sqlist;
Status InitList(Sqlist &);
Status DestroyList(Sqlist &);
Status InsertList(Sqlist &L,int pos,ElemType e);
Status CopyList(Sqlist *L,char *s);
Status DeleteList(Sqlist *L,int pos);
Status FindElem(Sqlist *L,char );
Status UnionList(Sqlist **L,Sqlist *R);
#endif
3.具体的实现
#include "line.h"
Status InitList(Sqlist **L)
{
(*L)->elem=(ElemType *)malloc(INIT_SIZE*sizeof(ElemType));
if((*L)->elem==NULL)
exit(OVERFLOW);
(*L)->length=0;
(*L)->listsize=INIT_SIZE;
memset((*L)->elem,0,(*L)->listsize);
printf("Initial line list success! \n");
return OK;
}
Status DestroyList(Sqlist **L)
{
if(((*L)->elem))
{
free((*L)->elem);
(*L)->elem=NULL;
printf("Destroy line list success! \n");
}
}
Status CopyList(Sqlist *L,char *str)
{
int len=strlen(str);
if(len > L->listsize-1)
{
L->elem = (ElemType *) realloc(L->elem,(L->listsize + len + INCREMENT)* sizeof(ElemType) );
L->listsize += INCREMENT +len;
memset(L->elem,0,L->listsize);
}
strncpy(L->elem,str,len);
L->length=len+1;
return OK;
}
Status InsertList(Sqlist **L,int pos,ElemType e)
{
//step1:check if the pos is bigger than line's length
if(pos < 1 || pos > ((*L)->length+1) )
return ERROR;
//step2:check the elements size
if((*L)->length>=(*L)->listsize)
{
//add space
(*L)->elem=(ElemType*) realloc ( (*L)->elem , ( (*L)->listsize+INCREMENT) * sizeof(ElemType) );
(*L)->listsize+=INCREMENT;
}
if((*L)->elem==NULL)
return OVERFLOW;
ElemType temp=(*L)->elem[pos];
int len=(*L)->length;
for(;len>=pos-1;len--)
{
(*L)->elem[len+1]=(*L)->elem[len];
}
(*L)->elem[pos-1]=e;
++((*L)->length);
return OK;
}
void printlist(Sqlist *L,char *msg)
{
if(L->elem==NULL)
printf("%s :line list is null\n",msg);
else
{
printf("============%s start=============\n",msg);
printf("\tData:%s\n",L->elem);
printf("\tLength:%d\n",L->length);
printf("\tSize:%d\n",L->listsize);
printf("============%s end=============\n",msg);
}
}
Status DeleteList(Sqlist *L,int pos)
{
//check the pos
if( pos < 1 || pos > L->length )
return ERROR;
int len=L->length;
for(;pos<=len;pos++)
{
L->elem[pos-1]=L->elem[pos];
}
--(L->length);
}
Status FindElem(Sqlist *L,char c)
{
if(L->elem==NULL)
return ERROR;
char *p=L->elem;
while(*p)
{
if(*p==c)
{
return OK;
}
p++;
}
return FALSE;
}
Status UnionList(Sqlist **L,Sqlist *R)
{
if( (*L)->elem == NULL || R->elem == NULL )
return ERROR;
int lenr=R->length;
if( lenr > ( ((*L)->listsize) - ((*L)->length) ))
{
(*L)->elem = (ElemType *)realloc( (*L)->elem , ((*L)->listsize + R->listsize + INCREMENT) * sizeof(ElemType) );
}
int i;
for( i=0 ; i<lenr ; i++)
{
if( FALSE == FindElem(*L,R->elem[i]) )
{
InsertList(L , (*L)->length , R->elem[i] );
}
}
return OK;
}
void main()
{
Sqlist *pArray,Array;
Sqlist *pSrc,Src;
pArray=&Array;
pSrc=&Src;
InitList(&pArray);
InitList(&pSrc);
CopyList(pArray,"rockay");
CopyList(pSrc,"liuataolaceflc2423ko");
printlist(pArray,"Copy");
InsertList(&pArray,2,'1');
printlist(pArray,"Insert");
DeleteList(pArray,3);
printlist(pArray,"Delete 3");
FindElem(pArray,'a');
UnionList(&pArray,pSrc);
printlist(pArray,"Union ");
DestroyList(&pArray);
exit(0);
}
Status InitList(Sqlist **L)
{
(*L)->elem=(ElemType *)malloc(INIT_SIZE*sizeof(ElemType));
if((*L)->elem==NULL)
exit(OVERFLOW);
(*L)->length=0;
(*L)->listsize=INIT_SIZE;
memset((*L)->elem,0,(*L)->listsize);
printf("Initial line list success! \n");
return OK;
}
Status DestroyList(Sqlist **L)
{
if(((*L)->elem))
{
free((*L)->elem);
(*L)->elem=NULL;
printf("Destroy line list success! \n");
}
}
Status CopyList(Sqlist *L,char *str)
{
int len=strlen(str);
if(len > L->listsize-1)
{
L->elem = (ElemType *) realloc(L->elem,(L->listsize + len + INCREMENT)* sizeof(ElemType) );
L->listsize += INCREMENT +len;
memset(L->elem,0,L->listsize);
}
strncpy(L->elem,str,len);
L->length=len+1;
return OK;
}
Status InsertList(Sqlist **L,int pos,ElemType e)
{
//step1:check if the pos is bigger than line's length
if(pos < 1 || pos > ((*L)->length+1) )
return ERROR;
//step2:check the elements size
if((*L)->length>=(*L)->listsize)
{
//add space
(*L)->elem=(ElemType*) realloc ( (*L)->elem , ( (*L)->listsize+INCREMENT) * sizeof(ElemType) );
(*L)->listsize+=INCREMENT;
}
if((*L)->elem==NULL)
return OVERFLOW;
ElemType temp=(*L)->elem[pos];
int len=(*L)->length;
for(;len>=pos-1;len--)
{
(*L)->elem[len+1]=(*L)->elem[len];
}
(*L)->elem[pos-1]=e;
++((*L)->length);
return OK;
}
void printlist(Sqlist *L,char *msg)
{
if(L->elem==NULL)
printf("%s :line list is null\n",msg);
else
{
printf("============%s start=============\n",msg);
printf("\tData:%s\n",L->elem);
printf("\tLength:%d\n",L->length);
printf("\tSize:%d\n",L->listsize);
printf("============%s end=============\n",msg);
}
}
Status DeleteList(Sqlist *L,int pos)
{
//check the pos
if( pos < 1 || pos > L->length )
return ERROR;
int len=L->length;
for(;pos<=len;pos++)
{
L->elem[pos-1]=L->elem[pos];
}
--(L->length);
}
Status FindElem(Sqlist *L,char c)
{
if(L->elem==NULL)
return ERROR;
char *p=L->elem;
while(*p)
{
if(*p==c)
{
return OK;
}
p++;
}
return FALSE;
}
Status UnionList(Sqlist **L,Sqlist *R)
{
if( (*L)->elem == NULL || R->elem == NULL )
return ERROR;
int lenr=R->length;
if( lenr > ( ((*L)->listsize) - ((*L)->length) ))
{
(*L)->elem = (ElemType *)realloc( (*L)->elem , ((*L)->listsize + R->listsize + INCREMENT) * sizeof(ElemType) );
}
int i;
for( i=0 ; i<lenr ; i++)
{
if( FALSE == FindElem(*L,R->elem[i]) )
{
InsertList(L , (*L)->length , R->elem[i] );
}
}
return OK;
}
void main()
{
Sqlist *pArray,Array;
Sqlist *pSrc,Src;
pArray=&Array;
pSrc=&Src;
InitList(&pArray);
InitList(&pSrc);
CopyList(pArray,"rockay");
CopyList(pSrc,"liuataolaceflc2423ko");
printlist(pArray,"Copy");
InsertList(&pArray,2,'1');
printlist(pArray,"Insert");
DeleteList(pArray,3);
printlist(pArray,"Delete 3");
FindElem(pArray,'a');
UnionList(&pArray,pSrc);
printlist(pArray,"Union ");
DestroyList(&pArray);
exit(0);
}
4.最后,linux下用vim编译后运行结果
rockay@ubuntu:~/cws/algorithm$ gcc line.c
rockay@ubuntu:~/cws/algorithm$ ./a.out
Initial line list success!
Initial line list success!
============Copy start=============
Data:rockay
Length:7
Size:20
============Copy end=============
============Insert start=============
Data:r1ockay
Length:8
Size:20
============Insert end=============
============Delete 3 start=============
Data:r1ckay
Length:7
Size:20
============Delete 3 end=============
============Union start=============
Data:r1ckayliutoef243
Length:18
Size:20
============Union end=============
Destroy line list success!
rockay@ubuntu:~/cws/algorithm$
rockay@ubuntu:~/cws/algorithm$ ./a.out
Initial line list success!
Initial line list success!
============Copy start=============
Data:rockay
Length:7
Size:20
============Copy end=============
============Insert start=============
Data:r1ockay
Length:8
Size:20
============Insert end=============
============Delete 3 start=============
Data:r1ckay
Length:7
Size:20
============Delete 3 end=============
============Union start=============
Data:r1ckayliutoef243
Length:18
Size:20
============Union end=============
Destroy line list success!
rockay@ubuntu:~/cws/algorithm$
好了,一个简单的线性表结构的相关操作完成了,由于代码的函数和变量命名不规范,还请大家不要学习这种习惯,函数也还未考虑进算法的优化等情况。