1、结点空间可以动态申请和释放;
2、数据元素的逻辑次序靠结点的指针来指示,插入和删除时不需要移动数据元素。
1、存储密度小,每个结点的指针域需额外占用存储空间。当每个结点的数据域所占字节不多时,指针域所占存储空间的比重显得很大。
2、链式存储结构是非随机存取结构。对任一节点的操作都要从头指针依指针链查找到该结点,这增加了算法的复杂度。
#include
void mergeArrays(int arr1[], int size1, int arr2[], int size2, int mergedArray[]) {
int i, j, k, duplicate;
// 将arr1的元素插入合并数组
for (i = 0; i < size1; i++) {
mergedArray[i] = arr1[i];
}
k = size1;
// 将arr2中非重复元素插入合并数组
for (i = 0; i < size2; i++) {
duplicate = 0;
for (j = 0; j < size1; j++) {
if (arr2[i] == arr1[j]) {
duplicate = 1;
break;
}
}
if (!duplicate) {
mergedArray[k++] = arr2[i];
}
}
}
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int arr2[] = {4, 5, 6, 7, 8};
int size2 = sizeof(arr2) / sizeof(arr2[0]);
// 计算合并数组的最大长度
int maxSize = size1 + size2;
int mergedArray[maxSize];
mergeArrays(arr1, size1, arr2, size2, mergedArray);
// 输出合并并去重后的数组
for (int i = 0; i < size1 + size2; i++) {
printf("%d ", mergedArray[i]);
}
printf("\n");
return 0;
}
#include
using namespace std;
struct Mylist
{
int m_int;
Mylist* myl;
};
int main()
{
Mylist* la = new Mylist;
la->myl = new Mylist;
la->myl->myl = new Mylist;
la->myl->myl->myl = new Mylist;
la->myl->myl->myl->myl = NULL;
la->myl->m_int = 1;
la->myl->myl->m_int = 7;
la->myl->myl->myl->m_int = 8;
Mylist* lb = new Mylist;
lb->myl = new Mylist;
lb->myl->myl = new Mylist;
lb->myl->myl->myl = new Mylist;
lb->myl->myl->myl->myl = new Mylist;
lb->myl->myl->myl->myl->myl = new Mylist;
lb->myl->myl->myl->myl->myl->myl = new Mylist;
lb->myl->myl->myl->myl->myl->myl->myl = NULL;
lb->myl->m_int = 2;
lb->myl->myl->m_int = 4;
lb->myl->myl->myl->m_int = 6;
lb->myl->myl->myl->myl->m_int = 8;
lb->myl->myl->myl->myl->myl->m_int = 10;
lb->myl->myl->myl->myl->myl->myl->m_int = 11;
Mylist* pc = la;
Mylist* lc = la;
Mylist* pa = la->myl;
Mylist* pb = lb->myl;
while (pa&&pb)
{
if (pa->m_int <= pb->m_int)
{
pc->myl = pa; pc = pa; pa = pa->myl;
}
else
{
pc->myl = pb; pc = pb; pb = pb->myl;
}
}
pc->myl = pa ? pa : pb;
delete lb;
pc = lc;
while (pc->myl)
{
cout << pc->myl->m_int << " ";
pc = pc->myl;
}
cout << endl;
return 0;
}
本来以为很简单,全部写在main函数里,结果……
本代码笔者尽可能将会导致程序出错以及内存泄漏的情况规避了。如果还有有缺陷的地方欢迎大佬们指出。
#include
using namespace std;
struct Poly
{
int c;
int e;
Poly* pnext;
};
int main()
{
cout << "本程序用链表方式实现两个一元多项式相加" << endl;
cout << "由低阶到高阶依次输入一元多项式的系数和指数" << endl;
cout << "用空格分隔,回车结束:(系数为0不需要输入)" << endl;
cout << "比如:y(x)=3x^0+4x^1+x^3 输入:3 0 4 1 1 3" << endl;
cout << "提示:输入任意非数字键退出" << endl;
int input;
int ctmp = 0;
int etmp = 0;
int tmp = 0;
int pta = 0;
int ptb = 0;
Poly* Pahead = new Poly;
Pahead->pnext = NULL;
Poly* Pa = Pahead;
Poly* Pbhead = new Poly;
Pbhead->pnext = NULL;
Poly* Pb = Pahead;
Poly* temp = NULL;
Poly* Pc = NULL;
while (true)
{
Pa = Pahead;
Pa = Pa->pnext;
while (Pa)
{
//cout << "Pa删除一项" << endl;
temp = Pa->pnext;
delete Pa;
Pa = temp;
}
Pa = Pahead;
Pb = Pbhead;
Pb = Pb->pnext;
Pahead->pnext = NULL;
while (Pb)
{
//cout << "Pb删除一项" << endl;
temp = Pb->pnext;
delete Pb;
Pb = temp;
}
Pb = Pbhead;
Pbhead->pnext = NULL;
cout << "下面请输入第一个一元多项式一共有几项" << endl;
cout << "=>";
while (true)
{
cin >> input;
if (cin.fail())
{
cin.clear();
cout << "已退出,欢迎再次使用。" << endl;
Pa = Pahead;
Pb = Pbhead;
if (Pahead != NULL)
{
//cout << "删除Pahead。" << endl;
Pa = Pahead;
while (Pa)
{
temp = Pa->pnext;
delete Pa;
Pa = temp;
}
}
if (Pbhead != NULL)
{
//cout << "删除Pbhead。" << endl;
Pb = Pbhead;
while (Pb)
{
temp = Pb->pnext;
delete Pb;
Pb = temp;
}
}
return 0;
}
if (input <= 0)
{
cout << "请至少输入一项" << endl;
continue;
}
if (input > 100)
{
cout << "最大不要超出100项……这么多的多项式爱因斯坦见了都得捂脸……" << endl;
continue;
}
pta = input * 2;
break;
}
cout << "下面请输入第一个一元多项式" << endl;
cout << "=>";
while (pta--)
{
cin >> input;
if (cin.fail())
{
cin.clear();
cout << "已退出,欢迎再次使用。" << endl;
Pa = Pahead;
Pb = Pbhead;
if (Pahead != NULL)
{
//cout << "删除Pahead。" << endl;
Pa = Pahead;
while (Pa)
{
temp = Pa->pnext;
delete Pa;
Pa = temp;
}
}
if (Pbhead != NULL)
{
//cout << "删除Pbhead。" << endl;
Pb = Pbhead;
while (Pb)
{
temp = Pb->pnext;
delete Pb;
Pb = temp;
}
}
return 0;
}
if (tmp == 0)
{
ctmp = input;
++tmp;
continue;
}
else
{
etmp = input;
Pa->pnext = new Poly;
Pa->pnext->c = ctmp;
Pa->pnext->e = etmp;
Pa = Pa->pnext;
Pa->pnext = NULL;
--tmp;
}
}
int availableChars = (int)cin.rdbuf()->in_avail();
if (availableChars)
{
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "第一个一元多项式:" << endl;
cout << "y(x)=";
Pa = Pahead;
cout << Pa->pnext->c << "x^" << Pa->pnext->e;
Pa = Pa->pnext;
while (Pa->pnext)
{
cout << "+";
cout << Pa->pnext->c << "x^" << Pa->pnext->e;
Pa = Pa->pnext;
}
cout << endl;
//第二个
Pb = Pbhead;
cout << "下面请输入第二个一元多项式一共有几项" << endl;
cout << "=>";
while (true)
{
cin >> input;
if (cin.fail())
{
cin.clear();
cout << "已退出,欢迎再次使用。" << endl;
Pa = Pahead;
Pb = Pbhead;
if (Pahead != NULL)
{
//cout << "删除Pahead。" << endl;
Pa = Pahead;
while (Pa)
{
temp = Pa->pnext;
delete Pa;
Pa = temp;
}
}
if (Pbhead != NULL)
{
//cout << "删除Pbhead。" << endl;
Pb = Pbhead;
while (Pb)
{
temp = Pb->pnext;
delete Pb;
Pb = temp;
}
}
return 0;
}
if (input <= 0)
{
cout << "请至少输入一项" << endl;
continue;
}
if (input > 100)
{
cout << "最大不要超出100项……这么多的多项式爱因斯坦见了都得捂脸……" << endl;
continue;
}
ptb = input * 2;
break;
}
cout << "下面请输入第二个一元多项式" << endl;
cout << "=>";
while (ptb--)
{
cin >> input;
if (cin.fail())
{
cin.clear();
cout << "已退出,欢迎再次使用。" << endl;
Pa = Pahead;
Pb = Pbhead;
if (Pahead != NULL)
{
//cout << "删除Pahead。" << endl;
Pa = Pahead;
while (Pa)
{
temp = Pa->pnext;
delete Pa;
Pa = temp;
}
}
if (Pbhead != NULL)
{
//cout << "删除Pbhead。" << endl;
Pb = Pbhead;
while (Pb)
{
temp = Pb->pnext;
delete Pb;
Pb = temp;
}
}
return 0;
}
if (tmp == 0)
{
ctmp = input;
++tmp;
continue;
}
else
{
etmp = input;
Pb->pnext = new Poly;
Pb->pnext->c = ctmp;
Pb->pnext->e = etmp;
Pb = Pb->pnext;
Pb->pnext = NULL;
--tmp;
}
}
availableChars = (int)cin.rdbuf()->in_avail();
if (availableChars)
{
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "第二个一元多项式:" << endl;
cout << "z(x)=";
Pb = Pbhead;
cout << Pb->pnext->c << "x^" << Pb->pnext->e;
Pb = Pb->pnext;
while (Pb->pnext)
{
cout << "+";
cout << Pb->pnext->c << "x^" << Pb->pnext->e;
Pb = Pb->pnext;
}
cout << endl;
//求和
Pa = Pahead->pnext;
Pc = Pahead;
Pb = Pbhead->pnext;
while (Pa&&Pb)
{
if (Pa->e < Pb->e)
{
Pc->pnext = Pa; Pc = Pa; Pa = Pa->pnext;
}
else if (Pa->e > Pb->e)
{
Pc->pnext = Pb; Pc = Pb; Pb = Pb->pnext;
}
else
{
if ((Pa->c + Pb->c) == 0)
{
temp = Pb;
Pb = Pb->pnext;
delete temp;
temp = Pa;
Pa = Pa->pnext;
delete temp;
}
else
{
Pc->pnext = Pa; Pc = Pa; Pa = Pa->pnext;
Pc->c += Pb->c;
temp = Pb;
Pb = Pb->pnext;
delete temp;
}
}
}
Pc->pnext = Pa ? Pa : Pb;
Pbhead->pnext = NULL;//必须设置为NULL,要不然下次对Pbhead进行释放时会造成野指针
cout << "二个一元多项式之和为:" << endl;
Pc = Pahead;
cout << "y(x)+z(x)=";
if (!Pc->pnext)//存在两个多项式相加为零的情况!
{
cout << 0 << endl;
Pc = Pahead;
continue;
}
cout << Pc->pnext->c << "x^" << Pc->pnext->e;
Pc = Pc->pnext;
while (Pc->pnext)
{
cout << "+";
cout << Pc->pnext->c << "x^" << Pc->pnext->e;
Pc = Pc->pnext;
}
cout << endl;
Pc = Pahead;
}
return 0;
}
案例实现方式与第一百零六天写的资源管理系统类似,这里不再赘述。