仅供参考!
#include
#include
#include
using namespace std;
int a_assign=0;
int b_assign=0;
int a_success=0;
int b_success=0;
int c_success=0;
#define true 1
#define false 0
#define ok 1
#define error 0
#define infeasible -1
#define overflow -2
#define list_init_size 100 //线性表存储空间的初始分配量
#define list_increment 10 //线性表存储空间的分配增量
typedef int Satus;
typedef int ElemType;
typedef struct List{
ElemType *elem; //存储空间基址
int length; //当前长度
int listsize; //当前分配的存储容量
}List;
void catalog();
Satus Init_List_a( List &La );
Satus Init_List_b( List &Lb );
Satus Destory( List &La,List &Lb );
Satus Clear( List &La,List &Lb );
void Judge( List &La,List &Lb );
void GetLength( List &La,List &Lb );
void GetelemByIndex( List &La,List &Lb );
void GetIndexByelem( List &La,List &Lb );
void Getfront( List &La,List &Lb );
void Getbehind( List &La,List &Lb );
void InsertElem( List &La,List &Lb );
void DeleteElem( List &La,List &Lb );
void PrintElem( List &La,List &Lb );
Satus CombineList( List &La,List &Lb,List &Lc );
int main()
{
int n,flag=1;//设定一个flag来判断循环需要的次数
while( flag )
{
catalog();
cout<<"请输入要进行的功能:";
cin>>n;
List La,Lb,Lc;
switch( n )
{
case 1:
Init_List_a(La);
Init_List_b(Lb);
break;
case 2:
Destory(La,Lb);
break;
case 3:
Clear(La,Lb);
break;
case 4:
Judge(La,Lb);
break;
case 5:
GetLength(La,Lb);
break;
case 6:
GetelemByIndex(La,Lb);
break;
case 7:
GetIndexByelem(La,Lb);
break;
case 8:
Getfront(La,Lb);
break;
case 9:
Getbehind(La,Lb);
break;
case 10:
InsertElem(La,Lb);
break;
case 11:
DeleteElem(La,Lb);
break;
case 12:
PrintElem(La,Lb);
break;
case 13:
CombineList(La,Lb,Lc);
break;
default :
cout<<"输入有误,请重新选择功能进行输入!"<初始化一个线性表 |"<销毁线性表 |"<清空线性表 |"<判断线性表是否为空 |"<求线性表的长度 |"<获取线性表中指定位置的元素 |"<获取线性表元素的位置 |"<求前驱 |"<求后继 |"<在线性表指定位置插入元素 |"<删除线性表指定位置的元素 |"<显示线性表 |"<合并两个非递减有序的线性表 |"< 0 && getchar()!='n' )//限定循环输入数字的次数以及结束循环的条件
{
La.length++;
if( La.length==1 )
La.elem[0]=a;
else
for(int i=0;i 0 && getchar()!='n' )
{
Lb.length++;
if( Lb.length==1 )
Lb.elem[0]=b;
else
for(int i=0;i>i;
if( i==1 )
{
free(La.elem);
La.elem=NULL;
La.length=0;
a_success=0;
cout<<"线性表A销毁成功!" <>list;
if( list==1 )
{
cout<<"请输入你要查询元素的位置:";
cin>>index;
if( index>0 && index<=La.length )
cout<<"你查询的元素为:"<>index;
if( index>0 && index<=Lb.length )
cout<<"你查询的元素为:"<>list;
if( list==1 )
{
cout<<"请输入你要查询的元素:" ;
cin>>elem_num;
for( int i=0;i>elem_num;
for( int i=0;i>list;
if( list==1 )
{
cout<<"输入元素即可得到前驱元素:" ;
cin>>elem_num;
for( int i=0;i>elem_num;
for( int i=0;i>list;
if( list==1 )
{
cout<<"输入元素即可得到前驱元素:" ;
cin>>elem_num;
for( int i=0;i>elem_num;
for( int i=0;i>list;
if( list==1 )
{
cout<<"请输入你要插入的位置:" <<"(范围是:1 - "<>index;
cout<<"请输入你要插入的元素值:";
cin>>insert_num;
if( index<1 || index>La.length+1 )
cout<<"插入位置无效,插入失败!"<=La.listsize )
{ //存储空间不够用
//申请新的空间,newbase为基地址
ElemType *newbase=(ElemType*)malloc((La.listsize+list_increment)*sizeof(ElemType));
if( !newbase )
{
cout<<"新开辟空间失败!"<=q;--p )
*(p + 1) = *p;
*q=insert_num;
La.length++;
cout<<"插入成功!"<>index;
cout<<"请输入你要插入的元素值:";
cin>>insert_num;
if( index<1 || index>Lb.length+1 )
cout<<"插入位置无效,插入失败!"<=Lb.listsize )
{//存储空间不够用
//申请新的空间,newbase为基地址
ElemType *newbase = (ElemType *)malloc((Lb.listsize + list_increment) * sizeof(ElemType));
if(!newbase) {
cout<<"新开辟空间失败!";
return;
}
else{
Lb.elem = newbase;
Lb.listsize+=list_increment;
}
}
//元素插入,index位置的元素对应 elem[index -1]
//即最后一个元素的位置为 [index-1]
//q 为插入元素的位置 地址
int *q = &(Lb.elem[index-1]) ;
//P 开始指向最后一个元素的地址循环向前移动使 index 位置及后面的所有元素依次向后,
//直到p移动到插入元素的位置 ,把需要插入的元素覆盖当前插入位置的元素即可
for(int *p = &(Lb.elem[Lb.length - 1]);p >= q;p--)
*(p + 1) = *p;
*q=insert_num;
Lb.length++;
cout<<"插入成功!" <>list;
if( list==1 )
{
cout<<"请输入你要删除的元素位置:"<<"(范围:1 - "<>index;
if( index<1 || index>La.length )
cout<<"删除元素的位置无效,删除失败!"<>index;
if( index<1 || index>Lb.length )
cout<<"删除元素的位置无效,删除失败!"<>list;
if( list==1 )
{
cout<<"线性表A:";
for(int i=0;i