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