C++作业,用模板类写链表,完成了对基本的单链表的插入,删除等。
#include <iostream>
using namespace std;
template<typename T>
struct Node
{
T data;
Node<T> *next;
};
template<typename T>
class LinkedList
{
public:
LinkedList() { size = 0; first = NULL; }
~LinkedList();
T find(int index); //返回索引对应的值
void remove(int index); //删除
void insert(T value); //插入在链表最后一位
void insert(int index, T value); //指定位置插入
void print(); //打印链表
int length() const; //获取链表长度
private:
Node<T> *first; //头结点
int size; //链表长度
bool checkRange(int index); //检测索引合法性
Node<T>* getNode(int index); //返回索引位置节点
};
template<typename T>
bool LinkedList<T>::checkRange(int index)
{
if (index <0 || index > size - 1)
return false;
return true;
}
template<typename T>
Node<T>* LinkedList<T>::getNode(int index)
{
if(!checkRange(index))
{
return NULL;
}
Node<T>* head = first;
for (int i = 0; i < index; i++)
{
head = head->next;
}
return head;
}
template<typename T>
void LinkedList<T>::insert(T value)
{
if (first == NULL)
{
Node<T> *newNode = new Node<T>;
newNode->data = value;
first = newNode;
first->next = NULL;
size++;
}
else
{
Node<T> *head = getNode(size-1);
Node<T> *newNode = new Node<T>;
newNode->data = value;
newNode->next = head->next;
head->next = newNode;
size++;
}
}
template<typename T>
void LinkedList<T>::insert(int index, T value)
{
if (!checkRange(index)) //忽略索引为负值
{
return;
}
if (index == 0)
{
Node<T>* newNode = new Node<T>;
newNode->data = value;
newNode->next = first;
first = newNode;
size++;
}
else
{
Node<T>* head = getNode(index-1);
Node<T>* newNode = new Node<T>;
newNode->data = value;
newNode->next = head->next;
head->next = newNode;
size++;
}
}
template<typename T>
void LinkedList<T>::print()
{
Node<T> *list = first;
cout << "[";
while (list != NULL) //判断当前位置是否存在节点
{
cout << list->data << " " ;
list = list->next;
}
cout << "]" << endl;
}
template<typename T>
int LinkedList<T>::length() const
{
return size;
}
template<typename T>
LinkedList<T>::~LinkedList()
{
if (first != NULL)
{
Node<T> *list = NULL, *cache = NULL;
list = first;
while (list != NULL)
{
cache = list->next;
delete list;
list = NULL;
list = cache;
}
}
}
template<typename T>
T LinkedList<T>::find(int index)
{
if (!checkRange(index))
return NULL;
Node<T> *head = getNode(index);
return head->data;
}
template<typename T>
void LinkedList<T>::remove(int index)
{
if(!checkRange(index)) return;
if (index == 0)
{
Node<T>* head = first;
first = head->next;
size--;
}
else
{
Node<T>* head = getNode(index - 1);
head->next = (head->next)->next;
size--;
}
}
int main()
{
LinkedList<int> a;
cout << "测试是否完成删除:"; a.print();
a.insert(0);
a.insert(1);
a.insert(-1, 1); //自动忽略
a.insert(2);
a.insert(3);
a.insert(4);
a.print();
cout << "在索引2位置插入10:"; a.insert(2, 10);a.print();
cout << "在索引0位置插入112:";a.insert(0, 112);a.print();
cout << "移除索引2:"; a.remove(2);a.print();
cout << "移除索引0:";a.remove(0);a.print();
cout << "移除最后一个索引:"; a.remove(a.length() - 1); a.print();
cout << "索引0的值:" << a.find(0) << endl
<< "索引3的值:" << a.find(3) << endl
<< "最后一位:" << a.find(a.length() - 1);
}