<数据结构>顺序表

#include<iostream>
#include<malloc.h> 
#include<stdio.h>
using namespace std;

/**——————————线性表的动态分配顺序存储结构——————————**/

#define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
#define LIST_INCREMENT 20  //线性表存储空间的分配增量
#define ElemType int       //设置元素类型为int 
//#define OK 1  
//#define ERROR 0  
//#define OVERFLOW -2
typedef struct 
{
	ElemType *elem;			//表头地址??可以这样理解吧
	int length;				//顺序表的现有长度
	int listsize;			//分配的存储容量(以sizeof(ElemType)为单位)
}SqList;

//初始化 线性表
int initSqList(SqList &L)
{
	L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
	if(!L.elem) exit(-2);		//存储分配失败
	L.length = 0;				//初始化为空表
	L.listsize = LIST_INCREMENT;//初始表的存储容量,为LIST_INIT_SIZE个elemType单位rn 
	return 1;
}


//在 pos 处 插入元素 e
int InsertList(SqList &L,int pos ,ElemType e)
{
	ElemType *newbase;
	if (pos<1||(pos>L.length+1))
		return 0;//pos不在顺序表长度范围内
	if(L.length >= L.listsize)
	{
		newbase = (ElemType*)realloc(L.elem,(LIST_INIT_SIZE+LIST_INCREMENT)*sizeof(ElemType));
		if(!newbase) exit(-2);		//重新存储分配失败
		L.elem = newbase;			//新基址
		L.listsize  += LIST_INCREMENT;	//增加存储空间
	}
	ElemType *q,*p;
	q = &L.elem[pos-1];   //插入地址
	for (p = &L.elem[L.length-1]; p>=q; --p)	//p为顺序表最后一位 
	{
		*(p+1)=*p;								//后移

	}
	*q = e;
	++L.length;
	return 1;
}




//在pos处删除元素,并用e返回其值
int deleteList(SqList &L,int pos,ElemType &e)
{
	ElemType *p,*q;
	if (pos<1||pos>L.length)
	{
		cout << "地址不合理";
		return 0;
	}
	p = &L.elem[pos-1];
	q = &L.elem[L.length-1];
	e = *p;
	for(p ++ ;p<=q;p++)
		*(p-1) = *p;
	L.length--;
}

//遍历线性表
void Traverse(SqList &L)
{
	int i ;
	for (i = 0; i< L.length;i++)
	{
		cout << L.elem[i]<<"\t";
	}
	cout<<endl;

}

//创建一个顺序表
void CreateList(SqList &L)
{
	int length;
	ElemType e; 
	cout << "请输入表长:"<<endl;
	cin >> length;  
	cout << "请输入元素:"<<endl;
	for (int i = 0; i < length; i++)
	{
		cin >> e;
		InsertList(L,1,e);
	} 
	Traverse(L);
}


//两表合并
void MergeList(SqList La,SqList Lb,SqList &Lc)
{
	ElemType *pa,*pb,*pc;
	pa = La.elem;
	pb = Lb.elem;
	Lc.length = La.length+Lb.length;
	Lc.listsize = Lc.length;
	Lc.elem= (ElemType *)malloc(Lc.listsize*sizeof(ElemType));
	pc = Lc.elem;
	if(!Lc.elem)  /*exit(-2)*/
		cout<<"分配存储失败";  //分配if存储失败
	/*&Lb.elem[Lb.length-1]*/
	while ((pa <= &Lb.elem[Lb.length-1])&&pb <=( Lb.elem+Lb.length-1))
	{
		/*	int i,j;
		for (i=0;i<La.length;i++)
		{
		for(j=0;j<Lb.length;j++)
		{
		if(La.elem[i]<=Lb.elem[j])*/
		if(*pa<=*pb)
		{
			*(pc++) = *(pa++);
		}else
		{
			*(pc++ )= *(pb++);
		}
		/*	}
		}*/
	}
	while (pa< &La.elem[La.length-1]) *(pc++) = *(pa++);
	while (pb < &Lb.elem[Lb.length-1]) 
	{
		*(pc++ )= *(pb++);
	}
}
int main ()
{
	// SqList L;
	// initSqList(L);
	// int length,i,n;
	 cout << "请输入表长:"<<endl;
	// //cin >> length; 
	// CreateList(L);
	// ElemType e; 
	// int pos;
	// cout << "请输入插入的位置:"<<endl;      
	// cin>>pos; 
	// cout << "请输入插入的元素:"<<endl; 
	// cin >> e;
	// InsertList(L,pos,e); 
	// Traverse(L);
	// cout << "请输入要删除的元素位置:"<<endl;
	// cin>>i;
	// deleteList(L,i,n);
	// Traverse(L);
	// cout<<"所删除的元素是:";
	// cout<<n<<endl;
	cout<<"新建顺序表La、Lb:";
	SqList La,Lb,Lc;
	initSqList(La);
	initSqList(Lc);
	initSqList(Lb);
	CreateList(La);
	CreateList(Lb);
	cout<<"合并表La和Lb:";
	MergeList(La,Lb,Lc);
	Traverse(Lc);
	system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值