1.顺序表
////////////////////////////////////////////////////////////
//
// 实现顺序表的初始化,插入,删除,查找,逆置,合并等操作
//
////////////////////////////////////////////////////////////
//////////////
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElemType ;
#define MAXSIZE 100
#define INCREMENT 100
typedef struct SeqList
{
ElemType * head ; // 数据存储空间
int length ; // 长度
int size ; // 最大容量
} SeqList ;
// 初始化
void InitSeqList( SeqList * S )
{
S->head = (ElemType*)malloc(sizeof(ElemType)*MAXSIZE) ;
if( S->head == NULL )
{
printf("线性表初始化时内存分配失败!\n") ;
exit(0) ;
}
S->length = 0 ;
S->size = MAXSIZE ;
}
// 查找
// 输入元素的值,返回元素的索引 ; 查找失败返回-1
int Find( SeqList * S , ElemType elem )
{
int i = 0 ;
for( ; i < S->length ; i++ )
{
if( S->head[i] == elem )
{
return i ;
}
}
return -1 ;
}
// 插入