头文件区域
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<array>
#include<cstdio>
#include<cctype>
#include<fstream>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<map>
主体代码实现
using namespace std;
typedef struct List_node\\结构体定义
{
int data;
struct List_node* next;
}list_node,*list_single;
void create_list_R(list_single &L,int n)\\后插法创建单链表
{
L = new list_node;
L -> next = NULL;
list_single R = L;
for (int i = 0; i < n; i++)
{
list_single P = new list_node;
cin >> P->data;
P->next = NULL;
R->next = P;
R = P;
}
}
list_node* search_node(const list_single L,int e)
\\查找函数
{
list_single P = L->next;
while (P != NULL && P->data != e)
{
P = P->next;
}
return P;
}
bool insert_list_node(list_single& L, int i, int e)
\\插入函数
{
list_single P = L;
int j = 0;
while (P != NULL && (j < i - 1))
{
P = P->next;
j++;
}
if (P == NULL || (j > i - 1))
{
return false;
}
list_single S = new list_node;
S->data = e;
S->next = P -> next;
P->next = S;
return true;
}
void show_list(const list_single L)
\\显示函数
{
list_single P = L->next;
while (P != NULL)
{
cout << P->data;
cout << " ";
P = P->next;
}
cout << endl;
}
bool delete_list_node(list_single L, int i)
\\ 删除函数
{
list_single P = L;
int j = 0;
while (P->next != NULL && (j < i - 1))
{
P = P->next;
j++;
}
if ((P->next == NULL) || (j > i - 1))
{
return false;
}
list_single D=P->next;
P->next = D->next;
delete(D);
return true;
}
void destory_list(list_single& L)
\\销毁函数
{
list_single P;
while (L!=NULL)
{
P = L;
L = L->next;
delete(P);
}
P = NULL;
}
int main(int argc, char const* argv[])
\\主函数
{
cout << endl;
list_single A;
cout << "请输入创建列表节点数 :";
int n;
cin >> n;
create_list_R(A, n);
show_list(A);
cout << "请输入你的选择A[寻找] B[插入] C[显示] D[删除 ] E[销毁] Z[退出]" << endl;
char chioce;
cin >> chioce;
while (chioce != 'Z')
{
switch (chioce)
{
case 'A':
{
cout << "请输入你想要查找的位序 :";
int i;
cin >> i;
cout << endl;
cout << "查找的位序为 :" << search_node(A, i)->data << endl;
}break;
case 'B':
{
cout << "请输入你要插入的位序和数字 :";
int i, n;
cin >> i >> n;
insert_list_node(A, i, n);
cout << endl;
cout << "此次操作后链表的输出为 :";
show_list(A);
}break;
case 'C':
{
cout << "此次操作后链表的输出为 :";
show_list(A);
}break;
case 'D':
{
cout << "请输入你要删除的位序 :";
int i;
cin >> i;
delete_list_node(A, i);
cout << endl;
cout << "此次操作后链表的输出为 :";
show_list(A);
}break;
case 'E':destory_list(A); break;
default:cout << "enter error!!!"; break;
}
if (chioce == 'E')
{
break;
}
cout << "请输入选择 :";
cin >> chioce;
cout << endl;
}
cout << endl;
cout << "END";
return 0;
}
后期我会补充代码的详细书写过程和原理实现,可以私信我催更,最近在搞小程序比赛,没有很多时间…