数据结构--线性表

线性表最简单也是最常用的一种数据结构,它的特点是,在数据元素的非空有限集中 ,(1)存在唯一一个被称为“第一个”的数据元素,存在唯一一个被称为“最后一个”的数据元素。(2)除了第一个数据元素外,集合中的其他数据元素都有一个直接前驱,除了最后一个元素外,集合中的其他数据元素都有一个直接后继;

 

线性表可分为顺序线性表和链式线性表,它们各有优缺点,顺序线性表中的数据元素之间在逻辑上相邻也在无理数相邻,顺序线性表的顺序存储结构决定了它的随机存取的特点,但是如果要对顺序进行插入或者删除特定的数据元素,则可能要进行大量的修改工作,但是链式线性表则克服了这种弊端,在进行删除或插入数据元素时可能比较方便,但是要提取一个数据元素则需要遍历线性表,所以这两种线性表在使用时应当根据需要并结合这两种线性表的特点进行选择。

 

顺序线性表的表示和实现:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define OK 1
#define ERROR -1
#define INIT_SIZE 100
#define LIST_SIZE 10
#define TRUE 1
#define FALSE 0
using namespace std;
typedef int ElemType ;
typedef int Status ;
typedef struct {
	ElemType *elem ;
	int len ; // 当前线性表的长度
	int capacity ; // 线性表的容量;
	
}sqList;
Status InitList(sqList &L)
{
	/*初始化 顺序线性表*/
//	L.elem = (ElemType *)malloc(sizeof(sqList)*INIT_SIZE);
	L.elem = new ElemType (sizeof(sqList)*INIT_SIZE) ;
	if(!L.elem)
	exit(ERROR) ;
	L.capacity = INIT_SIZE ;
	L.len = 0 ;
	return OK ;
}
Status DestroyList(sqList &L)
{
	/*如果线性表存在 , 销毁他*/
	if(L.elem==NULL)
	exit(ERROR);
	L.capacity = 0 ;
	L.len = 0 ;
	delete L.elem;
	return OK ;
}
Status ClearList(sqList &L)
{
	if(L.elem==NULL)
	exit(ERROR);
	memset(L.elem,0,sizeof(L.elem));
	L.len = 0 ;
	return OK ;
}
bool ListEmpty(sqList &L)
{

	return L.elem==NULL?true :false ;
	
}
int Listlength(sqList &L)
{
	if(L.elem==NULL)
	{
		cout<<"空表"<<endl ;
		exit(ERROR) ;
	}
	return L.len ;
}
Status GetElem(sqList &L ,int i ,ElemType &e)
{
	if(L.elem==NULL)
	{
		cout<<"空表"<<endl ;
		exit(ERROR) ;
	}
	// 用 e 返回 线性表第i个元素;
	if(i<0 || i>L.len)
	exit(ERROR);
	e = L.elem[i-1];
	return OK ;
}
int LocateElem(sqList &L , ElemType e )
{
	// operator if 返回L中第一个与e 满足compare() 的数据元素的维序.不存在返回0 ;
	if(L.elem==NULL)
	{
		cout<<"空表"<<endl ;
		exit(ERROR) ;
	}
	int i= 0  ;
	while(L.elem[i]<=e)
	{
		i++ ;
	}
	if(i>=L.len)
	return 0 ;
	else
	return i+1 ;

}
Status ListInsert(sqList &L ,int i ,ElemType e)
{
	// 初始条件: 线性表存在, 且1<= i <= Capacity ;
	if(L.elem==NULL)
	exit(ERROR) ;
	if(i<1 ||i>L.len+1)
    exit(ERROR) ;
	if(L.len>=L.capacity)
	{
		// 如果线性表容量不够,动态增加;
		ElemType *newelem = (ElemType *)realloc(L.elem ,sizeof(ElemType)*(INIT_SIZE+LIST_SIZE)) ;
		if(L.elem==NULL)
		{
			cout<<"fail "<<endl;
			exit(ERROR);
		}
		L.elem = newelem ;
		L.capacity+=LIST_SIZE;

		
	}
	ElemType *q = &L.elem[i-1];
	ElemType *p = &L.elem[L.len-1];
	for(;p>=q ;--p)
	{
		*(p+1) = *p ;
		
	}
	*q = e ;
	++L.len ;
	return OK ;
}
void MergeList(sqList &La ,sqList &Lb ,sqList &Lc)
{
	// 已知线性表La 和 Lb中的数据元素按值非递减排列;
	// 归并La 和 Lb 得到新的线性表Lc ,Lc的数据元素也按非递减排列;
	InitList(Lc); // 初始化Lc ;
	int i ,j , k;
	int ai ,bj ;
	i = j = 1 ;k = 0 ;
	int lalen = La.len ;
	int lblen = Lb.len ;
	while(i<=lalen  && j <=lblen )
	{
		GetElem(La,i,ai); // 从La 中的i位置 取出 数据元素返回给ai
		GetElem(Lb,j,bj);
		if(ai<=bj)
		{
			// 如果ai <= bj 则把ai 插入到Lc中
			ListInsert(Lc,++k,ai);
			i++;
		}
		else
		{
			ListInsert(Lc,++k,bj);
			j++;
			
		}

	}
	// 下面两个只执行一个;
	// 将La或Lb中剩余的元素也按非递减有序插入;
	while(i<=lalen)
	{
		GetElem(La,i++,ai);
		ListInsert(Lc,++k,ai);
	}
	while(j<=lblen)
	{
		GetElem(Lb,j++,bj);
		ListInsert(Lc,++k,bj);
	}
	
}
//未验证

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值