2024-3-13

Change encoding
This commit is contained in:
2024-03-13 11:32:13 +08:00
parent b671321535
commit 3c1d1432d5
9 changed files with 568 additions and 568 deletions

View File

@@ -1,4 +1,4 @@
// 2-1-SqList-顺序表 // 2-1-SqList-顺序表
#include <iostream>//cout,cin #include <iostream>//cout,cin
using namespace std; using namespace std;
@@ -8,147 +8,147 @@ char pause;
void dispmenu() { void dispmenu() {
//显示主菜单 //显示主菜单
cout << endl; cout << endl;
cout << "*** 顺 序 表 ***\n"; cout << "*** 顺 序 表 ***\n";
cout << " 1-初始化顺序表\n"; cout << " 1-初始化顺序表\n";
cout << " 2-创建顺序表\n"; cout << " 2-创建顺序表\n";
cout << " 3-获取第i个元素\n"; cout << " 3-获取第i个元素\n";
cout << " 4-按值查找元素位序\n"; cout << " 4-按值查找元素位序\n";
cout << " 5-插入第i个元素\n"; cout << " 5-插入第i个元素\n";
cout << " 6-删除第i个元素\n"; cout << " 6-删除第i个元素\n";
cout << " 7-修改第i个元素\n"; cout << " 7-修改第i个元素\n";
cout << " 8-清空表\n"; cout << " 8-清空表\n";
cout << " 9-测表长\n"; cout << " 9-测表长\n";
cout << " 10-测表空\n"; cout << " 10-测表空\n";
cout << " 11-测表满\n"; cout << " 11-测表满\n";
cout << " 12-显示表\n"; cout << " 12-显示表\n";
cout << " 13-按值查找前驱\n"; cout << " 13-按值查找前驱\n";
cout << " 0-退出\n"; cout << " 0-退出\n";
} }
//主函数 //主函数
void main() { void main() {
int i; int i;
int e, pre_e; int e, pre_e;
SqList<int> L; //建立容量为20、元素类型为整型的空顺序表 SqList<int> L; //建立容量为20、元素类型为整型的空顺序表
system("cls"); //执行系统命令cls清屏 system("cls"); //执行系统命令cls清屏
int choice; int choice;
do { do {
dispmenu(); //显示主菜单 dispmenu(); //显示主菜单
cout << "Enter choice(1~120 退出):"; cout << "Enter choice(1~120 退出):";
cin >> choice; cin >> choice;
switch (choice) { switch (choice) {
case 1: //初始化线性表 case 1: //初始化线性表
cout << "请输入要创建的顺序表的长度:"; cout << "请输入要创建的顺序表的长度:";
cin >> i; cin >> i;
cout << endl; cout << endl;
InitList(L, i); InitList(L, i);
cout << endl << "创建成功!" << endl; cout << endl << "创建成功!" << endl;
break; break;
case 2: //创建线性表 case 2: //创建线性表
cout << "请输入要创建的元素个数:"; cout << "请输入要创建的元素个数:";
cin >> i; cin >> i;
cout << endl; cout << endl;
CreateList(L, i); CreateList(L, i);
cout << "创建的顺序表元素为:\n"; //显示表元素 cout << "创建的顺序表元素为:\n"; //显示表元素
DispList(L); DispList(L);
cout << endl; cout << endl;
break; break;
case 3: //获取第i个元素 case 3: //获取第i个元素
cout << "请输入元素序号:"; cout << "请输入元素序号:";
cin >> i; cin >> i;
cout << endl; cout << endl;
if (GetElem_i(L, i, e)) if (GetElem_i(L, i, e))
cout << endl << "" << i << "个元素为:" << e << endl; cout << endl << "" << i << "个元素为:" << e << endl;
else else
cout << endl << "元素不存在!" << endl; cout << endl << "元素不存在!" << endl;
break; break;
case 4: //按值查找 case 4: //按值查找
cout << "请输入要查询的元素值:"; cout << "请输入要查询的元素值:";
cin >> e; cin >> e;
i = LocateElem_e(L, e); i = LocateElem_e(L, e);
if (i) if (i)
cout << endl << e << "是第" << i << "个数据元素" << endl; cout << endl << e << "是第" << i << "个数据元素" << endl;
else else
cout << endl << "不存在此元素!" << endl; cout << endl << "不存在此元素!" << endl;
break; break;
case 5: // 在第i个位置插入元素 case 5: // 在第i个位置插入元素
cout << "输入插入位置:" << endl; cout << "输入插入位置:" << endl;
cin >> i; cin >> i;
cout << "输入插入元素值:" << endl; cout << "输入插入元素值:" << endl;
cin >> e; cin >> e;
if (InsertElem_i(L, i, e)) { if (InsertElem_i(L, i, e)) {
cout << endl << "插入成功!" << endl; cout << endl << "插入成功!" << endl;
DispList(L); DispList(L);
} }
else else
cout << endl << "插入不成功!" << endl;; cout << endl << "插入不成功!" << endl;;
break; break;
case 6: //删除第i个元素 case 6: //删除第i个元素
cout << "输入删除元素位置:" << endl; cout << "输入删除元素位置:" << endl;
cin >> i; cin >> i;
if (DeleElem_i(L, i)) { if (DeleElem_i(L, i)) {
cout << endl << "删除成功!" << endl; cout << endl << "删除成功!" << endl;
DispList(L); DispList(L);
} }
else else
cout << endl << "删除失败!" << endl; cout << endl << "删除失败!" << endl;
break; break;
case 7: //修改第i个元素的值 case 7: //修改第i个元素的值
cout << "输入修改元素位置:" << endl; cout << "输入修改元素位置:" << endl;
cin >> i; cin >> i;
cout << "输入新元素值:" << endl; cout << "输入新元素值:" << endl;
cin >> e; cin >> e;
if (PutElem(L, i, e)) { if (PutElem(L, i, e)) {
cout << endl << "修改成功!" << endl; cout << endl << "修改成功!" << endl;
DispList(L); DispList(L);
} }
else else
cout << endl << "修改失败!" << endl; cout << endl << "修改失败!" << endl;
break; break;
case 8: // 清空表 case 8: // 清空表
ClearList(L); ClearList(L);
break; break;
case 9: // 测表长 case 9: // 测表长
cout << "表长为:" << ListLength(L) << endl; cout << "表长为:" << ListLength(L) << endl;
break; break;
case 10: //测表空 case 10: //测表空
if (ListEmpty(L)) if (ListEmpty(L))
cout << endl << "空表!" << endl; cout << endl << "空表!" << endl;
else else
cout << endl << "不是空表!" << endl; cout << endl << "不是空表!" << endl;
break; break;
case 11: // 未表满 case 11: // 未表满
if (ListFull(L)) if (ListFull(L))
cout << endl << "表满!" << endl; cout << endl << "表满!" << endl;
else else
cout << endl << "表不满!" << endl; cout << endl << "表不满!" << endl;
break; break;
case 12: //遍历显示表 case 12: //遍历显示表
DispList(L); DispList(L);
cout << endl; cout << endl;
cin.get(pause); cin.get(pause);
system("pause"); system("pause");
break; break;
case 13: case 13:
cout << "测试顺序表为\n"; cout << "测试顺序表为\n";
DispList(L); DispList(L);
cout << "输入查找前驱的元素值:\n"; cout << "输入查找前驱的元素值:\n";
cin >> e; cin >> e;
if (PriorElem_e(L, e, pre_e)) if (PriorElem_e(L, e, pre_e))
cout << e << "的前驱元素为:" << pre_e << endl; cout << e << "的前驱元素为:" << pre_e << endl;
else else
cout << e << "无前驱元素!" << endl; cout << e << "无前驱元素!" << endl;
break; break;
case 0: //退出 case 0: //退出
cout << "结束运行bye-bye!" << endl; cout << "结束运行bye-bye!" << endl;
break; break;
default: //无效选择 default: //无效选择
cout << "无效选择!\n"; cout << "无效选择!\n";
break; break;
} }
} while (choice != 0); } while (choice != 0);

View File

@@ -1,14 +1,14 @@
template <class DT> template <class DT>
struct SqList // 顺序表 struct SqList // 顺序表
{ {
DT* elem; // 表首址 DT* elem; // 表首址
int length; // 表长 int length; // 表长
int size; // 表容量 int size; // 表容量
}; };
//算法2.1 //算法2.1
template <class DT> template <class DT>
bool PriorElem_e(SqList<DT> L, DT e, DT& pre_e) // 求值为e的元素前驱 bool PriorElem_e(SqList<DT> L, DT e, DT& pre_e) // 求值为e的元素前驱
{ {
int k; int k;
k = LocateElem_e(L, e); // k = LocateElem_e(L, e); //
@@ -21,119 +21,119 @@ bool PriorElem_e(SqList<DT> L, DT e, DT& pre_e) //
return true; return true;
} }
//【算法2.2】 初始化 //【算法2.2】 初始化
template <class DT> template <class DT>
bool InitList(SqList<DT>& L, int m) bool InitList(SqList<DT>& L, int m)
{ {
L.elem = new DT[m]; // 申请表空间 L.elem = new DT[m]; // 申请表空间
if (L.elem == NULL) if (L.elem == NULL)
{ {
cout << "未创建成功!"; // 申请不成功,退出 cout << "未创建成功!"; // 申请不成功,退出
exit(1); exit(1);
} }
L.length = 0; // 申请成功属性赋值。空表表长为0 L.length = 0; // 申请成功属性赋值。空表表长为0
L.size = m; // 表容量为m L.size = m; // 表容量为m
return true; return true;
} }
//【算法2.3】 创建表元素 //【算法2.3】 创建表元素
template <class DT> template <class DT>
bool CreateList(SqList<DT>& L, int n) bool CreateList(SqList<DT>& L, int n)
{ {
int i; int i;
if (n > L.size) // 1.元素个数大于表容量,不能创建。 if (n > L.size) // 1.元素个数大于表容量,不能创建。
{ {
cout << "元素个数大于表长,不能创建!" << endl; cout << "元素个数大于表长,不能创建!" << endl;
return false; return false;
} }
cout << "请依次输入" << n << "个元素值:" << endl; // 2.依位序输入各元素值 cout << "请依次输入" << n << "个元素值:" << endl; // 2.依位序输入各元素值
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
cin >> L.elem[i - 1]; cin >> L.elem[i - 1];
L.length = n; // 3.表长为创建的元素个数 L.length = n; // 3.表长为创建的元素个数
return true; return true;
} }
//【算法2.4】 销毁顺序表 //【算法2.4】 销毁顺序表
template <class DT> template <class DT>
void DestroyList(SqList<DT>& L) void DestroyList(SqList<DT>& L)
{ {
delete[] L.elem; // 1.释放表空间 delete[] L.elem; // 1.释放表空间
L.length = 0; // 2.属性赋值 L.length = 0; // 2.属性赋值
L.size = 0; L.size = 0;
} }
//【算法2.5】 获取第i个元素值 //【算法2.5】 获取第i个元素值
template<class DT> template<class DT>
bool GetElem_i(SqList<DT> L, int i, DT& e) bool GetElem_i(SqList<DT> L, int i, DT& e)
{ {
if (i<1 || i>L.length) // 1.位序不合理返回false if (i<1 || i>L.length) // 1.位序不合理返回false
{ {
cout << "该元素不存在!" << endl; cout << "该元素不存在!" << endl;
return false; return false;
} }
e = L.elem[i - 1]; // 2. 否则获取第i个元素值 e = L.elem[i - 1]; // 2. 否则获取第i个元素值
return true; // 返回true return true; // 返回true
} }
//【算法2.6】 按值查找 //【算法2.6】 按值查找
template<class DT> template<class DT>
int LocateElem_e(SqList<DT> L, DT e) int LocateElem_e(SqList<DT> L, DT e)
{ {
for (int i = 1; i <= L.length; i++) // 顺序查找 for (int i = 1; i <= L.length; i++) // 顺序查找
if (L.elem[i - 1] == e) // 1.找到 if (L.elem[i - 1] == e) // 1.找到
return i; // 返回元素位序 return i; // 返回元素位序
return 0; // 2.未找到返回0 return 0; // 2.未找到返回0
} }
//【算法2.7】 //【算法2.7】
template<class DT> template<class DT>
bool InsertElem_i(SqList<DT>& L, int i, DT e) bool InsertElem_i(SqList<DT>& L, int i, DT e)
{ {
if (L.length >= L.size) // 1.表满,不能插入 if (L.length >= L.size) // 1.表满,不能插入
return false; return false;
if (i<1 || i>L.length + 1) // 2.插入位置不合理,不能插入 if (i<1 || i>L.length + 1) // 2.插入位置不合理,不能插入
return false; return false;
for (int j = L.length; j >= i; j--) // 3. an~ai依次后移 for (int j = L.length; j >= i; j--) // 3. an~ai依次后移
L.elem[j] = L.elem[j - 1]; L.elem[j] = L.elem[j - 1];
L.elem[i - 1] = e; L.elem[i - 1] = e;
L.length++; L.length++;
return true; // 插入成功返回true return true; // 插入成功返回true
} }
//【算法2.8】 删除第i个元素 //【算法2.8】 删除第i个元素
template<class DT> template<class DT>
bool DeleElem_i(SqList<DT>& L, int i) bool DeleElem_i(SqList<DT>& L, int i)
{ {
if (L.length == 0) // 1.表空,不能删除 if (L.length == 0) // 1.表空,不能删除
return false; return false;
if (i<1 || i>L.length) // 2.删除位置不合理,不能插入 if (i<1 || i>L.length) // 2.删除位置不合理,不能插入
return false; return false;
for (int j = i; j < L.length; j++) // 3. ai+1~an依次前移 for (int j = i; j < L.length; j++) // 3. ai+1~an依次前移
L.elem[j - 1] = L.elem[j]; L.elem[j - 1] = L.elem[j];
L.length--; L.length--;
return true; // 删除成功返回true return true; // 删除成功返回true
} }
//【算法2.9】 //【算法2.9】
template<class DT> template<class DT>
bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值 bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值
{ {
if (i<1 || i>L.length) // 1.位序不合理,不能修改, if (i<1 || i>L.length) // 1.位序不合理,不能修改,
return false; // 返回false return false; // 返回false
L.elem[i - 1] = e; // 2.重置第i个元素值 L.elem[i - 1] = e; // 2.重置第i个元素值
return true; // 3.修改成功返回true return true; // 3.修改成功返回true
} }
// 清空顺序表 // 清空顺序表
template<class DT> template<class DT>
void ClearList(SqList<DT>& L) void ClearList(SqList<DT>& L)
{ {
L.length = 0; // 空表表长为0 L.length = 0; // 空表表长为0
} }
// 测表长 // 测表长
template<class DT> template<class DT>
int ListLength(SqList<DT> L) int ListLength(SqList<DT> L)
{ {
@@ -142,29 +142,29 @@ int ListLength(SqList<DT> L)
template<class DT> template<class DT>
bool ListEmpty(SqList<DT> L) // 测表空 bool ListEmpty(SqList<DT> L) // 测表空
{ {
if (L.length == 0) // 空表返回true if (L.length == 0) // 空表返回true
return true; return true;
else else
return false; // 非空表返回false return false; // 非空表返回false
} }
template<class DT> template<class DT>
bool ListFull(SqList<DT> L) bool ListFull(SqList<DT> L)
{ {
if (L.length == L.size) // 表满返回true if (L.length == L.size) // 表满返回true
return true; return true;
else else
return false; // 表不满返回false return false; // 表不满返回false
} }
//【算法2.10】 遍历输出 //【算法2.10】 遍历输出
template <class DT> template <class DT>
void DispList(SqList<DT> L) void DispList(SqList<DT> L)
{ {
int i; int i;
for (i = 1; i <= L.length; i++) // 依位序输出元素值 for (i = 1; i <= L.length; i++) // 依位序输出元素值
{ {
cout << L.elem[i - 1] << "\t"; cout << L.elem[i - 1] << "\t";

View File

@@ -1,186 +1,186 @@
// 2-2-LinkList-单链表 // 2-2-LinkList-单链表
// WARNING: /sdl is disabled to pass the compilation process. // WARNING: /sdl is disabled to pass the compilation process.
#include<iostream> //cout,cin #include<iostream> //cout,cin
using namespace std; using namespace std;
#include "LinkList.h" #include "LinkList.h"
//算法2.25 单链表逆置 //算法2.25 单链表逆置
void ReverseLinkList(LNode<int>*& L) void ReverseLinkList(LNode<int>*& L)
{ {
LNode<int>* p, * q; // 1.设置工作指针 LNode<int>* p, * q; // 1.设置工作指针
p = L->next; // 原链表头结点,作为逆置后表的头结点 p = L->next; // 原链表头结点,作为逆置后表的头结点
L->next = NULL; L->next = NULL;
while (p) // 2. 依次摘除原链表结点,以头插法插入到逆置链表中 while (p) // 2. 依次摘除原链表结点,以头插法插入到逆置链表中
{ {
q = p; // 2.1 q取当前结点位置 q = p; // 2.1 q取当前结点位置
p = p->next; // 2.2 p指向下一个待处理结点 p = p->next; // 2.2 p指向下一个待处理结点
q->next = L->next; // 2.3 将q 插入到头结点之后 q->next = L->next; // 2.3 将q 插入到头结点之后
L->next = q; L->next = q;
} }
} }
void dispmenu() void dispmenu()
{ // 显示主菜单 { // 显示主菜单
cout << endl; cout << endl;
cout << " *** 单 链 表 ***\n"; cout << " *** 单 链 表 ***\n";
cout << " 1-初始化单链表\n"; cout << " 1-初始化单链表\n";
cout << " 2-尾插法顺序表\n"; cout << " 2-尾插法顺序表\n";
cout << " 3-头插法建表\n"; cout << " 3-头插法建表\n";
cout << " 4-获取第i个元素\n"; cout << " 4-获取第i个元素\n";
cout << " 5-按值查找\n"; cout << " 5-按值查找\n";
cout << " 6-插入第i个元素\n"; cout << " 6-插入第i个元素\n";
cout << " 7-删除第i个元素\n"; cout << " 7-删除第i个元素\n";
cout << " 8-修改第i个元素\n"; cout << " 8-修改第i个元素\n";
cout << " 9-清空表\n"; cout << " 9-清空表\n";
cout << "10-测表长\n"; cout << "10-测表长\n";
cout << "11-测表空\n"; cout << "11-测表空\n";
cout << "12-遍历输出\n"; cout << "12-遍历输出\n";
cout << "13-按值查找前驱\n"; cout << "13-按值查找前驱\n";
cout << "14-单链表逆置\n"; cout << "14-单链表逆置\n";
cout << "0-退出\n"; cout << "0-退出\n";
} }
char pause; char pause;
// 主函数 // 主函数
int main() int main()
{ {
int i, n; int i, n;
int e, pre_e; int e, pre_e;
LNode<int>* L; LNode<int>* L;
system("cls"); // 清屏 system("cls"); // 清屏
int choice; int choice;
do do
{ {
dispmenu(); // 显示主菜单 dispmenu(); // 显示主菜单
cout << "Enter choice(1~120 退出):"; cout << "Enter choice(1~120 退出):";
cin >> choice; cin >> choice;
switch (choice) switch (choice)
{ {
case 1: // 初始化单链表 case 1: // 初始化单链表
InitList(L); InitList(L);
cout << endl << "创建成功!" << endl; cout << endl << "创建成功!" << endl;
break; break;
case 2: // 尾插法建单链表 case 2: // 尾插法建单链表
cout << "尾插法创建单链表" << endl; cout << "尾插法创建单链表" << endl;
cout << "输入要创建的顺序表中元素个数:"; cout << "输入要创建的顺序表中元素个数:";
cin >> n; cin >> n;
cout << endl; cout << endl;
CreateList_1(L, n); CreateList_1(L, n);
cout << "创建的单链表为:"; cout << "创建的单链表为:";
DispList(L); DispList(L);
cout << endl; cout << endl;
break; break;
case 3: // 头插法创建单链表 case 3: // 头插法创建单链表
cout << "头插法创建单链表" << endl; cout << "头插法创建单链表" << endl;
cout << "输入要创建的顺序表中元素个数:"; cout << "输入要创建的顺序表中元素个数:";
cin >> n; cin >> n;
cout << endl; cout << endl;
CreateList_2(L, n); CreateList_2(L, n);
cout << "创建的单链表为:"; cout << "创建的单链表为:";
DispList(L); DispList(L);
cout << endl; cout << endl;
break; break;
case 4: // 获取第i个元素 case 4: // 获取第i个元素
cout << "请输入元素序号:"; cout << "请输入元素序号:";
cin >> i; cin >> i;
cout << endl; cout << endl;
if (GetElem_i(L, i, e)) if (GetElem_i(L, i, e))
cout << endl << "" << i << "个元素为:" << e << endl; cout << endl << "" << i << "个元素为:" << e << endl;
else else
cout << endl << "元素不存在!" << endl; cout << endl << "元素不存在!" << endl;
break; break;
case 5: // 查询元素位序 case 5: // 查询元素位序
cout << "请输入要查询的元素值:"; cout << "请输入要查询的元素值:";
cin >> e; cin >> e;
i = LocateElem_e(L, e); i = LocateElem_e(L, e);
if (i) if (i)
cout << endl << e << "是第" << i << "个数据元素" << endl; cout << endl << e << "是第" << i << "个数据元素" << endl;
else else
cout << endl << "不存在此元素!" << endl; cout << endl << "不存在此元素!" << endl;
break; break;
case 6: // 插入第i个元素 case 6: // 插入第i个元素
cout << "输入插入位置:" << endl; cout << "输入插入位置:" << endl;
cin >> i; cin >> i;
cout << "输入插入元素值:" << endl; cout << "输入插入元素值:" << endl;
cin >> e; cin >> e;
if (InsertElem_i(L, i, e)) if (InsertElem_i(L, i, e))
{ {
cout << endl << "插入成功!" << endl; cout << endl << "插入成功!" << endl;
cout << "插入元素后的单链表为:" << endl; cout << "插入元素后的单链表为:" << endl;
DispList(L); DispList(L);
} }
else else
cout << endl << "插入不成功!" << endl; cout << endl << "插入不成功!" << endl;
break; break;
case 7: // 删除第i个元素 case 7: // 删除第i个元素
cout << "输入删除元素位置:" << endl; cout << "输入删除元素位置:" << endl;
cin >> i; cin >> i;
if (DeleElem_i(L, i)) if (DeleElem_i(L, i))
{ {
cout << endl << "删除成功!" << endl; cout << endl << "删除成功!" << endl;
cout << "删除元素后的单链表为:" << endl; cout << "删除元素后的单链表为:" << endl;
DispList(L); DispList(L);
} }
else else
cout << endl << "删除失败!" << endl; cout << endl << "删除失败!" << endl;
break; break;
case 8: // 修改第i个元素 case 8: // 修改第i个元素
cout << "输入修改元素位置:" << endl; cout << "输入修改元素位置:" << endl;
cin >> i; cin >> i;
cout << "输入新元素值:" << endl; cout << "输入新元素值:" << endl;
cin >> e; cin >> e;
if (PutElem_i(L, i, e)) if (PutElem_i(L, i, e))
{ {
cout << endl << "修改成功!" << endl; cout << endl << "修改成功!" << endl;
cout << "修改后的单链表为:" << endl; cout << "修改后的单链表为:" << endl;
DispList(L); DispList(L);
} }
else else
cout << endl << "修改失败!" << endl; cout << endl << "修改失败!" << endl;
break; break;
case 9: // 清空表 case 9: // 清空表
ClearList(L); ClearList(L);
break; break;
case 10: // 测表长 case 10: // 测表长
cout << "表长为:" << ListLength(L) << endl; cout << "表长为:" << ListLength(L) << endl;
break; break;
case 11: // 测表空 case 11: // 测表空
if (ListEmpty(L)) if (ListEmpty(L))
cout << endl << "空表!" << endl; cout << endl << "空表!" << endl;
else else
cout << endl << "不是空表!" << endl; cout << endl << "不是空表!" << endl;
break; break;
case 12: //遍历显示表 case 12: //遍历显示表
DispList(L); DispList(L);
cout << endl; cout << endl;
break; break;
case 13: case 13:
cout << "测试链表为\n"; cout << "测试链表为\n";
DispList(L); DispList(L);
cout << "输入查找前驱的元素值:\n"; cout << "输入查找前驱的元素值:\n";
cin >> e; cin >> e;
if (PriorElem_e(L, e, pre_e)) if (PriorElem_e(L, e, pre_e))
cout << e << "的前驱元素为:" << pre_e << endl; cout << e << "的前驱元素为:" << pre_e << endl;
else else
cout << e << "无前驱元素!" << endl; cout << e << "无前驱元素!" << endl;
break; break;
case 14: // 逆置单链表 case 14: // 逆置单链表
cout << "逆置前的单链表为:"; cout << "逆置前的单链表为:";
DispList(L); DispList(L);
ReverseLinkList(L); ReverseLinkList(L);
cout << "逆置后的单链表为:"; cout << "逆置后的单链表为:";
DispList(L); DispList(L);
cout << endl; cout << endl;
break; break;
case 0: //退出,销毁链表 case 0: //退出,销毁链表
DestroyList(L); DestroyList(L);
cout << "结束运行bye-bye!" << endl; cout << "结束运行bye-bye!" << endl;
break; break;
default: //无效选择 default: //无效选择
cout << "无效选择!\n"; cout << "无效选择!\n";
break; break;
} }
} while (choice != 0); } while (choice != 0);

View File

@@ -1,249 +1,249 @@
template <class DT> template <class DT>
struct LNode //链表结点 struct LNode //链表结点
{ {
DT data; //数据域,存储数据元素值 DT data; //数据域,存储数据元素值
LNode* next; //指针域,指向下一个结点 LNode* next; //指针域,指向下一个结点
}; };
//算法2.1 //算法2.1
template <class DT> template <class DT>
bool PriorElem_e(LNode<DT>* L, DT e, DT& pre_e) // 求值为e的元素前驱 bool PriorElem_e(LNode<DT>* L, DT e, DT& pre_e) // 求值为e的元素前驱
{ {
int k; int k;
k = LocateElem_e(L, e); // 1.获取e的位序k k = LocateElem_e(L, e); // 1.获取e的位序k
if (k > 1) // 2.位序k大于1 if (k > 1) // 2.位序k大于1
{ {
GetElem_i(L, k - 1, pre_e); // 第k-1个元素为e的前驱 GetElem_i(L, k - 1, pre_e); // 第k-1个元素为e的前驱
return true; return true;
} }
else // 3.元素e无前驱 else // 3.元素e无前驱
return false; // 返回false return false; // 返回false
} }
//【算法2.14】 创建空单链表 //【算法2.14】 创建空单链表
template <class DT> template <class DT>
bool InitList(LNode<DT>*& L) bool InitList(LNode<DT>*& L)
{ {
L = new LNode<DT>; // 1.创建头结点 L = new LNode<DT>; // 1.创建头结点
if (!L) exit(1); // 2.创建失败,退出 if (!L) exit(1); // 2.创建失败,退出
L->next = NULL; // 3.创建成功 L->next = NULL; // 3.创建成功
return true; // 返回true return true; // 返回true
} }
//【算法2.15】 尾插法创建n的元素 //【算法2.15】 尾插法创建n的元素
template <class DT> template <class DT>
bool CreateList_1(LNode<DT>*& L, int n) bool CreateList_1(LNode<DT>*& L, int n)
{ {
int i; int i;
LNode<DT>* p, * s; LNode<DT>* p, * s;
p = L; //1.工作指针初始化,指向尾结点 p = L; //1.工作指针初始化,指向尾结点
cout << "依次输入" << n << "个数据元素:" << endl; cout << "依次输入" << n << "个数据元素:" << endl;
for (i = 1; i <= n; i++) // 2.按元素位序正序创建各结点 for (i = 1; i <= n; i++) // 2.按元素位序正序创建各结点
{ {
s = new LNode<DT>; // 2.1 新建一个结点s s = new LNode<DT>; // 2.1 新建一个结点s
if (!s) // 2.2 创建失败返回false if (!s) // 2.2 创建失败返回false
return false; return false;
cin >> s->data; // 2.3 输入结点值 cin >> s->data; // 2.3 输入结点值
s->next = p->next; // 2.4 s 链在表尾 s->next = p->next; // 2.4 s 链在表尾
p->next = s; p->next = s;
p = s; // 2.5 工作指针指向 s p = s; // 2.5 工作指针指向 s
} }
return true; // 3.创建成功返回true return true; // 3.创建成功返回true
} }
//【算法2.16】 头插法创建n个元素 //【算法2.16】 头插法创建n个元素
template <class DT> template <class DT>
bool CreateList_2(LNode<DT>* (&L), int n) bool CreateList_2(LNode<DT>* (&L), int n)
{ {
int i; int i;
LNode<DT>* s; LNode<DT>* s;
cout << "逆序输入" << n << "个数据元素:" << endl; cout << "逆序输入" << n << "个数据元素:" << endl;
for (i = 1; i <= n; i++) // 1.按元素位序逆序创建各结点 for (i = 1; i <= n; i++) // 1.按元素位序逆序创建各结点
{ {
s = new LNode<DT>; // 1.1 新建一个结点 s s = new LNode<DT>; // 1.1 新建一个结点 s
if (!s) // 1.2 创建失败返回false if (!s) // 1.2 创建失败返回false
return false; return false;
cin >> s->data; // 1.3 输入结点值 cin >> s->data; // 1.3 输入结点值
s->next = L->next; // 1.4 s 在头结点后 s->next = L->next; // 1.4 s 在头结点后
L->next = s; L->next = s;
} }
return true; // 1.创建成功返回true return true; // 1.创建成功返回true
} }
//【算法2.17】 //【算法2.17】
template <class DT> template <class DT>
void DestroyList(LNode<DT>* (&L)) // 释放链表所占空间 void DestroyList(LNode<DT>* (&L)) // 释放链表所占空间
{ {
LNode<DT>* p; LNode<DT>* p;
while (L) // 1. 表非空,从头结点开始,依次释放结点 while (L) // 1. 表非空,从头结点开始,依次释放结点
{ {
p = L; // 1.1 处理表头结点 p = L; // 1.1 处理表头结点
L = L->next; // 1.2 头指针后移 L = L->next; // 1.2 头指针后移
delete p; // 1.3 释放表头结点所占内存 delete p; // 1.3 释放表头结点所占内存
} }
L = NULL; // 2.头指针指向空 L = NULL; // 2.头指针指向空
} }
//【算法2.18】 获取第i个元素 //【算法2.18】 获取第i个元素
template<class DT> template<class DT>
bool GetElem_i(LNode<DT>* L, int i, DT& e) bool GetElem_i(LNode<DT>* L, int i, DT& e)
{ {
LNode<DT>* p; // 1.初始化 LNode<DT>* p; // 1.初始化
p = L->next; // 1.1 设置工作指针,从首结点开始数结点 p = L->next; // 1.1 设置工作指针,从首结点开始数结点
int j = 1; // 1.2 计数器初始化 int j = 1; // 1.2 计数器初始化
while (p && j < i) // 2.定位到第i个元素结点 while (p && j < i) // 2.定位到第i个元素结点
{ {
p = p->next; j++; p = p->next; j++;
} }
if (!p || j > i) // 3 未找到返回false if (!p || j > i) // 3 未找到返回false
return false; return false;
else // 4. 找到 else // 4. 找到
{ {
e = p->data; // 获取第i个元素值 e = p->data; // 获取第i个元素值
return true; // 返回true return true; // 返回true
} }
} }
//【算法2.19】 查找值为e的元素位序 //【算法2.19】 查找值为e的元素位序
template<class DT> template<class DT>
int LocateElem_e(LNode<DT>* L, DT e) int LocateElem_e(LNode<DT>* L, DT e)
{ {
LNode<DT>* p; // 1.初始化从首元开始查找 LNode<DT>* p; // 1.初始化从首元开始查找
p = L->next; // 1.1从首元开始查找 p = L->next; // 1.1从首元开始查找
int j = 1; // 1.2 计数器初值 int j = 1; // 1.2 计数器初值
while (p && p->data != e) // 2.顺序查找 while (p && p->data != e) // 2.顺序查找
{ {
p = p->next; // 2.1未找到指针后移 p = p->next; // 2.1未找到指针后移
j++; // 2.2 计数器增1 j++; // 2.2 计数器增1
} }
if (p == NULL) // 3. 判断是否找到 if (p == NULL) // 3. 判断是否找到
return 0; // 3.1末找到返回0 return 0; // 3.1末找到返回0
else else
return j; // 3.2 找到,返回位序 return j; // 3.2 找到,返回位序
} }
//【算法2.20】 插入第i个元素 //【算法2.20】 插入第i个元素
template<class DT> template<class DT>
bool InsertElem_i(LNode<DT>*& L, int i, DT e) bool InsertElem_i(LNode<DT>*& L, int i, DT e)
{ {
int j = 0; int j = 0;
LNode<DT>* p; // 1.初始化 LNode<DT>* p; // 1.初始化
p = L; // 工作指针初始化 p = L; // 工作指针初始化
while (p && j < i - 1) // 2. 定位到插入点前驱 while (p && j < i - 1) // 2. 定位到插入点前驱
{ {
p = p->next; p = p->next;
j++; j++;
} }
if (!p || j > i - 1) // 3.判断定位是否成功: if (!p || j > i - 1) // 3.判断定位是否成功:
return false; // 3.1 定位失败,不能插入 return false; // 3.1 定位失败,不能插入
else // 3.2 定位成功 else // 3.2 定位成功
{ {
LNode<DT>* s; LNode<DT>* s;
s = new LNode<DT>; // 3.2.1建立新结点 s = new LNode<DT>; // 3.2.1建立新结点
s->data = e; // 3.2.2新结点赋值 s->data = e; // 3.2.2新结点赋值
s->next = p->next; // 3.2.3结点S链接到p结点之后 s->next = p->next; // 3.2.3结点S链接到p结点之后
p->next = s; p->next = s;
return true; // 3.2.4 插入成功返回true return true; // 3.2.4 插入成功返回true
} }
} }
//【算法2.21】 删除第i个元素 //【算法2.21】 删除第i个元素
template<class DT> template<class DT>
bool DeleElem_i(LNode<DT>* (&L), int i) bool DeleElem_i(LNode<DT>* (&L), int i)
{ {
LNode<DT>* p, * q; //1.初始化:设置工作指针 LNode<DT>* p, * q; //1.初始化:设置工作指针
p = L; //查找从头结点开始 p = L; //查找从头结点开始
int j = 0; //计数器初始化 int j = 0; //计数器初始化
while (p->next && j < i - 1) //2.p定位到删除点的前驱 while (p->next && j < i - 1) //2.p定位到删除点的前驱
{ {
p = p->next; p = p->next;
j++; j++;
} }
if (!p->next || j > i - 1) //3.删除位置不合理,不能删除 if (!p->next || j > i - 1) //3.删除位置不合理,不能删除
return false; //返回false return false; //返回false
else //4.删除操作 else //4.删除操作
{ {
q = p->next; //4.1暂存删除结点位置 q = p->next; //4.1暂存删除结点位置
p->next = q->next; //4.2从链表中摘除删除结点 p->next = q->next; //4.2从链表中摘除删除结点
delete q; delete q;
return true; //4.3删除成功返回true return true; //4.3删除成功返回true
} }
} }
//【算法2.22】 修改第i个元素值 //【算法2.22】 修改第i个元素值
template<class DT> template<class DT>
bool PutElem_i(LNode<DT>* (&L), int i, DT e) bool PutElem_i(LNode<DT>* (&L), int i, DT e)
{ {
LNode<DT>* p; // 1.初始化:设置工作指针 LNode<DT>* p; // 1.初始化:设置工作指针
p = L->next; // 从首结点开始,数结点 p = L->next; // 从首结点开始,数结点
int j = 1; // 计数器初始化 int j = 1; // 计数器初始化
while (p && j < i) // 2.查找第i个元素结点 while (p && j < i) // 2.查找第i个元素结点
{ {
p = p->next; j++; p = p->next; j++;
} }
if (!p || j > i) // 3.元素不存在,返回false if (!p || j > i) // 3.元素不存在,返回false
return false; return false;
else // 4.定位成功 else // 4.定位成功
{ {
p->data = e; // 修改元素值 p->data = e; // 修改元素值
return true; // 返回true return true; // 返回true
} }
} }
// 释放链表所占空间 // 释放链表所占空间
template<class DT> template<class DT>
void ClearList(LNode<DT>* (&L)) void ClearList(LNode<DT>* (&L))
{ {
LNode<DT>* p; LNode<DT>* p;
while (L->next) // 从首元结点开始,依次释放结点 while (L->next) // 从首元结点开始,依次释放结点
{ {
p = L->next; p = L->next;
L->next = p->next; L->next = p->next;
delete p; delete p;
} }
cout << endl << "表已清空!" << endl; cout << endl << "表已清空!" << endl;
} }
//【算法2.23】 测表长 //【算法2.23】 测表长
template<class DT> template<class DT>
int ListLength(LNode<DT>* L) int ListLength(LNode<DT>* L)
{ // 1.初始化 { // 1.初始化
int len = 0; // 1.1 结点计数器赋初值0 int len = 0; // 1.1 结点计数器赋初值0
LNode<DT>* p; // 1.2设置工作指针 LNode<DT>* p; // 1.2设置工作指针
p = L; // 指向头结点 p = L; // 指向头结点
while (p->next) // 2.数结点个数。有后继结点, while (p->next) // 2.数结点个数。有后继结点,
{ {
len++; p = p->next; // 结点数增1指针后移 len++; p = p->next; // 结点数增1指针后移
} }
return len; // 3.返回表长 return len; // 3.返回表长
} }
// //
template<class DT> template<class DT>
bool ListEmpty(LNode<DT>* L) //测表空 bool ListEmpty(LNode<DT>* L) //测表空
{ {
if (L->next == NULL) if (L->next == NULL)
return true; //空表返回1 return true; //空表返回1
else else
return false; //不空返回0 return false; //不空返回0
} }
//【算法2.24】 遍历表 //【算法2.24】 遍历表
template <class DT> template <class DT>
void DispList(LNode<DT>* L) // 显示表内容 void DispList(LNode<DT>* L) // 显示表内容
{ {
LNode<DT>* p; // 1. 设置工作指针 LNode<DT>* p; // 1. 设置工作指针
p = L; // 从首元结点开始遍历 p = L; // 从首元结点开始遍历
while (p->next) // 2.依次输出各结点值 while (p->next) // 2.依次输出各结点值
{ {
p = p->next; cout << p->data << "\t"; p = p->next; cout << p->data << "\t";

View File

@@ -1,55 +1,55 @@
template <class DT> template <class DT>
struct SqList //顺序表类 struct SqList //顺序表类
{ {
DT* elem; //表首址 DT* elem; //表首址
int length; //表长 int length; //表长
int size;//表容量 int size;//表容量
}; };
//【算法2.2】 //【算法2.2】
template <class DT> template <class DT>
bool InitList(SqList<DT>& L, int m) bool InitList(SqList<DT>& L, int m)
{//构建函数创建一表容量为m的空表 {//构建函数创建一表容量为m的空表
L.elem = new DT[m];// 申请表空间 L.elem = new DT[m];// 申请表空间
if (L.elem == NULL) if (L.elem == NULL)
{ {
cout << "未创建成功!"; cout << "未创建成功!";
exit(1); exit(1);
} }
L.length = 0; // 空表表长为0 L.length = 0; // 空表表长为0
L.size = m; //表容量为m L.size = m; //表容量为m
return true; return true;
} }
//【算法2.3】 //【算法2.3】
template <class DT> template <class DT>
bool CreateList(SqList<DT>& L, int n) //创建表长度为n的顺序表 bool CreateList(SqList<DT>& L, int n) //创建表长度为n的顺序表
{ {
int i; int i;
if (n > L.size) if (n > L.size)
{ {
cout << "元素个数大于表长,不能创建!" << endl; cout << "元素个数大于表长,不能创建!" << endl;
return false; return false;
} }
cout << "请依次输入" << n << "个元素值:" << endl; cout << "请依次输入" << n << "个元素值:" << endl;
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
cin >> L.elem[i - 1]; cin >> L.elem[i - 1];
L.length = n; L.length = n;
return true; return true;
} }
//【算法2.4】 //【算法2.4】
template <class DT> template <class DT>
void DestroyList(SqList<DT>& L) //销毁顺序表,释放表空间 void DestroyList(SqList<DT>& L) //销毁顺序表,释放表空间
{ {
delete[] L.elem; delete[] L.elem;
L.length = 0; L.length = 0;
L.size = 0; L.size = 0;
} }
//【算法2.5】 //【算法2.5】
template<class DT> template<class DT>
bool GetElem_i(SqList<DT> L, int i, DT& e)// 获取第i个元素值 bool GetElem_i(SqList<DT> L, int i, DT& e)// 获取第i个元素值
{ {
if (i<0 || i>L.length) if (i<0 || i>L.length)
return false; return false;
@@ -57,60 +57,60 @@ bool GetElem_i(SqList<DT> L, int i, DT& e)//
return true; return true;
} }
//【算法2.6】 //【算法2.6】
template<class DT> template<class DT>
int LocateElem_e(SqList<DT> L, DT e) // 按值查找 int LocateElem_e(SqList<DT> L, DT e) // 按值查找
{ {
int i; int i;
for (i = 0; i < L.length; i++) // 顺序查找 for (i = 0; i < L.length; i++) // 顺序查找
if (L.elem[i] == e) // 找到 if (L.elem[i] == e) // 找到
return i + 1; return i + 1;
return 0; // 未找到 return 0; // 未找到
} }
//【算法2.7】 //【算法2.7】
template<class DT> template<class DT>
bool InsertElem_i(SqList<DT>& L, int i, DT e) // 在第i个位置插入新元素 bool InsertElem_i(SqList<DT>& L, int i, DT e) // 在第i个位置插入新元素
{ {
int j; int j;
if (L.length >= L.size) //1.表满,不能插入 if (L.length >= L.size) //1.表满,不能插入
return false; return false;
if (i<1 || i>L.length + 1) //2.插入位置不合理,不能插入 if (i<1 || i>L.length + 1) //2.插入位置不合理,不能插入
return false; return false;
for (j = L.length; j >= i; j--) //3. an~ai依次后移 for (j = L.length; j >= i; j--) //3. an~ai依次后移
L.elem[j] = L.elem[j - 1]; L.elem[j] = L.elem[j - 1];
L.elem[i - 1] = e; L.elem[i - 1] = e;
L.length++; L.length++;
return true; // 插入成功返回true return true; // 插入成功返回true
} }
//【算法2.8】 //【算法2.8】
template<class DT> template<class DT>
bool DeleElem_i(SqList<DT>& L, int i) // 删除第i个元素 bool DeleElem_i(SqList<DT>& L, int i) // 删除第i个元素
{ {
int j; int j;
if (L.length == 0) //1.表空,不能删除 if (L.length == 0) //1.表空,不能删除
return false; return false;
if (i<1 || i>L.length) //2.删除位置不合理,不能插入 if (i<1 || i>L.length) //2.删除位置不合理,不能插入
return false; return false;
for (j = i; j < L.length; j++) //3. ai~an依次前移 for (j = i; j < L.length; j++) //3. ai~an依次前移
L.elem[j - 1] = L.elem[j]; L.elem[j - 1] = L.elem[j];
L.length--; L.length--;
return true; // 删除成功返回true return true; // 删除成功返回true
} }
//【算法2.9】 //【算法2.9】
template<class DT> template<class DT>
bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值 bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值
{ {
if (i<1 || i>L.length) //1.位置不合理,不能修改 if (i<1 || i>L.length) //1.位置不合理,不能修改
return false; return false;
L.elem[i - 1] = e; //2.重置第i个元素值 L.elem[i - 1] = e; //2.重置第i个元素值
return true; //3.修改成功返回true return true; //3.修改成功返回true
} }
//清空顺序表 //清空顺序表
template<class DT> template<class DT>
void ClearList(SqList<DT>& L) void ClearList(SqList<DT>& L)
{ {
@@ -118,14 +118,14 @@ void ClearList(SqList<DT>& L)
} }
//测表长 //测表长
template<class DT> template<class DT>
int ListLength(SqList<DT> L) int ListLength(SqList<DT> L)
{ {
return L.length; return L.length;
} }
//测表空 //测表空
template<class DT> template<class DT>
bool ListEmpty(SqList<DT> L) bool ListEmpty(SqList<DT> L)
{ {
@@ -135,7 +135,7 @@ bool ListEmpty(SqList<DT> L)
return false; return false;
} }
//测表满 //测表满
template<class DT> template<class DT>
bool ListFull(SqList<DT> L) bool ListFull(SqList<DT> L)
{ {
@@ -145,9 +145,9 @@ bool ListFull(SqList<DT> L)
return false; return false;
} }
//【算法2.10】 //【算法2.10】
template <class DT> template <class DT>
void DispList(SqList<DT> L) //遍历输出 void DispList(SqList<DT> L) //遍历输出
{ {
int i; int i;
for (i = 0; i < L.length; i++) for (i = 0; i < L.length; i++)

View File

@@ -1,4 +1,4 @@
// 2-3-SqListApp-顺序表应用 // 2-3-SqListApp-顺序表应用
#include<iostream>//cout,cin #include<iostream>//cout,cin
using namespace std; using namespace std;
@@ -7,35 +7,35 @@ using namespace std;
char pause; char pause;
void dispmenu() void dispmenu()
{ // 显示主菜单 { // 显示主菜单
cout << "\n*** 顺序表的应用 ***\n"; cout << "\n*** 顺序表的应用 ***\n";
cout << " 1-集合并A=AB\n"; cout << " 1-集合并A=AB\n";
cout << " 2-顺序表逆置\n"; cout << " 2-顺序表逆置\n";
cout << " 3-多项式求和\n"; cout << " 3-多项式求和\n";
cout << " 0-退出\n"; cout << " 0-退出\n";
} }
//算法2.11 //算法2.11
template <class DT> template <class DT>
void Union(SqList<DT>& La, SqList<DT> Lb) // 求La=LaLa void Union(SqList<DT>& La, SqList<DT> Lb) // 求La=LaLa
{ {
DT e; DT e;
int k, i; int k, i;
for (i = 1; i <= Lb.length; i++) // 扫描Lb for (i = 1; i <= Lb.length; i++) // 扫描Lb
{ {
GetElem_i(Lb, i, e); // 1. 获取Lb的第i个元素 GetElem_i(Lb, i, e); // 1. 获取Lb的第i个元素
if (!LocateElem_e(La, e)) // 2.如果La中无此元素 if (!LocateElem_e(La, e)) // 2.如果La中无此元素
{ {
k = La.length + 1; // 添加在La的表尾 k = La.length + 1; // 添加在La的表尾
InsertElem_i(La, k, e); InsertElem_i(La, k, e);
} }
} }
} }
//算法2.12 //算法2.12
template <class DT> template <class DT>
void ReverseSqList(SqList<DT>& L) // 顺序表元素逆置 void ReverseSqList(SqList<DT>& L) // 顺序表元素逆置
{ {
DT t; DT t;
int i; int i;
@@ -48,28 +48,28 @@ void ReverseSqList(SqList<DT>& L) // ˳
return; return;
} }
//算法2.13 多项式求和 lc=la+lb //算法2.13 多项式求和 lc=la+lb
void PolyAdd(SqList<float> la, SqList<float> lb, SqList<float>& lc) void PolyAdd(SqList<float> la, SqList<float> lb, SqList<float>& lc)
{ {
int i = 0; // 1.初始化,设置处理起始位置 int i = 0; // 1.初始化,设置处理起始位置
while (i < la.length && i < lb.length) // 2.两个多项式均未处理完 while (i < la.length && i < lb.length) // 2.两个多项式均未处理完
{ {
lc.elem[i] = la.elem[i] + lb.elem[i]; // 相同位序上的系数相加 lc.elem[i] = la.elem[i] + lb.elem[i]; // 相同位序上的系数相加
i++; i++;
} }
if (la.length > lb.length) // 3.la未处理完lb已处理完 if (la.length > lb.length) // 3.la未处理完lb已处理完
{ {
while (i < la.length) // lc取la中剩余项 while (i < la.length) // lc取la中剩余项
{ {
lc.elem[i] = la.elem[i]; lc.elem[i] = la.elem[i];
i++; i++;
} }
} }
else // 3.lb未处理完la已处理完 else // 3.lb未处理完la已处理完
{ {
while (i < lb.length) // lc取lb中剩余项 while (i < lb.length) // lc取lb中剩余项
{ {
lc.elem[i] = lb.elem[i]; lc.elem[i] = lb.elem[i];
i++; i++;
@@ -78,7 +78,7 @@ void PolyAdd(SqList<float> la, SqList<float> lb, SqList<float>& lc)
} }
} }
//显示多顶式 //显示多顶式
void DispPoly(float A[], int n) void DispPoly(float A[], int n)
{ {
int i; int i;
@@ -91,95 +91,95 @@ void DispPoly(float A[], int n)
//主函数 //主函数
int main() int main()
{ {
int e; int e;
int na, nb, nc; int na, nb, nc;
SqList<int> La, Lb; // 集合A、B SqList<int> La, Lb; // 集合A、B
SqList<int> Lc; // 顺序表 SqList<int> Lc; // 顺序表
SqList<float> fa, fb, fc; // 多项式A、B、C SqList<float> fa, fb, fc; // 多项式A、B、C
system("cls"); // 清屏 system("cls"); // 清屏
int choice; int choice;
do do
{ {
dispmenu(); // 显示主菜单 dispmenu(); // 显示主菜单
cout << "Enter choice(1~4,0退出):"; cout << "Enter choice(1~4,0退出):";
cin >> choice; cin >> choice;
switch (choice) switch (choice)
{ {
case 1: // 求集合并 case 1: // 求集合并
cout << "创建集合A、B\n"; cout << "创建集合A、B\n";
cout << "输入集合A元素个数"; cout << "输入集合A元素个数";
cin >> na; cin >> na;
cout << "输入集合B元素个数"; cout << "输入集合B元素个数";
cin >> nb; cin >> nb;
InitList(La, na + nb); // 创建集合A InitList(La, na + nb); // 创建集合A
cout << "创建集合A的元素\n"; cout << "创建集合A的元素\n";
CreateList(La, na); CreateList(La, na);
InitList(Lb, nb); // 创建集合B InitList(Lb, nb); // 创建集合B
cout << "创建集合B的元素\n"; cout << "创建集合B的元素\n";
CreateList(Lb, nb); CreateList(Lb, nb);
cout << "集合A为" << endl; // 显示集合A cout << "集合A为" << endl; // 显示集合A
DispList(La); DispList(La);
cout << "集合B为" << endl; // 显示集合B cout << "集合B为" << endl; // 显示集合B
DispList(Lb); DispList(Lb);
Union(La, Lb); // 求集合并 Union(La, Lb); // 求集合并
cout << "AB为:" << endl; // 显示结果 cout << "AB为:" << endl; // 显示结果
DispList(La); DispList(La);
cout << endl; cout << endl;
DestroyList(La); DestroyList(La);
DestroyList(Lb); DestroyList(Lb);
break; break;
case 2: // 顺序表逆置 case 2: // 顺序表逆置
cout << "请输入要创建的顺序表中元素个数:"; cout << "请输入要创建的顺序表中元素个数:";
cin >> nc; cin >> nc;
InitList(Lc, nc); InitList(Lc, nc);
cout << endl; cout << endl;
CreateList(Lc, nc); CreateList(Lc, nc);
cout << "创建的顺序表为:" << endl; // 显示集合A cout << "创建的顺序表为:" << endl; // 显示集合A
DispList(Lc); DispList(Lc);
ReverseSqList(Lc); ReverseSqList(Lc);
cout << "逆置后的顺序表为:" << endl; // 显示集合A cout << "逆置后的顺序表为:" << endl; // 显示集合A
DispList(Lc); DispList(Lc);
cout << endl; cout << endl;
DestroyList(Lc); DestroyList(Lc);
break; break;
case 3: //多项式求和 case 3: //多项式求和
cout << "\n创建多项式A\n"; // 创建多项式A cout << "\n创建多项式A\n"; // 创建多项式A
cout << "输入多项式A的项数"; cout << "输入多项式A的项数";
cin >> na; cin >> na;
InitList(fa, na); InitList(fa, na);
cout << "按幂升序输入多项式A各项系数\n"; cout << "按幂升序输入多项式A各项系数\n";
CreateList(fa, na); CreateList(fa, na);
cout << "\n创建多项式B\n"; // 创建多项式B cout << "\n创建多项式B\n"; // 创建多项式B
cout << "输入多项式B的项数"; cout << "输入多项式B的项数";
cin >> nb; cin >> nb;
InitList(fb, nb); InitList(fb, nb);
cout << "按幂升序输入多项式B各项系数\n"; cout << "按幂升序输入多项式B各项系数\n";
CreateList(fb, nb); CreateList(fb, nb);
cout << "\n多项式 A 为 " << endl; // 显示多项式A cout << "\n多项式 A 为 " << endl; // 显示多项式A
DispPoly(fa.elem, na); DispPoly(fa.elem, na);
cout << "\n多项式 B 为 " << endl; // 显示多项式B cout << "\n多项式 B 为 " << endl; // 显示多项式B
DispPoly(fb.elem, nb); DispPoly(fb.elem, nb);
nc = (na >= nb) ? na : nb; nc = (na >= nb) ? na : nb;
InitList(fc, nc); // 创建多项式C InitList(fc, nc); // 创建多项式C
PolyAdd(fa, fb, fc); // 求多项式 C=A+B PolyAdd(fa, fb, fc); // 求多项式 C=A+B
cout << "\n多项式A + 多项式B = " << endl; // 显示结果 cout << "\n多项式A + 多项式B = " << endl; // 显示结果
DispPoly(fc.elem, nc); DispPoly(fc.elem, nc);
cout << endl; cout << endl;
DestroyList(fa); DestroyList(fa);
DestroyList(fb); DestroyList(fb);
DestroyList(fc); DestroyList(fc);
break; break;
case 0: // 退出 case 0: // 退出
cout << " 结束运行bye-bye!" << endl; cout << " 结束运行bye-bye!" << endl;
break; break;
default: // 无效选择 default: // 无效选择
cout << "无效选择!\n"; cout << "无效选择!\n";
break; break;
} }
} while (choice != 0); } while (choice != 0);

View File

@@ -1,33 +1,33 @@
// 2-4-PolyAdd-稀疏多项式求和 // 2-4-PolyAdd-稀疏多项式求和
// WARNING: /sdl is disabled to pass the compilation process. // WARNING: /sdl is disabled to pass the compilation process.
#include<iostream>//cout,cin #include<iostream>//cout,cin
using namespace std; using namespace std;
struct PolyNode // 多项式结点 struct PolyNode // 多项式结点
{ {
float coef; // 系数 float coef; // 系数
int exp; // 指数 int exp; // 指数
PolyNode* next; // 指向下一项结点 PolyNode* next; // 指向下一项结点
}; };
void InitPoly(PolyNode*& L) void InitPoly(PolyNode*& L)
{ //创建一空多项式 { //创建一空多项式
L = new PolyNode; L = new PolyNode;
L->next = NULL; L->next = NULL;
} }
bool CreatePoly(PolyNode*& L, int n) // 尾插法创建n项多项式 bool CreatePoly(PolyNode*& L, int n) // 尾插法创建n项多项式
{ {
int i; int i;
PolyNode* p, * s; PolyNode* p, * s;
p = L; p = L;
for (i = 1; i <= n; i++) // 按幂升序依次输入多项式各项系数与幂指数 for (i = 1; i <= n; i++) // 按幂升序依次输入多项式各项系数与幂指数
{ {
s = new PolyNode; s = new PolyNode;
if (!s) if (!s)
return false; return false;
cout << "输入第" << i << "项系数和幂指数:"; cout << "输入第" << i << "项系数和幂指数:";
cin >> s->coef >> s->exp; cin >> s->coef >> s->exp;
s->next = p->next; s->next = p->next;
p->next = s; p->next = s;
@@ -37,13 +37,13 @@ bool CreatePoly(PolyNode*& L, int n) // β
} }
//显示多顶式 //显示多顶式
void DispPoly(PolyNode* L) // 通过遍历结点,输出多项式 void DispPoly(PolyNode* L) // 通过遍历结点,输出多项式
{ {
PolyNode* p; PolyNode* p;
if (!L) // 空表,返回 if (!L) // 空表,返回
{ {
cout << "空表!"; cout << "空表!";
return; return;
} }
p = L->next; p = L->next;
@@ -56,100 +56,100 @@ void DispPoly(PolyNode* L) // ͨ
cout << endl; cout << endl;
} }
//【算法2.26】 求多项式LA=LA+LB //【算法2.26】 求多项式LA=LA+LB
void PolyAdd(PolyNode*& LA, PolyNode*& LB) void PolyAdd(PolyNode*& LA, PolyNode*& LB)
{ {
float sum; float sum;
PolyNode* pa, * pb, * qa, * qb; // 1.工作指针初始化 PolyNode* pa, * pb, * qa, * qb; // 1.工作指针初始化
pa = LA; pa = LA;
qa = pa->next; qa = pa->next;
pb = LB; pb = LB;
qb = pb->next; qb = pb->next;
while (qa != NULL && qb != NULL) // 2. 两表均不空 while (qa != NULL && qb != NULL) // 2. 两表均不空
{ {
if (qa->exp < qb->exp) // 2.1 LA的幂小 if (qa->exp < qb->exp) // 2.1 LA的幂小
{ // pa、qa后移 { // pa、qa后移
pa = qa; qa = qa->next; pa = qa; qa = qa->next;
} }
else if (qa->exp > qb->exp) //2.2 LA 幂大 else if (qa->exp > qb->exp) //2.2 LA 幂大
{ {
pb->next = qb->next; // qb链接到pa之后 pb->next = qb->next; // qb链接到pa之后
qb->next = qa; qb->next = qa;
pa->next = qb; pa->next = qb;
pa = qb; pa = qb;
qb = pb->next; qb = pb->next;
} }
else // 2.3 LA与LB幂相同 else // 2.3 LA与LB幂相同
{ {
sum = qa->coef + qb->coef; // 计算系数和 sum = qa->coef + qb->coef; // 计算系数和
if (sum != 0) // 2.3.1 系数和不为0 if (sum != 0) // 2.3.1 系数和不为0
{ {
qa->coef = sum; // 2.3.1.1 qa->coef←sum qa->coef = sum; // 2.3.1.1 qa->coef←sum
pa = qa; qa = qa->next; // 2.3.1.2 paqa后移 pa = qa; qa = qa->next; // 2.3.1.2 paqa后移
pb->next = qb->next; pb->next = qb->next;
delete qb; // 2.3.1.3删除qb delete qb; // 2.3.1.3删除qb
qb = pb->next; qb = pb->next;
} }
else // 2.3.2 系数和为0 else // 2.3.2 系数和为0
{ {
pa->next = qa->next; pa->next = qa->next;
delete qa; // 2.3.2.1 删除qa delete qa; // 2.3.2.1 删除qa
qa = pa->next; // 2.3.2.2 qa为pa后继 qa = pa->next; // 2.3.2.2 qa为pa后继
pb->next = qb->next; pb->next = qb->next;
delete qb; // 2.3.2.3 删除qb delete qb; // 2.3.2.3 删除qb
qb = pb->next; // 2.3.2.4 qb为pb的后继 qb = pb->next; // 2.3.2.4 qb为pb的后继
} }
} }
}//while }//while
if (qb != NULL) // 3. LA处理结束LB未结束 if (qb != NULL) // 3. LA处理结束LB未结束
pa->next = qb; // 3.1 qb链到qa之后 pa->next = qb; // 3.1 qb链到qa之后
delete pb; // 3.2 删除lb头结点 delete pb; // 3.2 删除lb头结点
LB = NULL; LB = NULL;
}//Add }//Add
void DestroyPoly(PolyNode*& L) // 释放链表所占空间 void DestroyPoly(PolyNode*& L) // 释放链表所占空间
{ {
PolyNode* p; PolyNode* p;
while (L) // 从头结点开始,依次释放结点 while (L) // 从头结点开始,依次释放结点
{ {
p = L; p = L;
L = L->next; L = L->next;
delete p; delete p;
} }
L = NULL; // 头结点指向空 L = NULL; // 头结点指向空
} }
void SortPoly(PolyNode*& L) // 将多项式按幂升序排序 void SortPoly(PolyNode*& L) // 将多项式按幂升序排序
{ {
PolyNode* p1, * p2, * q, * r; // 采用插入排序算法 PolyNode* p1, * p2, * q, * r; // 采用插入排序算法
p1 = L; p2 = p1->next; // p1是p2的前驱 p1 = L; p2 = p1->next; // p1是p2的前驱
if (p2 == NULL || p2->next == NULL) // 空表或只有1项的多项式不需要排序 if (p2 == NULL || p2->next == NULL) // 空表或只有1项的多项式不需要排序
{ {
cout << "不需要排序!" << endl; cout << "不需要排序!" << endl;
return; return;
} }
r = L->next; // 有序表表尾 r = L->next; // 有序表表尾
q = r->next; // q为当前处理项有序表的后一项 q = r->next; // q为当前处理项有序表的后一项
while (q) // 未处理完 while (q) // 未处理完
{ // 从首元结点开始查找插入点 { // 从首元结点开始查找插入点
p1 = L; p2 = p1->next; p1 = L; p2 = p1->next;
while (q->exp > p2->exp && p2 != q) // 当前结点幂大,插入点后移 while (q->exp > p2->exp && p2 != q) // 当前结点幂大,插入点后移
{ {
p1 = p2; p2 = p2->next; p1 = p2; p2 = p2->next;
} }
if (p2 == q) // 当前项无需移动 if (p2 == q) // 当前项无需移动
{ {
r = q; // 有序表表尾顺移 r = q; // 有序表表尾顺移
} }
else // q插入到p2前面 else // q插入到p2前面
{ {
r->next = q->next; // 摘除q结点 r->next = q->next; // 摘除q结点
q->next = p1->next; // 在p1后插入结点q q->next = p1->next; // 在p1后插入结点q
p1->next = q; p1->next = q;
} }
q = r->next; // 下一个需处理的项 q = r->next; // 下一个需处理的项
} }
return; return;
@@ -158,81 +158,81 @@ void SortPoly(PolyNode*& L) //
void dispmenu() void dispmenu()
{//显示主菜单 {//显示主菜单
cout << endl; cout << endl;
cout << "*** 稀疏多项式求和 ***\n"; cout << "*** 稀疏多项式求和 ***\n";
cout << "1-创建多项式A\n"; cout << "1-创建多项式A\n";
cout << "2-创建多项式B\n"; cout << "2-创建多项式B\n";
cout << "3-多项式求和A=A+B\n"; cout << "3-多项式求和A=A+B\n";
cout << "4-显示多项式\n"; cout << "4-显示多项式\n";
cout << "0-退出\n"; cout << "0-退出\n";
} }
//主函数 //主函数
int main() int main()
{ {
int m, n; int m, n;
char c; char c;
PolyNode* LA, * LB; PolyNode* LA, * LB;
system("cls"); // 执行系统命令cls清屏 system("cls"); // 执行系统命令cls清屏
int choice; int choice;
do do
{ {
dispmenu(); // 显示主菜单 dispmenu(); // 显示主菜单
cout << "Enter choice(1~4,0 退出):"; cout << "Enter choice(1~4,0 退出):";
cin >> choice; cin >> choice;
switch (choice) switch (choice)
{ {
case 1: // 创建多项式A case 1: // 创建多项式A
InitPoly(LA); InitPoly(LA);
cout << "请输入多项式 A 的项数: "; cout << "请输入多项式 A 的项数: ";
cin >> m; cin >> m;
CreatePoly(LA, m); CreatePoly(LA, m);
cout << "创建的多项式 A 为:" << endl; cout << "创建的多项式 A 为:" << endl;
DispPoly(LA); DispPoly(LA);
SortPoly(LA); SortPoly(LA);
cout << "排序后多项式 A 为:" << endl; cout << "排序后多项式 A 为:" << endl;
DispPoly(LA); DispPoly(LA);
break; break;
case 2: // 创建多项式B case 2: // 创建多项式B
InitPoly(LB); InitPoly(LB);
cout << "请输入多项式B的项数 "; cout << "请输入多项式B的项数 ";
cin >> n; cin >> n;
CreatePoly(LB, n); CreatePoly(LB, n);
cout << "创建的多项式B为" << endl; cout << "创建的多项式B为" << endl;
DispPoly(LB); DispPoly(LB);
SortPoly(LB); SortPoly(LB);
cout << "排序后多项式 B 为:" << endl; cout << "排序后多项式 B 为:" << endl;
DispPoly(LB); DispPoly(LB);
break; break;
case 3: //多项式求和 case 3: //多项式求和
cout << "A = "; cout << "A = ";
DispPoly(LA); DispPoly(LA);
cout << "B = "; cout << "B = ";
DispPoly(LB); DispPoly(LB);
PolyAdd(LA, LB); // 求多项式 LA=LA+LB PolyAdd(LA, LB); // 求多项式 LA=LA+LB
cout << "A + B = "; // 显示结果 cout << "A + B = "; // 显示结果
DispPoly(LA); DispPoly(LA);
cout << endl; cout << endl;
break; break;
case 4: // 显示多项式 case 4: // 显示多项式
cout << "选择要显示的多项式 A 或 B" << endl; cout << "选择要显示的多项式 A 或 B" << endl;
cin >> c; cin >> c;
if (c == 'A' || c == 'a') if (c == 'A' || c == 'a')
DispPoly(LA); DispPoly(LA);
else if (c == 'B' || c == 'b') else if (c == 'B' || c == 'b')
DispPoly(LB); DispPoly(LB);
else else
cout << "选择错误!" << endl; cout << "选择错误!" << endl;
break; break;
case 5: //退出 case 5: //退出
DestroyPoly(LA); DestroyPoly(LA);
DestroyPoly(LB); DestroyPoly(LB);
cout << "结束运行bye-bye!" << endl; cout << "结束运行bye-bye!" << endl;
break; break;
default: //非法选择 default: //非法选择
cout << "非法选择!\n"; cout << "非法选择!\n";
break; break;
} }
} while (choice != 0); } while (choice != 0);

View File

@@ -1,14 +1,14 @@
template <class DT> template <class DT>
struct SqList // 顺序表 struct SqList // 顺序表
{ {
DT* elem; // 表首址 DT* elem; // 表首址
int length; // 表长 int length; // 表长
int size; // 表容量 int size; // 表容量
}; };
//算法2.1 //算法2.1
template <class DT> template <class DT>
bool PriorElem_e(SqList<DT> L, DT e, DT& pre_e) // 求值为e的元素前驱 bool PriorElem_e(SqList<DT> L, DT e, DT& pre_e) // 求值为e的元素前驱
{ {
int k; int k;
k = LocateElem_e(L, e); // k = LocateElem_e(L, e); //
@@ -21,119 +21,119 @@ bool PriorElem_e(SqList<DT> L, DT e, DT& pre_e) //
return true; return true;
} }
//【算法2.2】 初始化 //【算法2.2】 初始化
template <class DT> template <class DT>
bool InitList(SqList<DT>& L, int m) bool InitList(SqList<DT>& L, int m)
{ {
L.elem = new DT[m]; // 申请表空间 L.elem = new DT[m]; // 申请表空间
if (L.elem == NULL) if (L.elem == NULL)
{ {
cout << "未创建成功!"; // 申请不成功,退出 cout << "未创建成功!"; // 申请不成功,退出
exit(1); exit(1);
} }
L.length = 0; // 申请成功属性赋值。空表表长为0 L.length = 0; // 申请成功属性赋值。空表表长为0
L.size = m; // 表容量为m L.size = m; // 表容量为m
return true; return true;
} }
//【算法2.3】 创建表元素 //【算法2.3】 创建表元素
template <class DT> template <class DT>
bool CreateList(SqList<DT>& L, int n) bool CreateList(SqList<DT>& L, int n)
{ {
int i; int i;
if (n > L.size) // 1.元素个数大于表容量,不能创建。 if (n > L.size) // 1.元素个数大于表容量,不能创建。
{ {
cout << "元素个数大于表长,不能创建!" << endl; cout << "元素个数大于表长,不能创建!" << endl;
return false; return false;
} }
cout << "请依次输入" << n << "个元素值:" << endl; // 2.依位序输入各元素值 cout << "请依次输入" << n << "个元素值:" << endl; // 2.依位序输入各元素值
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
cin >> L.elem[i - 1]; cin >> L.elem[i - 1];
L.length = n; // 3.表长为创建的元素个数 L.length = n; // 3.表长为创建的元素个数
return true; return true;
} }
//【算法2.4】 销毁顺序表 //【算法2.4】 销毁顺序表
template <class DT> template <class DT>
void DestroyList(SqList<DT>& L) void DestroyList(SqList<DT>& L)
{ {
delete[] L.elem; // 1.释放表空间 delete[] L.elem; // 1.释放表空间
L.length = 0; // 2.属性赋值 L.length = 0; // 2.属性赋值
L.size = 0; L.size = 0;
} }
//【算法2.5】 获取第i个元素值 //【算法2.5】 获取第i个元素值
template<class DT> template<class DT>
bool GetElem_i(SqList<DT> L, int i, DT& e) bool GetElem_i(SqList<DT> L, int i, DT& e)
{ {
if (i<1 || i>L.length) // 1.位序不合理返回false if (i<1 || i>L.length) // 1.位序不合理返回false
{ {
cout << "该元素不存在!" << endl; cout << "该元素不存在!" << endl;
return false; return false;
} }
e = L.elem[i - 1]; // 2. 否则获取第i个元素值 e = L.elem[i - 1]; // 2. 否则获取第i个元素值
return true; // 返回true return true; // 返回true
} }
//【算法2.6】 按值查找 //【算法2.6】 按值查找
template<class DT> template<class DT>
int LocateElem_e(SqList<DT> L, DT e) int LocateElem_e(SqList<DT> L, DT e)
{ {
for (int i = 1; i <= L.length; i++) // 顺序查找 for (int i = 1; i <= L.length; i++) // 顺序查找
if (L.elem[i - 1] == e) // 1.找到 if (L.elem[i - 1] == e) // 1.找到
return i; // 返回元素位序 return i; // 返回元素位序
return 0; // 2.未找到返回0 return 0; // 2.未找到返回0
} }
//【算法2.7】 //【算法2.7】
template<class DT> template<class DT>
bool InsertElem_i(SqList<DT>& L, int i, DT e) bool InsertElem_i(SqList<DT>& L, int i, DT e)
{ {
if (L.length >= L.size) // 1.表满,不能插入 if (L.length >= L.size) // 1.表满,不能插入
return false; return false;
if (i<1 || i>L.length + 1) // 2.插入位置不合理,不能插入 if (i<1 || i>L.length + 1) // 2.插入位置不合理,不能插入
return false; return false;
for (int j = L.length; j >= i; j--) // 3. an~ai依次后移 for (int j = L.length; j >= i; j--) // 3. an~ai依次后移
L.elem[j] = L.elem[j - 1]; L.elem[j] = L.elem[j - 1];
L.elem[i - 1] = e; L.elem[i - 1] = e;
L.length++; L.length++;
return true; // 插入成功返回true return true; // 插入成功返回true
} }
//【算法2.8】 删除第i个元素 //【算法2.8】 删除第i个元素
template<class DT> template<class DT>
bool DeleElem_i(SqList<DT>& L, int i) bool DeleElem_i(SqList<DT>& L, int i)
{ {
if (L.length == 0) // 1.表空,不能删除 if (L.length == 0) // 1.表空,不能删除
return false; return false;
if (i<1 || i>L.length) // 2.删除位置不合理,不能插入 if (i<1 || i>L.length) // 2.删除位置不合理,不能插入
return false; return false;
for (int j = i; j < L.length; j++) // 3. ai+1~an依次前移 for (int j = i; j < L.length; j++) // 3. ai+1~an依次前移
L.elem[j - 1] = L.elem[j]; L.elem[j - 1] = L.elem[j];
L.length--; L.length--;
return true; // 删除成功返回true return true; // 删除成功返回true
} }
//【算法2.9】 //【算法2.9】
template<class DT> template<class DT>
bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值 bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值
{ {
if (i<1 || i>L.length) // 1.位序不合理,不能修改, if (i<1 || i>L.length) // 1.位序不合理,不能修改,
return false; // 返回false return false; // 返回false
L.elem[i - 1] = e; // 2.重置第i个元素值 L.elem[i - 1] = e; // 2.重置第i个元素值
return true; // 3.修改成功返回true return true; // 3.修改成功返回true
} }
// 清空顺序表 // 清空顺序表
template<class DT> template<class DT>
void ClearList(SqList<DT>& L) void ClearList(SqList<DT>& L)
{ {
L.length = 0; // 空表表长为0 L.length = 0; // 空表表长为0
} }
// 测表长 // 测表长
template<class DT> template<class DT>
int ListLength(SqList<DT> L) int ListLength(SqList<DT> L)
{ {
@@ -142,29 +142,29 @@ int ListLength(SqList<DT> L)
template<class DT> template<class DT>
bool ListEmpty(SqList<DT> L) // 测表空 bool ListEmpty(SqList<DT> L) // 测表空
{ {
if (L.length == 0) // 空表返回true if (L.length == 0) // 空表返回true
return true; return true;
else else
return false; // 非空表返回false return false; // 非空表返回false
} }
template<class DT> template<class DT>
bool ListFull(SqList<DT> L) bool ListFull(SqList<DT> L)
{ {
if (L.length == L.size) // 表满返回true if (L.length == L.size) // 表满返回true
return true; return true;
else else
return false; // 表不满返回false return false; // 表不满返回false
} }
//【算法2.10】 遍历输出 //【算法2.10】 遍历输出
template <class DT> template <class DT>
void DispList(SqList<DT> L) void DispList(SqList<DT> L)
{ {
int i; int i;
for (i = 1; i <= L.length; i++) // 依位序输出元素值 for (i = 1; i <= L.length; i++) // 依位序输出元素值
{ {
cout << L.elem[i - 1] << "\t"; cout << L.elem[i - 1] << "\t";

View File

@@ -7,10 +7,10 @@ bool CreateListNoRepeat(SqList<DT>& L, int n)
int i; int i;
if (n > L.size) if (n > L.size)
{ {
cout << "元素个数大于表长,不能创建!" << endl; cout << "元素个数大于表长,不能创建!" << endl;
return false; return false;
} }
cout << "请依次输入" << n << "个元素值:" << endl; cout << "请依次输入" << n << "个元素值:" << endl;
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
{ {
int temp; int temp;
@@ -53,22 +53,22 @@ int main()
InitList(A, 32); InitList(A, 32);
InitList(B, 32); InitList(B, 32);
int length_a, length_b; int length_a, length_b;
cout << "请输入集合A的元素个数: "; cout << "请输入集合A的元素个数: ";
cin >> length_a; cin >> length_a;
CreateListNoRepeat(A, length_a); CreateListNoRepeat(A, length_a);
cout << "请输入集合B的元素个数: "; cout << "请输入集合B的元素个数: ";
cin >> length_b; cin >> length_b;
CreateListNoRepeat(B, length_b); CreateListNoRepeat(B, length_b);
cout << "集合A: "; cout << "集合A: ";
DispList(A); DispList(A);
cout << "集合B: "; cout << "集合B: ";
DispList(B); DispList(B);
Union(A, B); Union(A, B);
cout << "AB: "; cout << "AB: ";
DispList(A); DispList(A);
A.length = length_a; // Reset A to its original length A.length = length_a; // Reset A to its original length
Intersection(A, B); Intersection(A, B);
cout << "A∩B: "; cout << "A∩B: ";
DispList(A); DispList(A);
DestroyList(A); DestroyList(A);
DestroyList(B); DestroyList(B);