手写list(c++)

该文章介绍了如何在C++中使用模板创建一个双向链表类,包括初始化、添加元素(push_Back和push_Front)、删除元素(pop_Back和pop_Front)、检查是否为空、打印链表以及定义迭代器的方法。
摘要由CSDN通过智能技术生成

#include<iostream>
using namespace std;
//创建链表类
template<typename T>
class My_list {
    using element = T;
    struct node{
        element ele;
        node* next;
        node* pre;
        node() {
            next = nullptr;
            pre = nullptr;
        }
    };
public:
    My_list():lenth(0) //初始化链表
    {
        head = tail = new node;
    }
    node* begin() {
        return head->next;
    }
    node* end() {
        return tail->next;
    }
    bool empty() {
        return lenth==0;
    }
    void push_Back(element e) {
        node* tmp = new node;
        tmp->ele = e;
        tmp->pre = tail;
        tail->next = tmp;
        tail = tmp;
        lenth++;
    }
    void pop_Back() {
        if (empty()) {
            return;
        }
        else {
            node* tmp=tail->pre;
            delete tail;
            tail = tmp;
            lenth--;
        }
    }
    void push_Front(element e) {
        node* tmp = new node;
        tmp->ele = e;
        node* s = head->next;
        tmp->next = head->next;
        s->pre = tmp;
        tmp->pre = head;
        head->next = tmp;
    }
    void pop_Front() {
        if (empty()) {
            return;
        }
        node* tmp = head->next;
        head->next = tmp->next;
        if (tmp->next == nullptr)
        {
            tail = head;
        }
        else {
            tmp->next->pre = head;
        }
        delete tmp;
        lenth--;
    }
    void print_List() {
        if (head->next == nullptr) {}
        else {
            node* ptr = new node;
            ptr = head->next;
            while (ptr!=nullptr) {
                cout << ptr->ele << " ";
                ptr = ptr->next;
            }
        }
    }
    My_list(const My_list& list) {
        head = tail = new node;
        node* ptr = list.head->next;
        while (ptr != nullptr) {
            push_Back(ptr->ele);
            ptr = ptr->next;
        }
    }
    void operator=(My_list list) {
        My_list(list);
    }
    //定义链表指针
    class iterator {
    public:
        node* it;
        iterator(node* x) {//指针初始化
            it = x;
        }
        element& operator*() {
            return it->ele;
        }
        iterator& operator++() {
                it = it->next;
                return *this;
        }
        iterator& operator++(int) {
            iterator tmp = *this;
            it = it->next;
            return tmp;
        }
        bool operator != (const iterator& it) const {  // 重载运算符
            return this->it != it.it;
        }
    };
public:
    int lenth;//存放链表长度
    node* head;
    node* tail;
};
int main() {
    
}

以下是一个手写的HashMap的C++代码示例: ```cpp #include <iostream> #include <vector> using namespace std; class HashMap { private: vector<pair<int, int>> buckets; int size; public: HashMap() { size = 10000; buckets.resize(size); } void insert(int key, int value) { int index = key % size; buckets[index = make_pair(key, value); } int find(int key) { int index = key % size; if (buckets[index].first == key) { return buckets[index].second; } return 0; } }; int main() { HashMap map; map.insert(1, 1); map.insert(2, 2); map.insert(3, 3); map.insert(4, 4); cout << map.find(1) << endl; cout << map.find(2) << endl; cout << map.find(3) << endl; cout << map.find(4) << endl; return 0; } ``` 这个HashMap类使用除留余数法来处理冲突。它具有插入和查找功能。在插入时,将关键字和值存储在散列表的对应位置。在查找时,根据关键字找到对应的位置,并返回对应的值。如果没有找到对应的关键字,则返回0。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [手写HashMap](https://blog.csdn.net/weixin_45750972/article/details/121058487)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C++ 手写简单哈希散列表](https://blog.csdn.net/weixin_44178960/article/details/125766207)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值