c++实现链表
创始人
2024-01-31 22:26:32
0
#pragma once
#include
using namespace std;template
struct Node
{T data;//数据Node* next;//存放下一个同类型的节点
};template
class LinkList
{
public:LinkList();~LinkList();public:bool ListInsert_head(int i,const T& e);//带头节点//bool ListInsert(int i, const T& e);//不带头节点bool ListDelete(int i);bool GetElem(int i,T&e);//获取第i个元素的位置int LocateElem(const T&e);//获取第一个元素出现的位置void DispList();int ListLength();bool Empty();void ReverseList();private:Node* m_head;//头节点int m_length;
};template
LinkList::LinkList()
{//带头的初始化m_head = new Node;m_head->next = nullptr;m_length = 0;不带头的//m_head = nullptr;//m_length = 0;
}template
bool LinkList::ListInsert_head(int i, const T& e)//带头节点---插入
{if (i<1 || i>(m_length + 1)){cout << "插入位置不合法" << endl;return false;}Node* p_curr=m_head;for (int j = 0; j < (i - 1); j++)//找到第i个节点{p_curr = p_curr->next;}Node* node = new Node;//开辟新节点空间node->data = e;node->next = p_curr->next;p_curr->next = node;m_length++;cout << "插入成功" << endl;return true;
}//template
//bool LinkList::ListInsert(int i, const T& e)//不带头节点---插入
//{
//	if (i<1 || i>(m_length + 1))
//	{
//		cout << "插入位置不合法" << endl;
//		return false;
//	}
//
//	if (i == 1)//插入第一个位置
//	{
//		Node* node = new Node;
//		node->data = e;
//		node->next = m_head;
//		m_head = node;
//		m_length++;
//		return true;
//	}
//
//	Node* p_curr = m_head;//插入不是第一个位置
//	for (int j = 1; j < (i - 1); j++)
//	{
//		p_curr = p_curr->next;
//	}
//	Node* node = new Node;
//	node->data = e;
//	node->next = p_curr->next;
//	p_curr->next = node;
//	m_length++;
//	return true;
//}template
bool LinkList::ListDelete(int i)
{if (i<1 || i>(m_length + 1) || i < m_length){cout << "删除位置不合法,或者链表为空" << endl;return false;}Node* p_curr = m_head;for (int j = 0; j < (i - 1); j++){p_curr = p_curr->next;//找到删除位置的前一个节点}Node* p_willdel = p_curr->next;//要删除的位置p_curr->next = p_willdel->next;delete p_willdel;m_length--;cout << "删除成功" << endl;return true;
}template
bool LinkList::GetElem(int i, T& e)//获取第i个元素的位置
{if (i<1 || i>(m_length + 1) || i < m_length){cout << "查找位置不合法,或者链表为空" << endl;return false;}Node* p_curr = m_head;for (int j = 0; j < (i - 1); j++){p_curr = p_curr->next;}e = p_curr->data;//找到第i个位置cout << "找到值:" << e << endl;return true;
}template
int LinkList::LocateElem(const T& e)//获取第一个元素出现的位置
{Node* p_curr = m_head;for(int i=1; i<=m_length; i++){if (p_curr->next->data == e){cout << "找到了:" << p_curr->next->data << " ";return i;}p_curr = p_curr->next;//不相等就往下走}cout << "找不到该值" << endl;return -1;
}template
void LinkList::DispList()
{//for (Node* p = m_head->next; p != nullptr; p=p->next)//{//	cout <data << " ";//}//cout << endl;Node* p = m_head->next;while (p != nullptr){cout << p->data << " ";p = p->next;}cout << endl;
}template
int LinkList::ListLength()
{return m_length;
}template
bool LinkList::Empty()
{if (m_head->next ==nullptr){return true;}return false;
}template
void LinkList::ReverseList()
{if ( 1>= m_length){return ;}//a1,a2,a3,a4---将其分为两组a1和头节点,第二组是a2,a3,a4,让第二组的头节点插入a1前面,依次重复前面操作Node* pptmp = m_head->next->next;//pptmp---a2m_head->next->next = nullptr;Node* ptmp;while (pptmp != nullptr){ptmp = pptmp;//pptmp  为a2pptmp = pptmp->next;//pptmp---a3ptmp->next = m_head->next;//把a2插入a1头m_head->next = ptmp;//头节点后为a2}
}template
LinkList::~LinkList()
{Node* pnode = m_head->next;Node* ptmp;while (ptmp != nullptr)//释放数据链表节点{ptmp = pnode;pnode = pnode->next;delete ptmp;}delete m_head;m_head = nullptr;m_length = 0;
}
#include"List.hpp"void test()
{LinkListlk;//插入lk.ListInsert_head(1, 12);lk.ListInsert_head(2, 22);//删除//lk.ListDelete(1);//查找//int val = 0;//lk.GetElem(2, val);//第二个位置为12,因为有头节点//查找//int pos = lk.LocateElem(22);//if(pos!=-1)//{//	cout << "下标为:" << pos << endl;//}//反转lk.ReverseList();//打印lk.DispList();
}int main()
{test();system("pause");return 0;
}

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...