#include<iostream>
#include<malloc.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ERROR 0
#define OK 1
using namespace std;
typedef struct{
int *elem;
int length;
int listsize;
}sqlist;
sqlist Sqcreat(sqlist L)
{
L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem) cout<<"空间分配失败"<<endl;
L.length=0;
L.listsize = LIST_INIT_SIZE;
int n;
cout<<"请输入所需元素值"<<endl;
cin>>n;
cout<<"请输入"<<n<<"个元素"<<endl;
for(int i=0;i<n;i++)
{
cin>>L.elem[i];
L.length++;
}
return L;
}
int Destroy(sqlist L)
{
if(!L.elem) return ERROR;
free(L.elem);
L.elem=NULL;
L.length=0;
L.listsize=0;
return OK;
}
int empty(sqlist L)
{
if(!L.elem) return ERROR;
if(L.length==0)cout<<"线性表为空"<<endl;
else cout<<"线性表为不为空"<<endl;
}
int length(sqlist L)
{
if(!L.elem) return ERROR;
cout<<"线性表的长度为"<<L.length<<endl;
}
int Get(sqlist L,int i)
{
if(!L.elem) return ERROR;
if((i<1)||(i>L.length )) return ERROR;
return L.elem[i-1];
}
int GetElemSq(sqlist L,int i,int &e)//属于上个函数的一部分
{
e=L.elem[i-1];
return e;
}
int qian(sqlist L,int i)
{
if(!L.elem) return ERROR;
if((i<2)||(i>L.length )) return ERROR;
cout<<"第"<<i<<"个元素的前驱为"<<L.elem[i-2]<<endl;
return OK;
}
int hou(sqlist L,int i)
{
if(!L.elem) return ERROR;
if((i<1)||(i>L.length-1)) return ERROR;
cout<<"第"<<i<<"个元素的后驱为"<<L.elem[i]<<endl;
return OK;
}
int cha(sqlist &L,int i,int e)
{
int *newbase,*p,*q;
if(!L.elem) return ERROR;
if((i<1)||(i>L.length+1)) return ERROR;
if(L.length>=L.listsize)
{
newbase=(int*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
if(!newbase) cout<<"重新分配失败"<<endl;
L.elem=newbase;
L.listsize=L.listsize+LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
{
*(p+1)=*p;
}
*q=e;
L.length++;
return OK;
}
int Delete(sqlist &L,int i,int e)
{
if ((i<1)||(i>L.length)) return ERROR;
e = L.elem[i-1];
int j;
for (j= i; j<= L.length-1; ++j)
L.elem[j-1] = L.elem[j];
--L.length;
}
int xianshi(sqlist L)
{
if(!L.elem) return ERROR;
cout<<L.length<<endl;
for(int i=0;i<L.length;i++)
{
cout<<L.elem[i]<<" ";
}
cout<<endl;
}
int LenthSq_(sqlist &L)//属于上个函数的一部分
{
return L.length;
}
sqlist InitListSq(sqlist &L)
{
int n,i;
L.elem=(int*)malloc(LIST_INIT_SIZE *sizeof(int));
L.length=0;
L.listsize=LIST_INIT_SIZE;
return L;
}
int MergeList_Sq(sqlist La,sqlist Lb,sqlist &Lc)
{
int i,j,k;
int ai,bj;
int La_len,Lb_len;
i=j=1;
k=0;
La=Sqcreat(La);
Lb=Sqcreat(Lb);
InitListSq(Lc);
La_len=LenthSq_(La); Lb_len=LenthSq_(Lb);
while((i<=La_len)&&(j<=Lb_len))
{
cout<<"La:"<<GetElemSq(La,i,ai)<<" ";//分别获取La与Lb中各个位置上的元素值
cout<<"Lb:"<<GetElemSq(Lb,j,bj)<<" ";
if(ai<=bj)
{
cha(Lc,++k,ai);
++i;
}
else
{
cha(Lc,++k,bj);
++j;
}
}
while(i<=La_len)
{
GetElemSq(La,i++,ai);
cha(Lc,++k,ai);
}
while(j<=Lb_len)
{
GetElemSq(Lb,j++,bj);
cha(Lc,++k,bj);
}
cout<<"归并后的新顺序表:"<<endl;
xianshi(Lc);
cout<<endl;
}
int show()
{
cout<<"0----初始化并创建顺序表"<<endl;
cout<<"1----清空线性表"<<endl;
cout<<"2----判断线性表是否为空"<<endl;
cout<<"3----求线性表长度"<<endl;
cout<<"4----获取线性表指定位置元素"<<endl;
cout<<"5----求前驱"<<endl;
cout<<"6----求后继"<<endl;
cout<<"7----在线性表指定位置前插入元素"<<endl;
cout<<"8----删除线性表指定位置元素"<<endl;
cout<<"9----归并两个线性表"<<endl;
cout<<"10----显示线性表"<<endl;
cout<<" 退出,输出一个负数!"<<endl;
}
int main()
{
sqlist L;
sqlist La;
sqlist Lb;
sqlist Lc;
show();
int n;
while(1)
{
cout<<"请输入您想要进行的操作"<<endl;
cin>>n;
if(n==0)
L=Sqcreat(L);
if(n==1)
Destroy(L);
if(n==2)
empty(L);
if(n==3)
length(L);
if(n==4)
{
int a;
cout<<"请输入您想要获取元素的位置"<<endl;
cin>>a;
cout<<a<<"上的元素是"<<Get(L,a)<<endl;
}
if(n==5)
{
int i;
cout<<"请输入你要找的位置"<<endl;
cin>>i;
qian(L,i);
}
if(n==6)
{
int i;
cout<<"请输入你要找的位置"<<endl;
cin>>i;
hou(L,i);
}
if(n==7)
{
int i,e;
cout<<"请输入你要插的位置"<<endl;
cin>>i;
cout<<"请输入你要插入元素的值"<<endl;
cin>>e;
cha(L,i,e);
}
if(n==8)
{
int i,e;
cout<<"请输入你要删除的位置"<<endl;
cin>>i;
Delete(L,i,e);
}
if(n==9)
{
MergeList_Sq(La,Lb,Lc);
}
if(n==10)
{
xianshi(L);
}
}
}