#include<iostream>
using namespace std;
#define MAXSIZE 100
typedef struct
{
int* elem;
int length;
int maxsize;
}Sqlist;
int Initlist(Sqlist& L)//初始化线性表
{
L.elem = new int[MAXSIZE];
if (!L.elem)exit(OVERFLOW);
L.length = 0;
return 1;
}
void create(Sqlist& L)//初始化
{
for (int i = 1; i <= 10; i++)
{
L.elem[i - 1] = i;
L.length++;
}
}
void inputsqlist(Sqlist L)//输出线性表
{
for (int i = 1; i <= L.length; i++)
{
cout << L.elem[i - 1] << " ";
}
}
int Getelem(Sqlist& L, int i, int& e)//获取元素
{
if ((i < 1) || (i > L.length))return -1;
e = L.elem[i - 1];
return 1;
}
int ListInsert(Sqlist& L, int i, int e)//插入操作
{
if ((i < 1) || (i > L.length + 1)) { return 0; }
if (L.length == MAXSIZE)return 0;
for (int j = L.length; j >= i; j--)
{
L.elem[j] = L.elem[j - 1];
}
L.elem[i - 1] = e;
++L.length;
return 1;
}
int Listdelete(Sqlist& L, int i)//删除操作
{
if ((i < 1) || (i > L.length)) { return 0; }
for (int j = i; j <= L.length - 1; j++)
{
L.elem[j - 1] = L.elem[j];
}
--L.length;
return 1;
}
int Locatelem(Sqlist L, int e)//查找
{
for (int i = 0; i <= L.length - 1; i++)
{
if (L.elem[i] == e) return i + 1;//查找成功
}
return 0;//查找失败
}
int Listlength(Sqlist L)//返回线性表
{
return L.length;
}
void addlist(Sqlist& a, Sqlist b)//无序
{
int c = Listlength(a);
for (int i = 1; i <= b.length; i++)
{
int temp;
Getelem(b, i, temp);
if (!Locatelem(a, temp))
{
ListInsert(a, ++c, temp);
}
}
//for (int i = 1; i <= a.length-1; i++)
//{
// for (int j = i; j <= a.length - i; j++)
// {
// if (a.elem[j] < a.elem[j - 1])
// {
// int temp = a.elem[j-1];
// a.elem[j - 1] = a.elem[j];
// a.elem[j] = temp;
// }
// }
//}
}
void MergeLIst(Sqlist a, Sqlist b, Sqlist& c)//有序
{
c.length = a.length + b.length;
c.elem = new int[c.length];
int* pc = c.elem;
int* pa = a.elem;
int* pb = b.elem;
int* pa_last = a.elem + a.length - 1;
int* pb_last = b.elem + b.length - 1;
while ((pa <= pa_last) && (pb <= pb_last))
{
if (*pa < *pb)*pc++ = *pa++;
else if (*pa > *pb)
{
*pc++ = *pb++;
}
else if (*pa == *pb)
{
*pc++ = *pa++;
pb++;
c.length--;
}
}
while (pa <= pa_last)*pc++ = *pa++;
while (pb <= pb_last)*pc++ = *pb++;
}
int main()
{
Sqlist m;//创建线性表m
Initlist(m);//初始化线性表
create(m);//赋值
cout << "第一个线性表为:" << endl;
inputsqlist(m);//输出线性表m
cout << endl;
int i;
cout << "请输入你想插入的数:";
cin >> i;
int xy;
cout << "请输入你想插入数的位置:";
cin >> xy;
ListInsert(m, xy, i);//插入
cout << "插入后的线性表为:" << endl;
inputsqlist(m);
cout << endl;
int ds;
cout << "请输入你想删除数的位置:";
cin >> ds;
Listdelete(m, ds);//删除
cout << "删除后的线性表为:" << endl;
inputsqlist(m);
cout << endl;
int cs;
cout << "请输入你想查找的数字:" << endl;
cin >> cs;
cout << "该数序号为:" << Locatelem(m, cs) << endl;//查找
Sqlist n;
Initlist(n);
cout << "请输入第二个线性表:" << endl;
for (int i = 1; i <= 5; i++)
{
cin >> n.elem[i - 1];
++n.length;
}
cout << "第二个线性表为:" << endl;
inputsqlist(n); cout << endl;//输出线性表n
addlist(m, n);
cout << "两个无序线性表的并集为:" << endl;
inputsqlist(m); cout << endl;
Sqlist la;
Initlist(la);
cout << "请输入第三个线性表:" << endl;
for (int i = 1; i <= 4; i++)
{
cin >> la.elem[i - 1];
++la.length;
}
Sqlist lb;
Initlist(lb);
cout << "请输入第四个线性表:" << endl;
for (int i = 1; i <= 7; i++)
{
cin >> lb.elem[i - 1];
++lb.length;
}
Sqlist lc;
MergeLIst(la, lb, lc);
cout << "两个有序表合并后为:" << endl;
inputsqlist(lc);
}
线性表顺序存储
最新推荐文章于 2024-07-29 14:29:21 发布