实验内容:
编程实现链表下教材第二章定义的线性表的基本操作,最好用菜单形式对应各个操作,使其编程一个完整的小软件。
实验步骤:
1.按照实验要求编写代码,构造链表,初始化链表
2.再插入元素1 2 3
3.分别求链表中元素的个数、求直接前驱、求直接后继
4.删除一个元素
5.查看链表中元素的个数并输出所有链表元素
6.初始化链表并重新输入链表元素
7.输出所输入的链表元素
8.最后销毁链表,退出。
9.输入验收用例,验证其输出结果。
#include <iostream>
#include <cstdlib>
#define False -1
using namespace std;
struct LNode {
int data;
LNode* next;
};
void InitList(LNode*& L) {
L = NULL;
}
void DestroyList(LNode*& L) {
LNode* p;
while (L) {
p = L;
L = L->next;
delete p;
}
}
int ListLength(LNode* L) {
int len = 0;
LNode* p = L;
while (p) {
len++;
p = p->next;
}
return len;
}
int GetElemValue(LNode* L, int i) {
LNode* p = L;
int j = 1;
while (p && j < i) {
p = p->next;
j++;
}
if (!p || j > i) {
cout << "错误: 非法位置!" << endl;
return False;
}
return p->data;
}
int GetPosition(LNode* L, int e) {
LNode* p = L;
int pos = 1;
while (p && p->data != e) {
p = p->next;
pos++;
}
if (!p) {
cout << "错误: 非法位置!" << endl;
return False;
}
return pos;
}
LNode* GetPredecessor(LNode* L, int e) {
LNode* p = L;
LNode* pred = NULL;
while (p->next && p->next->data != e) {
p = p->next;
}
if(p->next->data == e){
pred = p;
}
return pred;
}
LNode* GetSuccessor(LNode* L, int e) {
LNode* p = L;
while (p && p->data != e) {
p = p->next;
}
if (p && p->next) {
return p->next;
}
return NULL;
}
bool ListInsert(LNode*& L, int i, int e) {
if (i < 1) {
cout << "错误: 非法位置!" << endl;
return false;
}
LNode* p = new LNode;
p->data = e;
if (i == 1) {
p->next = L;
L = p;
} else {
LNode* q = L;
int j = 1;
while (q && j < i - 1) {
q = q->next;
j++;
}
if (!q || j > i - 1) {
cout << "错误: 非法位置!" << endl;
return false;
}
p->next = q->next;
q->next = p;
}
return 0;
}
bool ListDelete(LNode*& L, int i) {
if (i < 1) {
cout << "错误: 非法位置!" << endl;
return false;
}
if (i == 1) {
LNode* p = L;
L = L->next;
delete p;
} else {
LNode* p = L;
int j = 1;
while (p && j < i - 1) {
p = p->next;
j++;
}
if (!p || !p->next || j > i - 1) {
cout << "错误: 非法位置!" << endl;
return false;
}
LNode* q = p->next;
p->next = q->next;
delete q;
}
return 0;
}
void showList(LNode* L) {
LNode* p = L;
while (p) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main() {
LNode* L1;
InitList(L1);
int a=0;
int commander;
while (true) {
cout << "1. 初始化链表" << endl;
cout << "2. 销毁链表" << endl;
cout << "3. 求链表长度" << endl;
cout << "4. 求指定位序的元素值" << endl;
cout << "5. 求链表输入元素的位序" << endl;
cout << "6. 求输入元素直接前驱" << endl;
cout << "7. 求输入元素直接后继" << endl;
cout << "8. 在第i个位置插入元素" << endl;
cout << "9. 删除第i个元素" << endl;
cout << "10. 输出所输入的链表元素" << endl;
cout << "11. 初始化并输入链表元素" << endl;
cout << "负数退出" << endl;
cout << "请输入你的选择: ";
cin >> commander;
if(commander==1||commander==11){
a++;
}
if(commander<1){
break;
}
else{
if(a==0){
cout << "请先进行初始化操作!" << endl ;
continue;
}
}
switch (commander) {
case 1:
InitList(L1);
break;
case 2:
DestroyList(L1);
break;
case 3:
cout << "链表中数据元素个数为: " << ListLength(L1) << endl;
break;
case 4: {
int i;
cout << "请输入指定位序i: ";
cin >> i;
if(GetElemValue(L1, i)){
cout << "指定位序的元素值为: " << GetElemValue(L1, i) << endl;
}
break;
}
case 5: {
int e;
cout << "请输入元素e: ";
cin >> e;
if(GetPosition(L1, e)){
cout << "元素e的位序为: " << GetPosition(L1, e) << endl;
}
break;
}
case 6: {
int e_pred;
cout << "请输入元素e: ";
cin >> e_pred;
LNode* pred = GetPredecessor(L1, e_pred);
if (pred) {
cout << "元素e的直接前驱为: " << pred->data << endl;
} else {
cout << "未找到元素e的直接前驱!" << endl;
}
break;
}
case 7: {
int e_succ;
cout << "请输入元素e: ";
cin >> e_succ;
LNode* succ = GetSuccessor(L1, e_succ);
if (succ) {
cout << "元素e的直接后继为: " << succ->data << endl;
} else {
cout << "未找到元素e的直接后继!" << endl;
}
break;
}
case 8: {
int i_insert, e_insert;
cout << "请输入插入位置i: ";
cin >> i_insert;
cout << "请输入插入元素e: ";
cin >> e_insert;
ListInsert(L1, i_insert, e_insert);
break;
}
case 9: {
int i_delete;
cout << "请输入删除位置i: ";
cin >> i_delete;
ListDelete(L1, i_delete);
break;
}
case 10:
cout << "链表元素为: ";
showList(L1);
break;
case 11: {
int n;
cout << "请输入链表元素个数n: ";
cin >> n;
cout << "请输入链表元素: ";
for (int j = 0; j < n; j++) {
int element;
cin >> element;
ListInsert(L1, j + 1, element);
}
break;
}
case 12:
DestroyList(L1);
exit(0);
default:
cout << "无效的输入,请重新输入!" << endl;
break;
}
system("pause");
system("cls");
}
return 0;
}