C++单链表最简单的创建,插入,删除功能实现

 C++单链表最简单的创建,插入,删除功能实现

   这里是对数据结构的复习,好久没有动C++了,现在再来写写链表, 怕真的以后看都看不懂了就尴尬了。逻辑语句不难,应该能看懂。大神别骂我为什么不用C++模板,因为我只想单纯写写链表看看自己还会不会。另外main函数可以自己写写清屏语句,我就不写了。

 

#include <iostream>
using namespace std;
/* 创建一个单链表 */
struct Node {
	int data;
	Node* next;
};
class Linklist
{
public:
	Linklist();
	~Linklist();
	void Creatlist(int num);
	void Insertlist(int position, int num);
	void Showlist();
	void Delete(int position);

private:
	Node *Head;
};

Linklist::Linklist()
{
	Head = new Node;
	Head->next = NULL;
}

Linklist::~Linklist()
{
	Node *p;
	while (Head) {
		p = Head;
		Head = Head->next;
		delete p;
	}
	Head = NULL;
}

void Linklist::Creatlist(int num) {
	Node *L, *Y;
	Y = Head;
	cout << "Please enter your " << num << "number:" << endl;
	for (int i = 0; i < num; i++) {
		L = new Node;
		cin >> L->data;
		L->next = Y->next;
		Y->next = L;
		Y = L;
	};
}

void Linklist::Insertlist(int position, int num) {
	int i = 0;
	Node *L;
	L = Head;
	while (L&&i < position - 1) {
		L = L->next;
		i++;
	};
	if (!L||i>position-1) {
		cout << "插入位置不对!" << endl;
	}
	else {
		Node *Y;
		Y = new Node;
		Y->data = num;
		Y->next = L->next;
		L->next = Y;
	}
}

void Linklist::Showlist() {
	Node *L;
	L = Head->next;
	while (L) {
		cout << "--->"<<L->data ;//主义这一句和下一句的顺序,颠倒了就会出现很难发现的问题。
		L = L->next;
	}
	cout << endl;
}

void Linklist::Delete(int position) {
	int i = 0;
	Node *L;
	Node *Y = Head;
	L = Head;
	while (L->next&&i < position-1) {
		L = L->next;
		i++;
	};
	if (!L->next||i>position-1) {
		cout << "删除位置不对!!" << endl;
	}
	else {
		Y = L->next;
		L->next = Y->next;
		delete Y;
	}
}

int main()
{
	//这是我最开始检测链表功能的语句
	//Linklist P;
	//P.Creatlist(4);
	//P.Showlist();
	//P.Insertlist(1, 2);
	//P.Showlist();
	//P.Delete(5);
	//P.Showlist();
	//return 0;
	int num, position;
	Linklist P;
	system("cls");
	int chioce;
	do {
		cout << endl;
		cout << "\t\t\t  链表最基本操作\n" << endl;
		cout << "\t\t---------------------------------\n";
		cout << "\t\t---------------------------------\n";
		cout << "                      ***1.创建链表***\n";
		cout << "                      ***2.插入节点(已预定、未预定、)***\n";
		cout << "                      ***3.删除节点***\n";
		cout << "                      ***4.打印链表***\n";
		cout << "                      ***5.退出系统***\n";
		cout << "\t\t----------------------------------\n";
		cout << "\t\t----------------------------------\n";
		cout << "\t\t----*****输入chioce:" << "  " << endl;
		cin >> chioce;
		switch (chioce)
		{
		case 1:
			int i;
			cout << "请输入创建的链表的长度:" << endl;
			cin >> i;
			P.Creatlist(i);
			cout << "创建完成!" << endl;
			break;
		case 2:
		{
			cout << "请输入你需要插入的位置" << endl;
			cin >> position;
			cout << endl;
			cout << "请输入你需要插入的数:" << endl;
			cin >> num;
			P.Insertlist(position, num);
			cout << "插入完成!" << endl;
		}break;
		case 3: {
			int position;
			cout << "请输入你需要删除的位置" << endl;
			cin >> position;
			cout << endl;
			P.Delete(position);
			cout << "删除完成!" << endl;
		} break;
		case 4: {
			cout << "链表目前如下:" << endl;
			P.Showlist();
		} break;
		case 5:cout << "\n感谢使用本系统欢迎您下次使用!\n";
			exit(0);
		default:
			cout << "Invalid chioce!\n";
		}
	} while (chioce != 5);
	return 0;
}

 

  • 13
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值