C++手写list

#include <iostream>
using namespace std;

template <typename T>

class llist{
public:
    typedef T _type;
private:
    struct node{                        // 内嵌结构体,存每个节点的当前值和前后指针
        _type data;
        node* next;
        node* pre;
        node(){next=NULL, pre=NULL;}
    };
    node* head;
    node* tail;
    size_t _size;
public:
    llist() {                            // 构造函数
        head = tail = new node;
        _size = 0;
    }

    ~llist() {                           // 析构函数
        node* p = head;
        while(p!=NULL) {
            node* tem = p;
            p = p->next;
            delete tem;
        }
        tail = head;
        _size = 0;
    }
    llist(const llist& li) {             // 拷贝构造函数
        head = tail = new node;
        node* p = head->next;
        while (p!=NULL) {
            push_back(p->data);
            p = p->next;
        }
    }

    llist& operator=(const llist &li) {  // 重载赋值运算符
        if (&li == this) return *this;
        clear();
        head = tail = new node;
        node* p = li.head->next;
        while (p != NULL) {
            push_back(p->data);
            p = p->next;
        }
        //return *this;
    }

    void clear() {                       // 清空
        node* p = head->next;
        while (p != NULL) {
            node* tem = p;
            p = p->next;
            delete tem;
        }
        _size = 0;
        tail = head;
    }

    size_t size() const {               // 返回链表大小
        return _size;
    }

    bool empty() const{                 // 判断链表是否为空
        return _size==0;
    }

    _type front() const{                 // 返回表头
        return head->next->data;
    }

    _type back() const {                 // 返回表尾
        return tail->data;
    }

    void push_back(const _type& val) {  // 在尾部插入一个节点
        node* res = new node;
        res->data = val;
        res->next = NULL;
        res->pre = tail;
        tail->next = res;
        tail = res;
        _size++;
    }

    void pop_back() {                    // 在尾部删除一个节点
        if(empty()) return;
        node* tem = tail->pre;
        delete tail;
        tem->next = NULL;
        tail = tem;
        _size--;
    }

    void push_front(const _type& val) {  // 在头部插入一个节点
        node* res = new node;
        res->data = val;
        res->next = head->next;
        res->pre = head;
        if(head->next == NULL) tail = res;
        else head->next->pre = res;
        head->next = res;
        _size++;
    }

    void pop_front() {                    // 在头部删除一个节点
        if(empty()) return;
        node* tem = head->next;
        head->next = tem->next;
        if(tem->next == NULL) tail = head;
        else tem->next->pre = head;
        delete tem;
        _size--;
    }

    void Print()const{                     // 正序逆序分别打印链表
        node* p = head->next;
        while(p!=NULL) {
            cout<<p->data<<" ";
            p = p->next;
        }
        cout<<endl;

        p = tail;
        while(p!=head) {
            cout<<p->data<<" ";
            p = p->pre;
        }
        cout<<endl;
    }

    class iterator {                      // 迭代器
        node* res;
    public:
        iterator(node* x){res=x;}

        _type& operator*() {              // 重载运算符
            return res->data;
        }

        _type* operator->() {
            return &res->data;
        }

        iterator operator++() {
            res = res->next;
            return *this;
        }

        iterator operator--() {
            res = res->pre;
            return *this;
        }

        iterator operator++(int) {
            iterator tem = res;
            res = res->next;
            return tem;
        }

        iterator operator--(int) {
            iterator tem = res;
            res = res->pre;
            return tem;
        }

        bool operator == (const iterator& it) const {
            return res == it.res;
        }

        bool operator != (const iterator& it) const {  // 重载运算符
            return res != it.res;
        }
    };

    iterator begin() {                    // 返回第一个节点的迭代器
        return iterator(head->next);
    }

    iterator end() {
        return iterator(tail->next);      // 返回最后一个节点的next节点
    }
};

int main() {
    llist<string> li;
    string n;
    for(int i = 1;i <= 5;i++) {
        cin>>n;
        li.push_back(n);                          // 插入节点
    }
    llist<string>::iterator it = li.begin();

    while(it!=li.end()){
        cout<<*it<<" ";
        ++it;                                      // 测试自增运算符
    }
    cout<<endl;

    for(auto x:li) {
        cout<<x<<" ";
    }
    cout<<endl;

    llist<string> li2 = li;
    llist<string> li3;
    li3 = li;
    for(auto n:li3){
        cout << n << " ";
    }
    cout << endl;

    li.Print();

    while(!li.empty()){
        // li.pop_front();
	li.pop_back();                                //测试尾删函数
	li.Print();
    }
    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值