参考了网上的一些资料,同时也是主要想参照c#的列表写一个类似的
整个类功能有添加,删除,排序(一共两个,一个是正排序,一个是自定义排序),判定value存不存在,判定是否为空,size显示,打印全部,清空列表,用中括号index取值这几个功能
// test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
#include<stdio.h>
using namespace std;
template <class T>
class Node
{
public:
Node<T> *next;
Node<T> *prev;
T date;
};
template <class T>
class List
{
private:
Node<T> *head;
Node<T> *tail;
int length;
public:
List();
List(const List& ln);
~List();
void Add(T e);
void Sort();
void Remove(T e);
bool IsContain(T index);
bool isEmpty();
int szie();
void Show();
void Clear();
T operator[](int e);
void Sort(int(*result)(T, T));
};
template<typename T>
void List<T>::Sort(int(*result)(T e1,T e2))
{
if (length <= 1) return;
Node<T> *p = head->next;
for (int i = 0; i < length - 1; i++)
{
Node<T> *q = p->next;
for (int j = i + 1; j < length; j++)
{
if ((*result)(p->date, q->date) > 0)
{
T temp = p->date;
p->date = q->date;
q->date = temp;
}
else if ((*result)(p->date, q->date) < 0)
{
T temp = q->date;
q->date = p->date;
p->date = temp;
}
q = q->next;
}
p = p->next;
}
}
template<typename T>
List<T>::List()
{
head = new Node<T>;
tail = new Node<T>;
head->next = tail;
head->prev = nullptr;
tail->next = nullptr;
tail->prev = head;
length = 0;
}
template<typename T>
List<T>::List(const List &ln)
{
head = new Node<T>;
tail = new Node<T>;
head->prev = nullptr;
head->next = tail;
tail->prev = head;
tail->next = nullptr;
length = 0;
Node<T> *temp = ln.head;
while (temp->next != ln.tail)
{
temp = temp->next;
tail->date = temp->date;
Node<T> *p = new Node<T>;
p->prev = tail;
tail->next = p;
tail = p;
length++;
}
tail->next = nullptr;
}
template<typename T>
void List<T>::Add(T e)
{
Node<T> *temp = this->tail;
tail->date = e;
tail->next = new Node<T>;
Node<T> *p = tail;
tail = tail->next;
tail->next = nullptr;
tail->prev = p;
length++;
}
template<typename T>
void List<T>::Remove(T index)
{
if (length == 0)
{
cout << "list is empty";
return NULL;
}
Node<T> *p = head;
while (p->next != nullptr)
{
p = p->next;
if (p->date == index)
{
Node<T> *temp = p->prev;
temp->next = p->next;
p->next->prev = temp;
delete p;
return;
}
}
}
template<typename T>
void List<T>::Clear()
{
if (length == 0)
{
return;
}
Node<T> *p = head->next;
Node<T> *temp;
while (p != tail)
{
temp = p;
p = p->next;
delete temp;
}
head->next = tail;
tail->prev = head;
length = 0;
}
template <typename T>
void List<T>::Sort()
{
if (length <= 1) return;
Node<T> *p = head->next;
for (int i = 0; i < length - 1; i++)
{
Node<T> *q = p->next;
for (int j = i + 1; j < length; j++)
{
if (p->date > q->date)
{
T temp = p->date;
p->date = q->date;
q->date = temp;
}
q = q->next;
}
p = p->next;
}
}
template <typename T>
bool List<T>::isEmpty()
{
if (length == 0)
{
return true;
}
else
{
return false;
}
}
template <typename T>
int List<T> ::szie()
{
return length;
}
template <typename T>
bool List<T>::IsContain(T e)
{
if (length == 0)
{
cout << "list is empty";
return false;
}
Node<T> *p = head->next;
while (p->next != nullptr)
{
if (p->date == e)
{
return true;
}
p = p->next;
}
return false;
}
template <typename T>
void List<T>::Show()
{
if (length == 0)
{
cout << "list is empty";
return;
}
Node<T> *p = head->next;
while (p != tail)
{
cout << p->date << "\n";
p = p->next;
}
cout << endl;
}
template <typename T>
List<T>::~List()
{
if (length == 0)
{
delete head;
delete tail;
head = nullptr;
tail = nullptr;
return;
}
while (head->next != nullptr)
{
Node<T> *temp = head;
head = head->next;
delete temp;
}
delete head;
head = nullptr;
}
template <typename T>
T List<T>::operator[](int index)
{
if (index < 0)
{
cout << "index is OUT bouns";
return 0;
}
if (index < 2 / length) {
Node<T> *p = head;
int i = 0;
while (p->next != nullptr)
{
p = p->next;
if (i == index)
{
return p->date;
}
i++;
}
}
else
{
Node<T> *p = tail;
int i = length;
while (p->prev!=nullptr)
{
if (i == index)
{
return p->date;
}
p = p->prev;
i--;
}
}
return 0;
}
int result(int a, int b)
{
return a - b;
};
int main()
{
List<int>mylist;
for (int i = 0; i < 10; i++)
{
mylist.Add(i);
}
//cout << mylist[9];
mylist.Sort(result);
mylist.Show();
return 0;
}