C++ 模拟List

#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;


//C++中STL中的list是一个双向链表
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;
    /*用于排序的compare和函数指针*/
    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();          //如果list是空的则返回true
    T& front();                   //返回第一个元素
    int find(const T& t) const;   //查找元素返回位置
    T* find(int pos);             //根据位置返回指针
    void insert(int pos, const T &t);   //在pos位置插入一个元素
    void pop_back();              //删除最后一个元素
    void pop_front();             //删除第一个元素
    void push_back(const T &t);   //尾插
    void push_front(const T &t);  //前插
    void remove(const T& t);      //从list删除元素
    void removeAt(int pos);       //删除pos位置的元素
    void remove_if();             //按指定条件删除元素**
    void reverse();               //把list元素倒转
    int size() const { return length; }    //返回list中的元素个数
    void swap(int i, int j);      //交换两个元素
    void unique();                //删除重复元素
    void swap(MyList<T> &other);  //交换两个list
    void print_back();            //从前遍历输出
    void print_front();           //从后遍历输出
    void print(Node *n);          //遍历后续节点

    //合并两个链表,默认升序排序
    void merge(MyList &other);
  //  int erase(int begin,int end)  //删除区间[begin,end]内的所有元素,并返回下一元素的位置
 //   void insert(int pos, begin,end)   //在pos位置插入区间[begin,end]内的全部元素

    /*****归并排序算法实现sort*****/
    void sort(/*bool (*cmp)(T a,T b)*/);    //排序

    class iterator
    {
    public:
        Node *i;
   //     typedef T* differrence_type

        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; }
      //  T& operator*() const { return  i->data; }
      //  Node* operator->() const { return i; }
        inline int operator-(iterator j) const { return int(i - j.i); }
    };

 //   typedef std::reverse_iterator<iterator> reverse_iterator;

    /*reverse_iterator*/
    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);}
      //  T& operator*() const { return  i->data; }
      //  Node* operator->() const { return i; }
        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); }

    };

    /**********erase()、begin()、end()、splice()**********/
    iterator erase(iterator& iter);   //删除一个元素

    iterator begin()  //返回指向第一个元素的迭代器
    {
        iterator iter(head->next);
        return iter;  //此处为首节点
    }

    iterator end()     //返回末尾的迭代器
    {
        iterator iter(tail);
        return iter;    //此处为尾节点之后
    }

    //将链表对象other归并到当前链表的迭代器后,归并后other将被清空
    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)
    {
       // cout << p_pro->next->data;
       // system("pause");
        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;
    }

    //failed
    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;
    //调用merge
    head->next = merge(head->next, other.head->next);
    length += other.size();

    //改变other的头尾哨点
    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)
{
    //注意到这里链表first,second已经是顺序的了
    Node* resList = new Node;   //开辟一个临时头节点
    Node* current = resList;
    while(first != NULL && second != NULL)
    {
        //某一条链表空时结束
       if(compare(first->data, second->data))   //默认升序(first > second)
       {
            //双向链表连接的时候要注意
            current->next = first;      //连接first链表的第一个节点
            first->front = current;
            current = current->next;
            first = first->next;    //first指针后移,第一个节点改变
        }
        else
        {
            current->next = second; //连接second链表的第一个节点
            second->front = current;
            current = current->next;
            second = second->next;  //second指针后移,第一个节点改变
        }
    }

    //剩余元素加入到链表中
    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;  //first指针指向头
    Node* second = temHead; //second指针指向头
    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;   //second指针指向二分点
        first->next = NULL;     //将整个链表断开为两个
        first = temHead;        //first指针指向头
    }
/*    print(first);
    print(second);
    system("pause");*/
    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;    //注意:首节点为head->next而不是head
    node->front = head;     //新首节点的front指向哨点

    node->next->front = node;   //原首节点的front指针指向node
    head->next = node;      //哨点的next指向新首节点
    ++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;      //注意:尾节点为tail->front而不是tail

    node->front->next = node;   //原尾节点的next指向node
    tail->front = node;    //尾指针后移
    ++length;
}

template<typename T>
void MyList<T>::insert(int pos, const T& t)
{
    if(pos <= 0)    //小于等于0,插入作为首节点
    {
        push_front(t);
        return;
    }

    if(pos >= length)    //大于等于length,插入作为尾节点
    {
        push_back(t);
        return;
    }

    if((pos < length) && (pos > 0))    //小于Length
    {
        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;
    }
    //未找到返回-1
    return pos;
}

template<typename T>
T* MyList<T>::find(int pos)
{
    if(pos >= length || pos < 0)
    {
        cout << "index out of range" << endl;
        return NULL;
    }

    //0,1,2,3...
    Node *p = head->next;
    while(pos--)
        p = p->next;
    return &p->data;
}

//从0开始,0,1,2,3...
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(/*bool (*cmp)(T a,T b)*/)    //排序
{
//      this->cmp = cmp;
    if(empty() || length == 1)     //为空或为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;
 //       cout << p_delete->data;
        delete p_delete;
        p_delete = p;
        --length;
 //       cout << length;
 //       system("pause");
    }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>
MyList<T>::iterator MyList<T>::end()
{
    return iterator(tail);
}*/

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->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;
}

//将链表对象other归并到当前链表的迭代器后,归并后other将被清空
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;

    //指向当前链表position的后半段
    Node *p = position.i;
    Node *p_p = position.i;
    p = p->next;
    length = size() + other.size();

    p_p->next = other.head->next;  //加入到当前链表position后
    other.tail->front->next = p;    //other尾节点指向当前链表的后半段
    //改变other的头尾哨点
    other.head->next = other.tail;
    other.tail->front = other.head;
    other.length = 0;
}

#endif // MYLIST_H
// main.cpp
#include"mylist.h"
#include <list>
void Test_erase();

MyList<string> Eg;

int main()
{
    /************主函数用于测试***************/

    cout << "********程序函数测试结果**********" << endl;
 //   Eg.print_front();
    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.clear();
    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 << "Before the one of erased: " << (*iter);
    cout << "\n删除 begin() 返回的迭代器:\n";
    iter = Eg.begin();
    iter = Eg.erase(iter);
   // cout << "Before the one of erased: " << (*iter) << endl;
    Eg.print_front();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值