首页 > 其他 > 详细

自己实现list

时间:2021-04-07 23:23:50      阅读:37      评论:0      收藏:0      [点我收藏+]

腾讯笔试题,对于student结构体,实现一个list,包括拷贝构造函数,双端push和pop,返回头尾元素,长度,clear,reverse等功能

#include <iostream>
using namespace std;
struct student
{
public:
    student(int _id, string _name, int _age) :
        id(_id), name(_name), age(_age)
    { }

    student(const student& rhs) = default;
    student() {
        id = 0;
    }

    int id;
    string name;
    int age;
};
template <typename T>
struct ListNode
{
    ListNode() {
        prev = nullptr;
        next = nullptr;
        item = T();
    };
    ListNode(const T& rhs) :
        prev(nullptr), next(nullptr), item(rhs)
    {}
    ListNode* prev;
    ListNode* next;
    T item;
};
template <typename T>
class List {
public:
    int Listsize = 0;
    List() {
        head->next = tail;
        tail->prev = head;
    }
    ListNode<T>* head = new ListNode<T>(), *tail = new ListNode<T>();
    void push_back(T item) {
        ListNode<T>* pre = tail->prev;
        ListNode<T>* newnode = new ListNode<T>(item);
        pre->next = newnode;
        tail->prev = newnode;
        newnode->prev = pre;
        newnode->next = tail;
        Listsize++;
    }

    void push_front(T item) {
        ListNode<T>* nxt = head->next;
        ListNode<T>* newnode = new ListNode<T>(item);
        head->next = newnode;
        newnode->prev = head;
        newnode->next = nxt;
        nxt->prev = newnode;
        Listsize++;
    }
    T back() {
        return tail->prev->item;
    }

    void pop_back() {
        ListNode* bac = tail->prev;
        tail->prev = bac->prev;
        bac->prev->next = tail;
        delete bac;
        bac = nullptr;
        Listsize--;
    }
    T front() {
        return head->next->item;
    }
    void pop_front() {
        ListNode* fro = head->next;
        head->next = fro->next;
        head->next->prev = head;
        delete fro;
        fro = nullptr;
        Listsize--;
    }
    int size() {
        return Listsize;
    }
    void clear() {
        Listsize = 0;
        ListNode *cur = head->next, *tmp;
        while (cur != tail) {
            tmp = cur;
            cur = cur->next;
            delete tmp;
            tmp = nullptr;
        }
        head->next = tail;
        tail->prev = head;
    }
    List(const List& list) {
        this->head = list.head;
        this->tail = list.tail;
        this->Listsize = list.Listsize;
    }
    void reverse() {
        ListNode<T>* cur = head, * pre = nullptr, * nxt = nullptr;
        while (cur) {

            nxt = cur->next;
            cur->next = pre;
            cur->prev = nxt;
            pre = cur;
            cur = nxt;
        }
        pre = head;
        head = tail;
        tail = pre;
    }
};


int main() {
    student stu1(1, "zhang", 18);
    student stu2(2, "yue", 19);
    student stu3(3, "zhang", 20);
    List<student> list;
    list.push_back(stu1);
    list.push_front(stu2);
    cout << list.size() << endl;
    List<student> list2(list);
    cout << list2.back().id << endl;
    
    list2.push_back(stu3);
    list2.reverse();
    cout << list2.back().id << endl;
    cout << list2.front().id << endl;
    return 0;
}
  

 

自己实现list

原文:https://www.cnblogs.com/clarathebest/p/14629573.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!