顺序表(c/c++)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huxiaotong_exp/article/details/44685643
/*
顺序表 (已测试) 
*/ 

#include<stdlib.h>
#include<stdio.h>

#define INIT_LIST_SIZE 80
#define LIST_INCREAMENT 10


typedef int State;

const int OK = 1;
const int OVERFLOAW = 0;
const int EQUAL = 1;
const int GREATER = 1;
const int SMALLER = 0;
const int ERROR = 0;

typedef struct{
	int id;
} ElemType;

typedef struct{
	ElemType * elem;
	int listSize;
	int lenth;
} SqList;

//函数声明 
State initList(SqList &L);
int findElem(SqList &L,ElemType e,State (*compare)(ElemType a,ElemType b));
State listInsert(SqList &L,ElemType e,int pos);
State listDelete(SqList &L,int pos,ElemType &e);
State add(SqList &L,ElemType e); 
State compare(ElemType a,ElemType b);
State merge(SqList la,SqList lb,SqList &L,State (*cp)(ElemType a,ElemType b));

int main()
{
	return 0;
}

//初始化顺序表 
State initList(SqList &L)
{
	L.elem = (ElemType *)malloc(INIT_LIST_SIZE*sizeof(ElemType));
	if(!L.elem) return  OVERFLOAW;
	L.lenth = 0;
	L.listSize = INIT_LIST_SIZE;
	return OK;
}

//查找元素,返回元素的位子 ,若没找到返回0 
int findElem(SqList &L,ElemType e,State (*cp)(ElemType a,ElemType b))
{
	int index = 1;
	ElemType *p = L.elem;
	while(index<=L.lenth && !(*cp)(*p++,e)) 
	{
		index++;
	}
	if(index<=L.lenth)
		return index;
	else
		return 0;
}
  
//向顺序表中添加顺序添加元素
State add(SqList &L,ElemType e)
{
	//如果线性表空间已满,则增加空间 
	if(L.lenth>=L.listSize)
	{
		ElemType *newList;
		newList = (ElemType *)realloc(L.elem,(L.listSize+LIST_INCREAMENT)*sizeof(ElemType));
		if(!newList)
			return ERROR;
		L.elem = newList;
		L.listSize += LIST_INCREAMENT;
	}
	
	//向顺序表尾添加元素
	L.elem[L.lenth] = e;
	L.lenth++;
	return OK; 
 } 
//添加元素至指定pos 
State listInsert(SqList &L,ElemType e,int pos)
{
	//插入位子不合法 
	if(pos<1 || pos>L.lenth+1) return ERROR;
	//如果线性表空间已满,则增加空间 
	if(L.lenth>=L.listSize)
	{
		ElemType *newList;
		newList = (ElemType * )realloc(L.elem,(L.listSize+LIST_INCREAMENT)*sizeof(ElemType));
		if(!newList) return ERROR;//分配内存失败 
		L.elem = newList;
		L.listSize += LIST_INCREAMENT;
	}
	//添加
	ElemType *p = &(L.elem[L.lenth-1]);
	ElemType *fence = &(L.elem[pos-1]);
	for( ;p>=fence;p--)
		*(p+1)=*p;
	*(fence)= e;
	L.lenth++;
	return OK;
} 

//删除元素,并把删掉的元素存在e中 
State listDelete(SqList &L,int pos,ElemType &e)
{
	//判断位置是否合法
	if(pos<1 || pos>L.lenth) return ERROR;
	
	ElemType *p = &(L.elem[pos-1]);
	e = *p;
	
	//删除 
	ElemType *q = L.elem+L.lenth-1;
	while(p<q) 
	{
		*p=*(p+1);
		p++;
	}
	
	L.lenth--;
	return OK; 
} 


//比较两个元素是否相同,这里比较元素的id 
State compare(ElemType a,ElemType b)
{
	if(a.id == b.id )
		return EQUAL;
	else if(a.id > b.id)
		return GREATER;
	else 
		return SMALLER; 
}

//归并排序
State merge(SqList la,SqList lb,SqList &L,State (*cp)(ElemType a,ElemType b))
{
	initList(L);
	int a = 1;
	int b = 1;
	int k = 0;
	int len_la = la.lenth;
	int len_lb = lb.lenth; 
	while(a<=len_la && b<=len_lb)
	{
		if((*cp)(la.elem[a-1],lb.elem[b-1]))
		{
			L.elem[k] = lb.elem[b-1];
			b++;	
		}	
		else
		{
			L.elem[k] = la.elem[a-1];
			a++;	
		}
		k++;	
	}
	for(;a<=len_la;a++)
	{
		L.elem[k] = la.elem[a-1];
		k++;
	}
	
	for(;b<=len_lb;b++)
		L.elem[k] = lb.elem[b-1];
		k++;	
	return OK;
} 

猜你喜欢

转载自blog.csdn.net/huxiaotong_exp/article/details/44685643