Answer to Question #239032 in C++ for Sword_Dancer

Question #239032
Create class or struct and functions,then implement doubly Linked List operations such as
1)insertion(at start, given pos and at end)
2)deletion(at start, given pos and at end)
3)search
4)reverse
5)reversing k Nodes
6)sort
7)display

Note:
1)Create a manu in main to call all the functions one-by-one
2)no global declarations
1
Expert's answer
2021-09-18T14:33:48-0400
#include <iostream>


using namespace std;


#include <cstdlib>
#include <functional>
#include <ostream>
template <typename T>
class Container {
public:
    Container() {
        m_start = m_end = nullptr;
        m_size = 0;
    }
    ~Container() { clear(); }
    void push_front(const T& data) {
        if (m_start) {
            Node* new_node = new Node();
            new_node->data = data;
            new_node->next = m_start;
            m_start->prev = new_node;
            m_start = new_node;
        }
        else {
            init(data);
        }
        m_size++;
    }
    void pop_front() {
        if (!m_start) return;
        if (m_start->next) {
            Node* next = m_start->next;
            next->prev = nullptr;
            delete m_start;
            m_start = next;
        }
        else {
            delete m_start;
            m_start = m_end = nullptr;
        }
        m_size--;
    }
    void push_back(const T& data) {
        if (m_end) {
            Node* new_node = new Node();
            new_node->data = data;
            new_node->prev = m_end;
            m_end->next = new_node;
            m_end = new_node;
        }
        else {
            init(data);
        }
        m_size++;
    }
    void pop_back() {
        if (!m_end) return;
        if (m_end->prev) {
            Node* prev = m_end->prev;
            prev->next = nullptr;
            delete m_end;
            m_end = prev;
        }
        else {
            delete m_end;
            m_start = m_end = nullptr;
        }
        m_size--;
    }
    void sort(std::function<bool(const T&, const T&)> cmp) {
        if (!m_start) return;
        Node* tmp = m_start;
        while (tmp) {
            Node* tmp_next = tmp->next;
            while (tmp_next) {
                if (cmp(tmp->data, tmp_next->data))
                    std::swap(tmp->data, tmp_next->data);
                tmp_next = tmp_next->next;
            }
            tmp = tmp->next;
        }
    }
    void remove_one(std::function<bool(const T&)> cnd) {
        Node* tmp = m_start;
        while (tmp) {
            if (cnd(tmp->data)) {
                remove_node(tmp);
                return;
            }
            tmp = tmp->next;
        }
    }
    void remove_all(std::function<bool(const T&)> cnd) {
        Node* tmp = m_start;
        while (tmp) {
            if (cnd(tmp->data)) {
                Node* bck = nullptr;
                if (tmp->prev)
                    bck = tmp->prev;
                else if (tmp->next)
                    bck = tmp->next;
                remove_node(tmp);
                if (!bck) return;
                tmp = bck;
            }
            else {
                tmp = tmp->next;
            }
        }
    }
    void clear() {
        if (!m_start) return;
        if (m_start == m_end) {
            delete m_start;
        }
        else {
            Node* tmp = m_start;
            while (tmp != m_end) {
                Node* next = tmp->next;
                delete tmp;
                tmp = next;
            }
        }
        m_size = 0;
        m_start = m_end = nullptr;
    }
    void for_each(std::function<void(T&)> fun) {
        Node* tmp = m_start;
        while (tmp) {
            fun(tmp->data);
            tmp = tmp->next;
        }
    }
    inline size_t size() const noexcept { return m_size; }
    Container<T>& operator=(Container<T>&& other) noexcept {
        clear();
        Node* tmp = other.m_start;
        while (tmp) {
            push_back(tmp->data);
            tmp = tmp->next;
        }
        other.clear();
        return *this;
    }
private:
    struct Node {
        Node() {
            next = nullptr;
            prev = nullptr;
        }
        ~Node() = default;
        T data;
        Node* next;
        Node* prev;
    };
    void init(const T& data) {
        m_start = new Node();
        m_start->data = data;
        m_end = m_start;
    }
    void remove_node(Node* node) {
        Node* prev = node->prev;
        Node* next = node->next;
        if (prev != nullptr) prev->next = next;
        if (next != nullptr) next->prev = prev;
        if (node == m_start) m_start = next;
        if (node == m_end) m_end = prev;
        delete node;
        m_size--;
    }
    Node* m_start;
    Node* m_end;
    size_t m_size;
};


int main() {
    Container<double> C;
    C.push_front(1.5);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS