/*
*
*数据结构<一>
*顺序存储链表的实现
*/
#include<iostream>
using namespace std;
typedef int ElemType;
#define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
#define LISTINCREMENT 10 //线性表存储空间的分配增量
typedef struct
{
ElemType *elem; //存储空间基地址,elem 为指向int型的指针
int length; //保存当前线性表的长度
int listsize; //当前分配的存储容量(以sizeof(ElemType)为单位)
}SqList;
//构造一个空的线性表
int InitList(SqList &L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)
return 0;
L.listsize=LIST_INIT_SIZE;
L.length=0;
return 1;
}
//销毁线性表线性表
void DestroyList(SqList &L)
{
if(L.elem!=NULL){
free(L.elem);
L.elem=NULL;
cout<<"线性表销毁成功!"<<endl;
}
}
//将L重置为空表
void ClearList(SqList &L)
{
if(L.elem!=NULL){
L.length=0;
cout<<"链表置空完成!"<<endl;
}
}
//若L为空表,则返回TRUE,否则返回FALSE
int ListEmpty(SqList L)
{
if (L.elem)
{
if(L.length==0){
cout<<"链表为空"<<endl;
return 1;
}
else
cout<<"链表非空"<<endl;
}
return 0;
}
//输出L中数据元素的个数
int ListLength(SqList L)
{
if(L.elem){
//cout<<"表中数据元素的个数为:"<<L.length<<endl;
return L.length;
}
return 0;
}
//用e返回L中第i个数据元素的值
int GetElem(SqList L,int i,ElemType &e)
{
if(L.elem)
if(i>=0 && i<=ListLength(L)){
e=L.elem[i-1];//第i个数据元素在表中的位置是i-1
//cout<<"表中第 "<<i<<" 个数据元素为 "<<e<<endl;
return e;
}
return 0;
}
//返回L中第1个与e满足相等关系的数据元素的位序。若这样的元素不存在,则返回0值
void LocateElem(SqList L,ElemType e)
{
int i;
if(L.elem)
{
if(L.length!=0){
for(i=0;i<L.length;i++){
if(L.elem[i]==e){
cout<<"所查找的元素 "<<e<<" 出现在顺序存储表的第 "<<i+1<<" 个位置"<<endl;
}
}
}
}
}
//若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,否则操作失败,pre_e无定义
void PriorElem(SqList L,ElemType cur_e,ElemType &pre_e)
{
int i;
if(L.elem){
if(L.length){
for(i=0;i<L.length;i++){
if(L.elem[i]==cur_e && i==0)
{
cout<<"该位置元素没有前驱"<<endl;
}
if(L.elem[i]==cur_e && i!=0)//第一个元素没有前驱
{
pre_e=L.elem[i-1];
cout<<"数据元素 "<<i<<" 的前驱为 "<<pre_e<<endl;
}
}
}
}
}
//若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,否则操作失败,next_e无定义
void NextElem(SqList L,ElemType cur_e,ElemType &next_e)
{
int i;
if(L.elem){
if(L.length){
for (i=0;i<L.length;i++)
{
if(L.elem[i]==cur_e && i==L.length-1)
{
cout<<"该位置元素没有后继"<<endl;
}
if(L.elem[i]==cur_e && i!=L.length-1)
{
next_e=L.elem[i+1];
cout<<"数据元素 "<<i<<" 的后继为 "<<next_e<<endl;
}
}
}
}
}
//在L中第i个位置之前插入新的数据元素e,L的长度加1
void ListInsert(SqList &L,int i,ElemType e)
{
ElemType *p,*q,*newbase;
if(L.elem)
if(i>0 && i<=L.length)
if(L.length>=L.listsize){
newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)
cout<<"Failed!"<<endl;
L.elem=newbase;
L.listsize += LISTINCREMENT;
}
q=&(L.elem[i-1]);//q为插入位置
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;//插入元素之后的元素右移
*q=e;//插入元素
++L.length;//表长增1
//cout<<L.length<<endl;
}
//删除L的第i个数据元素,并用e返回其值,L的长度减1
void ListDelete(SqList &L,int i,ElemType &e)
{
ElemType *p,*q;
if(L.elem)
if(i>=0 && i<=L.length)
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;//表尾元素的位置
for (++p;p<=q;++p)
{
*(p-1)=*p;//删除后位置前移
}
--L.length;
cout<<"删除操作成功!"<<endl;
}
//依次对L的每个数据元素调用visit(),一旦visit()失败,则操作失败
void ListTraverse(SqList L)
{
int i;
if(L.elem){
cout<<"顺序存储表中存储的元素为:";
for(i=0;i<L.length;i++)
cout<<L.elem[i]<<" ";
cout<<endl;
}
else
cout<<"线性表为空!"<<endl;
}
//已知线性表La,Lb中的数据元素按值非递减排列
//归并La,Lb得到新的线性表Lc
void MergeList(SqList La,SqList Lb,SqList &Lc)
{
int i,j,k=0;
int a[4]={3,5,8,11},b[7]={2,6,8,9,11,15,20};
int La_len,Lb_len;
for(i = 1 ; i < 5 ; i++)
ListInsert(La,i,a[i-1]);
cout<<"线性表La的元素为:";
for(i = 0 ; i < La.length; i++)
cout<<La.elem[i]<<" ";
cout<<endl;
for(j = 1 ; j < 8 ; j++)
ListInsert(Lb,j,b[j-1]);
cout<<"线性表Lb的元素为:";
for(j = 0 ; j < Lb.length ; j++)
cout<<Lb.elem[j]<<" ";
cout<<endl;
InitList(Lc);
i=j=1;
La_len=La.length;
Lb_len=Lb.length;
while((i <= La_len) && (j <= Lb_len))
{
GetElem(La,i,a[i-1]);
GetElem(Lb,j,b[j-1]);
if(a[i-1] <= b[j-1])
{
ListInsert(Lc,++k,a[i-1]);
++i;
}
else
{
ListInsert(Lc,++k,b[j-1]);
++j;
}
}
while (i <= La_len)
{
GetElem(La,i,a[i-1]);
ListInsert(Lc,++k,a[i-1]);
i++;
}
while (j <= Lb_len)
{
GetElem(Lb,j,b[j-1]);
ListInsert(Lc,++k,b[j-1]);
j++;
}
cout<<"归并后线性表Lc为:";
for(i=0;i<Lc.length;i++)
cout<<Lc.elem[i]<<" ";
cout<<endl;
}
int main()
{
int i;
ElemType j;
SqList L;
InitList(L);
for(i=1,j=0;i<=10;i++,j++)
ListInsert(L,i,j);//i为插入元素的位置,j为插入的数据*/
ListTraverse(L);
/*
//测试MergeList函数
SqList La,Lb,Lc;
InitList(La);
InitList(Lb);
MergeList(La,Lb,Lc);
*/
/*
//测试ListDelete函数
int position,m
cout<<"请输入需要删除元素的位置:"<<endl;
cin>>position;
ListDelete(L,position,m);
*/
/*
//测试PriorElem和NextElem函数
ElemType pre_e;
ElemType next_e;
ElemType cur_e;
cout<<"请输入需要查找的元素:"<<endl;
cin>>cur_e;
PriorElem(L,cur_e,pre_e);
NextElem(L,cur_e,next_e);
*/
/*
//测试LocateElem函数
ElemType compNum;
cout<<"请输入元素:";
cin>>compNum;
LocateElem(L,compNum);
*/
/*
//测试GetElem函数
ElemType elem;
int k;
cout<<"请输入需要获取的第k个元素,k=";
cin>>k;
GetElem(L,k,elem);
*/
/*
//测试ListLength函数
int len;
len=ListLength(L);
*/
/*
//测试ListEmpty函数
ListEmpty(L);
*/
/*
//测试ClearList函数
ClearList(L);
*/
/*
//测试DestroyList函数
DestroyList(L);
ListTraverse(L);
*/
return 0;
}