C++实现单链表的基本功能(包括头插法,尾插法,在任意位置插入元素等)

1.在任意位置删除元素没写,和插入差不多,在任意位置前面插入元素很经典巧妙
2.功能很多,包括头插法,尾插法,删除所有指定元素,在任意位置插入数据等

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

typedef int ElemType;

struct Lnode
{
	ElemType data;
	struct Lnode* next;
};

Lnode* initlist()// 初始化链表
{
	Lnode* head = new (std::nothrow)Lnode;
	if (head == nullptr) { return nullptr; }
	head->next = NULL;
	return head;
}

void destorylist(Lnode* head)//销毁链表
{
	Lnode* temp;
	while (head!= nullptr)
	{
		temp = head->next;
		delete head;
		head = temp;
	}
}

bool pushhead(Lnode* head, ElemType ee)//头插法插入元素
{
	if (head == nullptr) { cout << "链表不存在!" << endl; return false; }
	Lnode* temp = new (std::nothrow)Lnode;
	if (temp == nullptr) { cout << "分配失败" << endl; return false; }
	temp->data = ee;
	temp->next = head->next;
	head->next = temp;
	return true;
}

bool pushtail(Lnode* head, ElemType ee)//尾插法插入元素
{
	if (head == nullptr) { cout << "链表不存在!" << endl; return false; }
	Lnode* p = head;
	while (p->next != nullptr)
	{
		p = p->next;
	}
	Lnode* temp = new (std::nothrow)Lnode;
	if (temp == nullptr) { cout << "分配失败" << endl; return false; }
	temp->data = ee;
	 p->next=temp;
	 temp->next = nullptr;
	return true;
}

size_t getlength(Lnode* head)//得到链表的表长
{
	if (head == nullptr) { cout << "链表不存在!" << endl; return 0; }
	size_t length = 0;
	Lnode* p = head->next;
	while (p != nullptr)
	{
		p = p->next;
		length++;
	}
	return length;
}

bool deletepop(Lnode* head)//删除第一个节点的1元素
{
	if (head == nullptr) { cout << "链表不存在!" << endl; return false; }
	if (head->next == nullptr) { cout << "链表为空!" << endl; return false; }
	Lnode* temp = head->next;
	head->next = head->next->next;
	delete temp;
	return true;
}

bool deletetail(Lnode* head)//删除最后一个结点的元素1
{
	if (head == nullptr) { cout << "链表不存在!" << endl; return false; }
	if (head->next == nullptr) { cout << "链表为空!" << endl; return false; }
	Lnode* temp = head->next;
	while (temp->next->next != nullptr) { temp = temp->next; }
	delete temp->next;
	temp->next = nullptr;
	return true;
}

bool deleteallee(Lnode* head, const ElemType ee)
{
	if (head == nullptr) { cout << "链表不存在!" << endl; return false; }
	Lnode* temp = head;
	Lnode* temp2 = head;
	while (temp->next != nullptr)
	{
		while ((temp->next->data == ee)&&(temp->next=nullptr))
		{
			temp2 = temp->next;
			temp->next = temp->next->next;
			delete temp2;
		}
		if (temp->next != nullptr) { temp = temp->next; }
		else
			break;
	}
	return true;
}

ElemType locateNode(Lnode* head, const size_t n)//返回第n个节点的元素值
{
	if (head == nullptr) { cout << "链表不存在!" << endl; return 0; }
	Lnode* temp = head;
	int a = 0;
	while (a < n && temp != nullptr)
	{
		temp = temp->next;
		a++;
	}
	if (temp == nullptr)
	{
		cout << "位置非法" << endl;
		return 0;
	}
	return temp->data;
}

bool insertntail(Lnode* p, const ElemType ee)//在任意节点后面插入元素
{
	if (p == nullptr) { cout << "该结点不存在!!" << endl; return false; }
	Lnode* temp = new Lnode;
	temp->data = ee;
	temp->next = p->next;
	p->next = temp;
	return true;
}

bool insertnpop(Lnode* p, const ElemType ee)//在任意节点前面插入元素
{
	if (p == nullptr) { cout << "该结点不存在!!" << endl; return false; }
	insertntail(p, ee);
	ElemType aa;
	aa = p->next->data;
	p->next->data = p->data;
	p->data = aa;
	return true;
}

void printlist(Lnode* head)//打印链表
{
	if (head == nullptr) { cout << "链表不存在!" << endl; return; }
	Lnode* temp = head->next;
	while (temp != nullptr)
	{
		cout << temp->data << "\t";
		temp =temp->next;
	}
	cout << endl;
}

int main()
{
	Lnode* LL = initlist();
	ElemType ee;
	pushhead(LL, 8);
	pushhead(LL, 8);
	pushhead(LL, 8);//利用头插法插入三个元素

	pushtail(LL, 8);
	pushtail(LL, 8);
	pushtail(LL,8);//利用尾插法插入三个元素
	printlist(LL);
	cout <<"链表长度为:"<< getlength(LL) << endl;

	deletepop(LL);//删除第一个元素
	printlist(LL);
	cout << "链表长度为:" << getlength(LL) << endl;

	deletetail(LL);//删除最后一个元素
	printlist(LL);
	cout << "链表长度为:" << getlength(LL) << endl;

	//在指定位置后面或者前面插入元素
	int x,nn=0;
	Lnode* temp = LL;
	cout << "请输入你想在第几个位置后面插入元素:"; cin >> x;
	cout << "插入的数字元素是:"; cin >> ee;
	if (x<1 || x>getlength(LL)) 
	{ 
		cout << "插入位置非法!"<<endl;
	}
	else {
		while (nn < x && temp != nullptr)
		{
			temp= temp->next;
			nn++;
		}
		insertntail(temp, ee);
	}
	temp = LL;
	nn = 0;
	cout << "请输入你想在第几个位置前面插入元素:"; cin >> x;
	cout << "插入的数字元素是:"; cin >> ee;
	if (x<1 || x>getlength(LL))
	{
		cout << "插入位置非法!" << endl;
	}
	else {
		while (nn < x && temp != nullptr)
		{
			temp = temp->next;
			nn++;
		}
		insertnpop(temp, ee);
	}
	
	printlist(LL);
	size_t n = 3;
	cout << "第" << n << "个位置的数据是:" << locateNode(LL, n) << endl;

	deleteallee(LL, 8);//删除链表中的所有8
	printlist(LL);

	destorylist(LL);//销毁链表
}

  • 17
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值