#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
template<typename T>
struct Node
{
T data;
Node <T> *next;
};
template<typename T>
class LinkList
{
public:
LinkList();
~LinkList();
public:
bool ListInsert(int i, const T &e);
bool ListDelete(int i);
bool GetElem(int i, T& e);
int LocateElem(const T&e);
void DispList();
int ListLength();
bool Empty();
void ReverseList();
private:
Node<T> *m_head;
int m_length;
};
template <typename T>
LinkList<T>::LinkList()
{
m_head = nullptr;
m_length = 0;
}
template<typename T>
bool LinkList<T>::ListInsert(int i, const T &e)
{
if (i < 1 || i >(m_length + 1))
{
return false;
}
if (i == 1)
{
Node<T> *node = new Node<T>;
node->data = e;
node->next = m_head;
m_head = node;
cout << "e = " << e << endl;
m_length++;
return true;
}
Node<T> *p_curr = m_head;
for (int j = 1; j < (i - 1); ++j)
{
p_curr = p_curr->next;
}
Node<T>*node = new Node<T>;
node->data = e;
node->next = p_curr->next;
p_curr->next = node;
m_length++;
cout << "e = " << e << endl;
return true;
}
template<typename T>
bool LinkList<T>::ListDelete(int i)
{
if (i < 1 || i >(m_length + 1))
{
return false;
}
Node<T> *p_curr = m_head;
for (int j = 0; j < (i - 1); ++j)
{
p_curr = p_curr->next;
}
Node<T> * p_willdel = p_curr->next;
p_curr->next = p_willdel->next;
cout << "删除 e = " << p_willdel->data << endl;
m_length--;
}
//-------------------------
template<typename T>
bool LinkList<T>::GetElem(int i, T& e)
{
if (m_length < 1)
{
return false;
}
if (i < 1 || i > m_length)
{
return false;
}
Node<T> * p_curr = m_head;
for (int j = 0; j < (i - 1); ++j)
{
p_curr = p_curr->next;
}
e = p_curr->data;
cout << "e获取 = " << e << endl;
}
template<typename T>
int LinkList<T>::LocateElem(const T & e)
{
Node<T> *p_curr = m_head;
for (int i = 1; i <= m_length; ++i)
{
if (p_curr->next->data == e)
{
cout << "e = " << e << " i = " << i << endl;
return i;
}
p_curr = p_curr->next;
}
cout << "值为:" << e << endl;
return -1;
}
//输出单链表的所有元素:
template < typename T>
void LinkList<T>::DispList()
{
Node<T> *p = m_head->next;
while (p != nullptr)
{
cout << p->data << "";
p = p->next;
}
cout << endl;
}
template<typename T>
int LinkList<T>::ListLength()
{
return m_length;
}
//
template <typename T>
bool LinkList<T>::Empty()
{
if (m_head->next == nullptr)
{
return true;
}
return false;
}
template <typename T>
void LinkList<T>::ReverseList()
{
if (m_length <= 1)
{
return;
}
Node<T> * pothersjd = m_head->next->next;
m_head->next->next= nullptr;
Node<T> *ptmp;
while (pothersjd != nullptr)
{
ptmp = pothersjd;
pothersjd = pothersjd->next;
ptmp->next = m_head->next;
m_head->next = ptmp;
}
}
template <typename T>
LinkList<T>::~LinkList()
{
Node<T> *pnode = m_head->next;
Node<T>* ptmp;
while (pnode != nullptr)
{
ptmp = pnode;
pnode = pnode->next;
delete ptmp;
}
delete m_head;
m_head = nullptr;
m_length = 0;
}
int main()
{
cout << "test1111111111" << endl;
LinkList <int> sLinkJob;
sLinkJob.ListInsert(1,12);
sLinkJob.ListInsert(2, 24);
sLinkJob.ListInsert(3, 36);
sLinkJob.ListInsert(4, 36);
sLinkJob.ListDelete(2);
int eval = 0;
sLinkJob.GetElem(1,eval);
int findValve = 24;
sLinkJob.LocateElem(findValve);
sLinkJob.DispList();
getchar();
return 0;
}