#ifndef MYLIST_H
#define MYLIST_H
#include<iostream>
#include<iomanip>
#include<string>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
using namespace std;
#define ture 1
#define false 0
const int MAX = 105;
bool flag = 0;
template <typename T>
class MyList
{
public:
struct Node
{
T data;
Node *next, *front;
};
typedef Node ListNode;
private:
Node *head;
Node *tail;
int length;
public:
class iterator;
class reverse_iterator;
bool compare(T a, T b){
return a < b;
}
bool (*cmp)(T a, T b);
MyList();
MyList(MyList &other);
~MyList();
T& back();
void clear();
inline bool empty();
T& front();
int find(const T& t) const;
T* find(int pos);
void insert(int pos, const T &t);
void pop_back();
void pop_front();
void push_back(const T &t);
void push_front(const T &t);
void remove(const T& t);
void removeAt(int pos);
void remove_if();
void reverse();
int size() const { return length; }
void swap(int i, int j);
void unique();
void swap(MyList<T> &other);
void print_back();
void print_front();
void print(Node *n);
void merge(MyList &other);
void sort();
class iterator
{
public:
Node *i;
iterator(Node *p) : i(p) {}
iterator(const iterator &o) : i(o.i){}
iterator operator=(const Node* p){ return iterator(p);}
inline T &operator*() const { return i->data; }
inline T *operator->() const { return &i->data; }
inline bool operator==(const iterator &o) { return i == o.i; }
inline bool operator!=(const iterator &o) { return i != o.i; }
inline bool operator<(const iterator& other) { return i < other.i; }
inline bool operator<=(const iterator& other) { return i <= other.i; }
inline bool operator>(const iterator& other) { return i > other.i; }
inline bool operator>=(const iterator& other) { return i >= other.i; }
inline iterator &operator++() { i = i->next; return *this; }
inline iterator operator++(int) { Node *n = i; i = i->next; return n; }
inline iterator &operator--() { i = i->front; return *this; }
inline iterator operator--(int) { Node *n = i; i = i->front; return n; }
inline int operator-(iterator j) const { return int(i - j.i); }
};
class reverse_iterator
{
public:
Node *i;
reverse_iterator(Node *p) : i(p) {}
reverse_iterator(const iterator &o) : i(o.i){}
reverse_iterator operator=(const Node* p){ return reverse_iterator(p);}
inline T &operator*() const { return i->data; }
inline T *operator->() const { return &i->data; }
inline bool operator==(const reverse_iterator &o) { return i == o.i; }
inline bool operator!=(const reverse_iterator &o) { return i != o.i; }
inline bool operator<(const reverse_iterator& other) { return i < other.i; }
inline bool operator<=(const reverse_iterator& other) { return i <= other.i; }
inline bool operator>(const reverse_iterator& other) { return i > other.i; }
inline bool operator>=(const reverse_iterator& other) { return i >= other.i; }
inline reverse_iterator &operator++() { i = i->front; return *this; }
inline reverse_iterator operator++(int) { Node *n = i; i = i->front; return n; }
inline reverse_iterator &operator--() { i = i->next; return *this; }
inline reverse_iterator operator--(int) { Node *n = i; i = i->next; return n; }
inline int operator-(reverse_iterator j) const { return int(i - j.i); }
};
iterator erase(iterator& iter);
iterator begin()
{
iterator iter(head->next);
return iter;
}
iterator end()
{
iterator iter(tail);
return iter;
}
void splice(iterator& position, MyList& other);
reverse_iterator rbegin()
{
return reverse_iterator(tail->front);
}
reverse_iterator rend()
{
return reverse_iterator(head);
}
private:
Node *mergeSort(Node* temHead);
ListNode *merge(Node* first, Node* second);
void mergeTail();
};
template<typename T>
MyList<T>::MyList()
{
MyList<T>::Node *node = new MyList<T>::Node;
head = node;
node = new MyList<T>::Node;
tail = node;
head->next = tail;
head->front = NULL;
tail->front = head;
tail->next = NULL;
length = 0;
}
template<typename T>
MyList<T>::MyList(MyList &other)
{
Node *p_pre = new Node;
head = p_pre;
head->front = head->next = NULL;
Node *p_next;
MyList<T>::iterator iter = other.begin();
for(; iter != other.end(); ++iter)
{
p_next = new Node;
p_next->data = (*iter);
p_next->front = p_pre;
p_next->next = NULL;
p_pre->next = p_next;
p_next = p_next->next;
p_pre = p_pre->next;
}
tail = new Node;
tail->next = NULL;
tail->front = p_pre;
p_pre->next = tail;
}
template<typename T>
MyList<T>::~MyList()
{
delete head;
delete tail;
}
template<typename T>
T& MyList<T>::back()
{
return tail->front->data;
}
template<typename T>
bool MyList<T>::empty()
{
return (length == 0);
}
template<typename T>
typename MyList<T>::iterator MyList<T>::erase(iterator& iter)
{
if(empty())
return iterator(head);
Node* p_delete = iter.i;
Node* p_pro = head;
while(p_pro->next != tail)
{
if(p_pro->next == p_delete)
{
p_pro->next = p_delete->next;
p_delete->next->front = p_pro;
delete p_delete;
--length;
iterator it(p_pro->next);
return it;
}
p_pro = p_pro->next;
}
return iterator(head);
}
template<typename T>
T& MyList<T>::front()
{
return head->next->data;
}
template<typename T>
void MyList<T>::merge(MyList &other)
{
if(empty())
return;
if(other.empty())
return;
sort(); other.sort();
tail->front->next = NULL;
other.tail->front->next = NULL;
head->next = merge(head->next, other.head->next);
length += other.size();
other.head->next = other.tail;
other.tail->front = other.head;
other.clear();
mergeTail();
}
template<typename T>
typename MyList<T>::ListNode *MyList<T>::merge(Node* first, Node* second)
{
Node* resList = new Node;
Node* current = resList;
while(first != NULL && second != NULL)
{
if(compare(first->data, second->data))
{
current->next = first;
first->front = current;
current = current->next;
first = first->next;
}
else
{
current->next = second;
second->front = current;
current = current->next;
second = second->next;
}
}
while(first != NULL)
{
current->next = first;
first->front = current;
current = current->next;
first = first->next;
}
while(second != NULL)
{
current->next = second;
second->front = current;
current = current->next;
second = second->next;
}
current = resList->next;
current->front = NULL;
delete resList;
return current;
}
template<typename T>
typename MyList<T>::ListNode* MyList<T>::mergeSort(Node* temHead)
{
Node* first = temHead;
Node* second = temHead;
if(first == NULL || first->next == NULL)
{
return first;
}
while(second->next != NULL && second->next->next != NULL)
{
first=first->next;
second=second->next->next;
}
if(first->next != NULL)
{
second = first->next;
first->next = NULL;
first = temHead;
}
return merge(mergeSort(first),mergeSort(second));
}
template<typename T>
void MyList<T>::mergeTail()
{
Node *p = head->next;
while(p->next != NULL)
p = p->next;
tail->front = p;
p->next = tail;
}
template<typename T>
void MyList<T>::push_front(const T& t)
{
Node *node = new Node;
node->data = t;
node->next = head->next;
node->front = head;
node->next->front = node;
head->next = node;
++length;
}
template<typename T>
void MyList<T>::push_back(const T& t)
{
Node *node = new Node;
node->data = t;
node->next = tail;
node->front = tail->front;
node->front->next = node;
tail->front = node;
++length;
}
template<typename T>
void MyList<T>::insert(int pos, const T& t)
{
if(pos <= 0)
{
push_front(t);
return;
}
if(pos >= length)
{
push_back(t);
return;
}
if((pos < length) && (pos > 0))
{
Node *p = head;
while(pos--)
p = p->next;
Node *node = new Node;
node->data = t;
node->next = p->next;
node->front = p;
node->next->front = node;
node->front->next = node;
++length;
}
}
template<typename T>
int MyList<T>::find(const T& t) const
{
Node *p = head;
int pos = -1;
while(p->next)
{
p = p->next;
++pos;
if(p->data == t)
return pos;
}
return pos;
}
template<typename T>
T* MyList<T>::find(int pos)
{
if(pos >= length || pos < 0)
{
cout << "index out of range" << endl;
return NULL;
}
Node *p = head->next;
while(pos--)
p = p->next;
return &p->data;
}
template<typename T>
void MyList<T>::removeAt(int pos)
{
if(pos >= length)
return;
if(pos < 0)
return;
Node *p_pro = head;
int i = pos;
while(i--)
{
p_pro = p_pro->next;
}
Node *p_delete = p_pro->next;
p_pro->next = p_delete->next;
p_delete->next->front = p_pro;
delete p_delete;
--length;
}
template<typename T>
void MyList<T>::remove(const T& t)
{
removeAt(find(t));
}
template<typename T>
void MyList<T>::remove_if()
{
}
template<typename T>
void MyList<T>::sort()
{
if(empty() || length == 1)
return;
tail->front->next = NULL;
head->next = mergeSort(head->next);
mergeTail();
}
template<typename T>
void MyList<T>::swap(int i, int j)
{
T *p_i = find(i);
T *p_j = find(j);
if(p_i != NULL && p_j != NULL)
std::swap(*p_i,*p_j);
else
cout << "Swap failed" << endl;
}
template<typename T>
void MyList<T>::swap(MyList<T> &other)
{
MyList<T>::Node *p_head = head;
MyList<T>::Node *p_end = tail;
head = other.head;
tail = other.tail;
other.head = p_head;
other.tail = p_end;
std::swap(length,other.length);
}
template<typename T>
void MyList<T>::clear()
{
if(head->next == tail)
length = 0;
if(length == 0)
return;
Node *p = head->next;
Node *p_delete = p;
do
{
p = p->next;
delete p_delete;
p_delete = p;
--length;
}while(length > 0);
length = 0;
head->next = tail;
tail->front = head;
}
template<typename T>
void MyList<T>::pop_front()
{
removeAt(0);
}
template<typename T>
void MyList<T>::pop_back()
{
removeAt(length-1);
}
template<typename T>
void MyList<T>::unique()
{
if(empty())
return;
char isRepeat[length];
memset(isRepeat,0,length);
MyList<T>::iterator iter = begin();
MyList<T>::iterator iter_r = begin();
iter_r++;
for(int i = 1; iter_r != end(); ++iter, ++iter_r, ++i)
if(*iter == *iter_r)
isRepeat[i] = '1';
int l = length;
int erased = 0;
for(int i = 1; i < l; ++i)
if(isRepeat[i] == '1')
{
removeAt(i-erased);
++erased;
}
}
template<typename T>
void MyList<T>::reverse()
{
if(empty())
return;
MyList<T>::Node *p = head;
MyList<T>::Node *p_pre = NULL;
MyList<T>::Node *p_next = p->next;
while(p)
{
p_next = p->next;
p->next = p_pre;
p->front = p_next;
p_pre = p;
p = p_next;
}
p = head;
head = tail;
tail = p;
}
template<typename T>
void MyList<T>::print_front()
{
Node *p = head->next;
while(p != tail)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
template<typename T>
void MyList<T>::print_back()
{
Node *p = tail->front;
while(p != head)
{
cout << p->data << " ";
p = p->front;
}
cout << endl;
}
template<typename T>
void MyList<T>::print(Node *n)
{
Node *p = n;
while(p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
template<typename T>
void MyList<T>::splice(iterator& position, MyList& other)
{
if(empty())
{
head = other.head;
tail = other.tail;
other.head->next = other.tail;
other.tail->front = other.head;
other.length = 0;
return;
}
if(other.empty())
return;
Node *p = position.i;
Node *p_p = position.i;
p = p->next;
length = size() + other.size();
p_p->next = other.head->next;
other.tail->front->next = p;
other.head->next = other.tail;
other.tail->front = other.head;
other.length = 0;
}
#endif
#include"mylist.h"
#include <list>
void Test_erase();
MyList<string> Eg;
int main()
{
/************主函数用于测试***************/
cout << "********程序函数测试结果**********" << endl;
Eg.print_back();
/************插入及删除类函数测试*************/
cout << "\n--------1.push_front()-----------" << endl;
Eg.push_front("D");
Eg.push_front("A");
Eg.push_front("B");
Eg.print_front();
cout << endl;
cout << "\n--------2.insert(-1,\"C\")-----------" << endl;
Eg.insert(-1,"C");
Eg.print_front();
cout << endl;
cout << "\n--------2.insert(4,\"E\")-----------" << endl;
Eg.insert(4,"E");
Eg.print_front();
cout << endl;
cout << "\n--------2.insert(8,\"F\")-----------" << endl;
Eg.insert(8,"F");
Eg.print_front();
cout << endl;
cout << "\n--------3.push_back(\"H\")-----------" << endl;
Eg.push_back("H");
Eg.print_front();
cout << endl;
cout << "\n--------4.removeAt(4)-----------" << endl;
Eg.removeAt(4);
Eg.print_front();
cout << endl;
cout << "\n--------4.removeAt(-1)-----------" << endl;
Eg.removeAt(-1);
Eg.print_front();
cout << endl;
cout << "\n--------5.remove(\"G\")-----------" << endl;
Eg.remove("G");
Eg.print_front();
cout << endl;
cout << "\n--------5.remove(\"B\")-----------" << endl;
Eg.remove("B");
Eg.print_front();
cout << endl;
cout << "\n--------5.remove(\"W\")-----------" << endl;
Eg.remove("W");
Eg.print_front();
cout << endl;
cout << "\n--------6.pop_front()-----------" << endl;
Eg.pop_front();
Eg.print_front();
cout << endl;
cout << "\n--------7.pop_back()-----------" << endl;
Eg.pop_back();
Eg.print_front();
cout << endl;
cout << "\n--------8.swap(0,2)-----------" << endl;
Eg.swap(0,2);
Eg.print_front();
cout << endl;
cout << "\n--------8.swap(0,4)-----------" << endl;
Eg.swap(0,4);
Eg.print_front();
cout << endl;
/********清空测试***********/
cout << "\n--------9.clear()-----------" << endl;
Eg.clear();
Eg.print_front();
cout << endl;
/********加入元素************/
Eg.push_back("A");
Eg.push_back("B");
Eg.push_back("C");
Eg.push_back("D");
Eg.push_back("F");
Eg.push_back("H");
/*************迭代器测试*********/
cout << "\n--------10.iterator-----------" << endl;
MyList<string>::iterator iter = Eg.begin();
for(; iter != Eg.end(); ++iter)
cout << " " << (*iter);
cout << endl;
cout << endl;
/*************逆向迭代器测试*********/
cout << "\n--------11.reverse_iterator-------\n";
MyList<string>::reverse_iterator r_iter = Eg.rbegin();
for(;r_iter != Eg.rend(); ++r_iter)
cout << " " << (*r_iter);
cout << endl;
cout << endl;
/************erase()测试*********/
cout << "\n--------12.erase()--------" << endl;
Test_erase();
cout << endl;
/*********排序测试**********/
cout << "\n--------13.sort()--------" << endl;
cout << "\n-------Before sort()------\n";
Eg.swap(0,3);
Eg.swap(1,2);
Eg.print_front();
Eg.sort();
cout << "\n-------After sort()------\n";
Eg.print_front();
cout << endl;
/*********创建对象2并加入CEB*******/
MyList<string> Eg2;
Eg2.push_back("C");
Eg2.push_back("E");
Eg2.push_back("B");
/********合并对象1和对象2**********/
/*******List_2不空******/
cout << "\n--------14.List_1.merge(List_2)--------" << endl;
cout << "\n------Before merge()------\n";
cout << "\nList_1 中的元素:\n";
Eg.print_front();
cout << "\nList_2 中的元素(不空):\n";
Eg2.print_front();
cout << "\n-------After merge()-------\n";
Eg.merge(Eg2);
cout << "\nList_1 中的元素:\n";
Eg.print_front();
cout << "\nList_2 中的元素:\n";
Eg2.print_front();
cout << endl;
/*******List_2为空******/
cout << "\n--------14.List_1.merge(List_2)--------" << endl;
cout << "\n------Before merge()------\n";
cout << "\nList_1 中的元素:\n";
Eg.print_front();
cout << "\nList_2 中的元素(空):\n";
Eg2.print_front();
cout << "\n-------After merge()-------\n";
Eg.merge(Eg2);
cout << "\nList_1 中的元素:\n";
Eg.print_front();
cout << "\nList_2 中的元素:\n";
Eg2.print_front();
cout << endl;
MyList<string> Eg3;
Eg3.push_back("F");
Eg3.push_back("G");
/********splice合并测试************/
cout << "\n-------15.List_1.splice(iter, List_2)-------\n";
MyList<string>::iterator iter_ = Eg.begin();
++iter_;
cout << "\n------Before splice()------\n";
cout << "\nList_1 中的元素:\n";
Eg.swap(2,4);
Eg.print_front();
cout << "\nList_2 中的元素(不空):\n";
Eg3.print_front();
cout << "\n-------After splice()-------\n";
Eg.splice(iter_, Eg3);
cout << "\nList_1 中的元素:\n";
Eg.print_front();
cout << "\nList_2 中的元素:\n";
Eg3.print_front();
cout << endl;
Eg3.push_back("F");
Eg3.push_back("G");
Eg3.clear();
cout << "\n-------15.List_1.splice(iter, List_2)-------\n";
++iter_;
cout << "\n------Before splice()------\n";
cout << "\nList_1 中的元素:\n";
Eg.swap(2,4);
Eg.print_front();
cout << "\nList_2 中的元素(空):\n";
Eg3.print_front();
cout << "\n-------After splice()-------\n";
Eg.splice(iter_, Eg3);
cout << "\nList_1 中的元素:\n";
Eg.print_front();
cout << "\nList_2 中的元素:\n";
Eg3.print_front();
cout << endl;
Eg3.push_back("F");
Eg3.push_back("G");
/***********swap两个链表*******/
cout << "\n-------16.List_1.swap(List_2)-------\n";
cout << "\n-------Before swap()-------\n";
cout << "\nList_1 中的元素:\n";
Eg.print_front();
cout << "\nList_2 中的元素:\n";
Eg3.print_front();
Eg.swap(Eg3);
cout << "\n-------After swap()-------\n";
cout << "\nList_1 中的元素:\n";
Eg.print_front();
cout << "\nList_2 中的元素:\n";
Eg3.print_front();
cout << endl;
cout << "\n-------16.List_1.swap(List_2)-------\n";
cout << "\n-------Before swap()-------\n";
cout << "\nList_1 中的元素:\n";
Eg.print_front();
cout << "\nList_2 中的元素:\n";
Eg3.print_front();
Eg.swap(Eg3);
cout << "\n-------After swap()-------\n";
cout << "\nList_1 中的元素:\n";
Eg.print_front();
cout << "\nList_2 中的元素:\n";
Eg3.print_front();
cout << endl;
/*************插入*************/
Eg3.push_back("A");
Eg3.push_back("A");
Eg3.push_back("B");
Eg3.push_back("A");
Eg3.push_back("A");
Eg3.push_back("B");
Eg3.push_back("C");
Eg3.push_back("C");
Eg3.push_back("C");
/************unique删除相邻重复项********/
cout << "\n-------17. unique()-------\n";
cout << "\n-------Before unique()-------\n";
Eg3.print_front();
Eg3.unique();
cout << "\n-------After unique()-------\n";
Eg3.print_front();
cout << endl;
/***********reverse逆置链表*************/
cout << "\n-------18. reverse()-------\n";
cout << "\n-------Before reverse()-------\n";
Eg3.push_back("A");
Eg3.push_back("B");
Eg3.push_back("B");
Eg3.push_back("C");
Eg3.print_front();
cout << "\n-------After reverse()-------\n";
Eg3.reverse();
Eg3.print_front();
cout << endl;
/***********构造函数*************/
cout << "\n-------19. List_1(List_2)-------\n";
MyList<string> Eg4(Eg3);
Eg4.print_front();
}
void Test_erase()
{
MyList<string>::iterator iter = Eg.begin();
cout << "\n删除指向第 4 个项目的迭代器:" << endl;
int i = 3;
iter = Eg.begin();
while(i--)
++iter;
iter = Eg.erase(iter);
Eg.print_front();
cout << "\n删除 begin() 返回的迭代器:\n";
iter = Eg.begin();
iter = Eg.erase(iter);
Eg.print_front();
}