标签:
#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef int ElemType;
//顺序表结构
typedef struct {
ElemType *elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList *L);
int ListInsert_Sq(SqList *L,int i,ElemType e);
void ListInput_Sq(SqList *L);
void ListOutput_Sq(SqList *L);
int ListLength_Sq(SqList *L);
int ListDelete_Sq(SqList *L,int i,ElemType *e);
int LocateElem_Sq(SqList *L,ElemType e,int (*compare)(ElemType,ElemType));
int compare(ElemType a,ElemType b);
void MergeList_Sq(SqList *La,SqList *Lb,SqList *Lc);
void GetElem(SqList *L,ElemType i,ElemType *e);
int EmptyList(SqList *L);
void ListInverse(SqList *L);
int main()
{
SqList sq,sq1,sq2;
int len,e,loc,e1,f;
e1 = 1;
//顺序表初始化
InitList_Sq(&sq);
//输入顺序表中的元素
ListInput_Sq(&sq);
//输出顺序表中的元素
ListOutput_Sq(&sq);
//向顺序表中插入元素
ListInsert_Sq(&sq,3,5);
ListOutput_Sq(&sq);
//输出顺序表的长度
len = ListLength_Sq(&sq);
printf("该线性表的长度为:%d\n",len);
//删除顺序表中某一位置的元素,并将被删除的元素返回
ListDelete_Sq(&sq,3,&e);
printf("被删除的元素为:%d\n",e);
ListOutput_Sq(&sq);
//输出顺序表中某一元素的位序
loc = LocateElem_Sq(&sq,3,&compare);
printf("该元素位序为:%d\n",loc);
//将两个顺序表合并
InitList_Sq(&sq1);
ListInput_Sq(&sq1);
MergeList_Sq(&sq,&sq1,&sq2);
ListOutput_Sq(&sq2);
//输出顺序表是否非空
f = EmptyList(&sq2);
if(f==0) {
printf("该顺序表是否为空? false\n");
} else {
printf("该顺序表是否为空? true\n");
}
//输出顺序表某一位置的元素
GetElem(&sq2,4,&e);
printf("第i个元素为:%d\n",e);
//将顺序表逆置
ListInverse(&sq2);
ListOutput_Sq(&sq2);
return 0;
}
//初始化线性表
int InitList_Sq(SqList *L) {
L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem) exit(OVERFLOW);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
//向线性表中输入元素
void ListInput_Sq(SqList *L) {
int i,m,n;
printf("请输入线性表中元素个数:\n");
scanf("%d",&n);
printf("请输入元素:\n");
for(i=0; i<n; i++) {
scanf("%d",&m);
L->elem[i] = m;
L->length++;
}
return;
}
//从线性表输出元素
void ListOutput_Sq(SqList *L) {
int i;
if(L->length == 0) {
printf("该线性表中没有元素!\n");
} else {
printf("该线性表中的元素为:");
for(i=0; i<L->length; i++) {
printf("%d ",L->elem[i]);
}
}
printf("\n");
return;
}
//返回线性表中元素的个数
int ListLength_Sq(SqList *L) {
return L->length;
}
//向线性表中插入元素
int ListInsert_Sq(SqList *L,int i,ElemType e) {
SqList *newbase;
//int *p,*q;
int m;
if(i<1 || i>L->length+1) return ERROR;
if(L->length >= L->listsize) {
newbase = (ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase) exit(OVERFLOW);
L->elem = newbase;
L->listsize += LISTINCREMENT;
}
/*q = &(L->elem[i-1]);
for(p=&(L->elem[L->length-1]);p>=q;--p) {
*(p+1) = *p;
}
*q = e;*/
for(m=L->length-1; m>=i-1; --m) {
L->elem[m+1] = L->elem[m];
}
L->elem[i-1] = e;
++L->length;
return OK;
}
//删除线性表中的元素
int ListDelete_Sq(SqList *L,int i,ElemType *e) {
int m;
//int *p,*q;
if(i<1 || i>L->length+1) return ERROR;
e = L->elem[i-1];
for(m=i-1;m<L->length-1;++m) {
L->elem[m] = L->elem[m+1];
}
/*p = &(L->elem[i-1]);
*e = *p;
q = L->elem+L->length-1;
for(++p; p<=q; ++p) {
*(p-1) = *p;
}*/
--L->length;
return OK;
}
//寻找某一元素在线性表里的位置
int LocateElem_Sq(SqList *L,ElemType e,int (*compare)(ElemType,ElemType)) {
int i;
i = 1;
while(i<=L->length && !(*compare)(L->elem[i-1],e)) {
++i;
}
if(i<=L->length) return i;
else return 0;
}
int compare(ElemType a,ElemType b) {
if(a == b) {
return 1;
} else {
return 0;
}
}
//归并排序将两个顺序表合并(这两个顺序表都是有序的)
void MergeList_Sq(SqList *La,SqList *Lb,SqList *Lc) {
int i,j,k;
i=0;j=0;k=0;
Lc->listsize = Lc->length = La->length + Lb->length;
Lc->elem = (ElemType*)malloc(Lc->listsize*sizeof(ElemType));
if(!Lc->elem) exit(OVERFLOW);
while(i<La->length && j<Lb->length) {
if(La->elem[i] <= Lb->elem[j]) Lc->elem[k++] = La->elem[i++];
else Lc->elem[k++] = Lb->elem[j++];
}
while(i<La->length) Lc->elem[k++] = La->elem[i++];
while(j<Lb->length) Lc->elem[k++] = Lb->elem[j++];
return;
}
//返回顺序表中第i个元素的值
void GetElem(SqList *L,ElemType i,ElemType *e) {
*e = L->elem[i-1];
}
//判断顺序表是否非空,是空的,返回true;否则,返回false。
int EmptyList(SqList *L) {
if(L->length==0) {
return 1;
} else {
return 0;
}
}
//顺序表逆置
void ListInverse(SqList *L) {
int i,temp;
for(i=0; i<L->length/2;i++) {
temp = L->elem[i];
L->elem[i] = L->elem[L->length-1-i];
L->elem[L->length-1-i] = temp;
}
return;
}
标签:
原文地址:http://www.cnblogs.com/chengzi123/p/4330458.html