ECJTU 单链表基础操作上机实验及代码实现

ECJTU 单链表上机实验

头文件区域


#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;
}

后期我会补充代码的详细书写过程和原理实现,可以私信我催更,最近在搞小程序比赛,没有很多时间…

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值