#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 = new Node<T>;
m_head->next = nullptr;
m_length = 0;
}
template <typename T>
bool LinkList<T>::ListDelete(int i)
{
cout << "sssssssssss11111111111" << endl;
if (m_length < 1)
{
cout << "test" << endl;
return false;
}
/*
if (i < 1 || m_length)
{
return false;
}
*/
cout << "sssssssssss" << endl;
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;
m_length--;
cout << "成功删除位置为:" << i << "元素为:" << p_willdel->data << endl;
delete p_willdel;
return true;
}
//在单链表插入元素
template<typename T>
bool LinkList<T>::ListInsert(int i, const T & e)
{
if (i < 1 || i >(m_length + 1))
{
cout << "222" << endl;
return false;
}
Node<T> *p_curr = m_head;
for (int j = 0; 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;
cout << "e = " << e << endl;
m_length++;
return true;
}
//不带头节点
/*
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;
}
*/
int main()
{
cout << "test1111111111" << endl;
LinkList <int> sLinkJob;
sLinkJob.ListInsert(1, 12);
sLinkJob.ListInsert(2, 24);
sLinkJob.ListInsert(3, 98);
sLinkJob.ListDelete(2);
getchar();
return 0;
}