Skip to main content
Tweeted twitter.com/StackCodeReview/status/1284186139809767426
Became Hot Network Question
added 2 characters in body; edited tags
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257
#define indexTooLarge -1
template <typename T>
class LincedList
{
public:
    LincedList(){}
    template<typename... Types>
    LincedList(T value,Types... values) :  LincedList(values...)
    {
        push_front(value);
    }
    LincedList(T value)
    {
        push_front(value);
    }
    ~LincedList()
    {
        clear();
    }
    void push_back(T value_a)
    {
        if(tail == nullptr)
        {
            tail = new Node(nullptr,value_a);
            head = tail;
            m_size++;
            return;
        }
        tail->next = new Node(nullptr,value_a);
        tail = tail->next;
        m_size++;
    }
    void push_front(T value_a)
    {
        if(head == nullptr)
        {
            head = new Node(nullptr,value_a);
            tail = head;
            m_size++;
            return;
        }
        head = new Node(head,value_a);
        m_size++;
    }
    void clear()
    {
        Node *buffer;
        for(int i = 0;i<m_size;i++)
        {
            buffer = head;
            head = head->next;
            delete buffer;
        }
        m_size = 0;
    }
    void remove(unsigned int index)
    {
        if(index >= m_size)
            throw indexTooLarge;
        Node *currectNode = head;
        for(int i = 0; i < index-1;i++)
            currectNode = currectNode->next;
        Node* buffer = currectNode->next;
        currectNode->next = currectNode->next->next;
        delete buffer;
        m_size--;
    }
    void remove(unsigned int index,unsigned int lenght)
    {
        if(index+lenght >= m_size)
            throw indexTooLarge;
        Node *currectNode = head;
        for(int i = 0; i < index-1; i++)
            currectNode = currectNode->next;
        Node* buffer = currectNode;
        currectNode = currectNode->next;
        for(int i = 0; i < lenght; i++ )
        {
            Node* buffer2 = currectNode;
            currectNode = currectNode->next;
            delete buffer2;
        }
        buffer->next = currectNode;
        m_size -= lenght;
    }
    T& operator[](unsigned const int &index)
    {
        if(index >= m_size)
            throw indexTooLarge;
        if(index == m_size-1)
            return tail->value;
        Node *currectNode = head;
        for(unsigned int i = 0; i < index;i++)
            currectNode = currectNode->next;
        return currectNode->value;
    }
    void operator=(const LincedList &C1)
    {
        head->value = C1.head->value;
        Node *currectNode = new Node(nullptr,C1[1]);
        Node *C1CurrectNode = C1[1];
        head->next = currectNode;
        for(int i = 2; i < m_size; i++)
        {
            C1CurrectNode = C1CurrectNode->next;
            currectNode->next = new Node(nullptr,C1CurrectNode->value);
            currectNode = currectNode->next;
        }
        tail->value = C1.tail->value;
    }
    unsigned int size()
    {
        return m_size;
    }
private:
    struct Node
    {
         Node* next;
         T value;
         Node(Node* next_a, T value_a) : next(next_a) , value(value_a)
         {}
    };
    unsigned int m_size = 0;
    Node* head = nullptr;
    Node* tail = nullptr;
}; 

#include <iostream>
int main()
{
    LincedList<int> ll(0,1,2,3);
    std::cout << ll[1] << std::endl; //writes to console 1
    ll.remove(1); //removes the element at index 1
    std::cout << ll[1] << std::endl; //writes to console 2
    ll.push_back(4);//adds to the end 4
    ll.push_front(5);//adds 5 to the beginning
    std::cout << ll.size() << std::endl; //writes to console 5
    ll.remove(1,2); //remove all 2 to 3 elements
    std::cout << ll[1] << std::endl ; //writes to console 3
    return  0;
}
```
#define indexTooLarge -1
template <typename T>
class LincedList
{
public:
    LincedList(){}
    template<typename... Types>
    LincedList(T value,Types... values) :  LincedList(values...)
    {
        push_front(value);
    }
    LincedList(T value)
    {
        push_front(value);
    }
    ~LincedList()
    {
        clear();
    }
    void push_back(T value_a)
    {
        if(tail == nullptr)
        {
            tail = new Node(nullptr,value_a);
            head = tail;
            m_size++;
            return;
        }
        tail->next = new Node(nullptr,value_a);
        tail = tail->next;
        m_size++;
    }
    void push_front(T value_a)
    {
        if(head == nullptr)
        {
            head = new Node(nullptr,value_a);
            tail = head;
            m_size++;
            return;
        }
        head = new Node(head,value_a);
        m_size++;
    }
    void clear()
    {
        Node *buffer;
        for(int i = 0;i<m_size;i++)
        {
            buffer = head;
            head = head->next;
            delete buffer;
        }
        m_size = 0;
    }
    void remove(unsigned int index)
    {
        if(index >= m_size)
            throw indexTooLarge;
        Node *currectNode = head;
        for(int i = 0; i < index-1;i++)
            currectNode = currectNode->next;
        Node* buffer = currectNode->next;
        currectNode->next = currectNode->next->next;
        delete buffer;
        m_size--;
    }
    void remove(unsigned int index,unsigned int lenght)
    {
        if(index+lenght >= m_size)
            throw indexTooLarge;
        Node *currectNode = head;
        for(int i = 0; i < index-1; i++)
            currectNode = currectNode->next;
        Node* buffer = currectNode;
        currectNode = currectNode->next;
        for(int i = 0; i < lenght; i++ )
        {
            Node* buffer2 = currectNode;
            currectNode = currectNode->next;
            delete buffer2;
        }
        buffer->next = currectNode;
        m_size -= lenght;
    }
    T& operator[](unsigned const int &index)
    {
        if(index >= m_size)
            throw indexTooLarge;
        if(index == m_size-1)
            return tail->value;
        Node *currectNode = head;
        for(unsigned int i = 0; i < index;i++)
            currectNode = currectNode->next;
        return currectNode->value;
    }
    void operator=(const LincedList &C1)
    {
        head->value = C1.head->value;
        Node *currectNode = new Node(nullptr,C1[1]);
        Node *C1CurrectNode = C1[1];
        head->next = currectNode;
        for(int i = 2; i < m_size; i++)
        {
            C1CurrectNode = C1CurrectNode->next;
            currectNode->next = new Node(nullptr,C1CurrectNode->value);
            currectNode = currectNode->next;
        }
        tail->value = C1.tail->value;
    }
    unsigned int size()
    {
        return m_size;
    }
private:
    struct Node
    {
         Node* next;
         T value;
         Node(Node* next_a, T value_a) : next(next_a) , value(value_a)
         {}
    };
    unsigned int m_size = 0;
    Node* head = nullptr;
    Node* tail = nullptr;
}; 

#include <iostream>
int main()
{
    LincedList<int> ll(0,1,2,3);
    std::cout << ll[1] << std::endl; //writes to console 1
    ll.remove(1); //removes the element at index 1
    std::cout << ll[1] << std::endl; //writes to console 2
    ll.push_back(4);//adds to the end 4
    ll.push_front(5);//adds 5 to the beginning
    std::cout << ll.size() << std::endl; //writes to console 5
    ll.remove(1,2); //remove all 2 to 3 elements
    std::cout << ll[1] << std::endl ; //writes to console 3
    return  0;
}
```
#define indexTooLarge -1
template <typename T>
class LincedList
{
public:
    LincedList(){}
    template<typename... Types>
    LincedList(T value,Types... values) :  LincedList(values...)
    {
        push_front(value);
    }
    LincedList(T value)
    {
        push_front(value);
    }
    ~LincedList()
    {
        clear();
    }
    void push_back(T value_a)
    {
        if(tail == nullptr)
        {
            tail = new Node(nullptr,value_a);
            head = tail;
            m_size++;
            return;
        }
        tail->next = new Node(nullptr,value_a);
        tail = tail->next;
        m_size++;
    }
    void push_front(T value_a)
    {
        if(head == nullptr)
        {
            head = new Node(nullptr,value_a);
            tail = head;
            m_size++;
            return;
        }
        head = new Node(head,value_a);
        m_size++;
    }
    void clear()
    {
        Node *buffer;
        for(int i = 0;i<m_size;i++)
        {
            buffer = head;
            head = head->next;
            delete buffer;
        }
        m_size = 0;
    }
    void remove(unsigned int index)
    {
        if(index >= m_size)
            throw indexTooLarge;
        Node *currectNode = head;
        for(int i = 0; i < index-1;i++)
            currectNode = currectNode->next;
        Node* buffer = currectNode->next;
        currectNode->next = currectNode->next->next;
        delete buffer;
        m_size--;
    }
    void remove(unsigned int index,unsigned int lenght)
    {
        if(index+lenght >= m_size)
            throw indexTooLarge;
        Node *currectNode = head;
        for(int i = 0; i < index-1; i++)
            currectNode = currectNode->next;
        Node* buffer = currectNode;
        currectNode = currectNode->next;
        for(int i = 0; i < lenght; i++ )
        {
            Node* buffer2 = currectNode;
            currectNode = currectNode->next;
            delete buffer2;
        }
        buffer->next = currectNode;
        m_size -= lenght;
    }
    T& operator[](unsigned const int &index)
    {
        if(index >= m_size)
            throw indexTooLarge;
        if(index == m_size-1)
            return tail->value;
        Node *currectNode = head;
        for(unsigned int i = 0; i < index;i++)
            currectNode = currectNode->next;
        return currectNode->value;
    }
    void operator=(const LincedList &C1)
    {
        head->value = C1.head->value;
        Node *currectNode = new Node(nullptr,C1[1]);
        Node *C1CurrectNode = C1[1];
        head->next = currectNode;
        for(int i = 2; i < m_size; i++)
        {
            C1CurrectNode = C1CurrectNode->next;
            currectNode->next = new Node(nullptr,C1CurrectNode->value);
            currectNode = currectNode->next;
        }
        tail->value = C1.tail->value;
    }
    unsigned int size()
    {
        return m_size;
    }
private:
    struct Node
    {
         Node* next;
         T value;
         Node(Node* next_a, T value_a) : next(next_a) , value(value_a)
         {}
    };
    unsigned int m_size = 0;
    Node* head = nullptr;
    Node* tail = nullptr;
}; 

#include <iostream>
int main()
{
    LincedList<int> ll(0,1,2,3);
    std::cout << ll[1] << std::endl; //writes to console 1
    ll.remove(1); //removes the element at index 1
    std::cout << ll[1] << std::endl; //writes to console 2
    ll.push_back(4);//adds to the end 4
    ll.push_front(5);//adds 5 to the beginning
    std::cout << ll.size() << std::endl; //writes to console 5
    ll.remove(1,2); //remove all 2 to 3 elements
    std::cout << ll[1] << std::endl ; //writes to console 3
    return  0;
}
edited tags
Link
qela
  • 125
  • 1
  • 8
test cases
Source Link
qela
  • 125
  • 1
  • 8
#define indexTooLarge -1
template <typename T>
class LincedList
{
public:
    LincedList(){}
    template<typename... Types>
    LincedList(T value,Types... values) :  LincedList(values...)
    {
        push_front(value);
    }
    LincedList(T value)
    {
        push_front(value);
    }
    ~LincedList()
    {
        clear();
    }
    void push_back(T value_a)
    {
        if(tail == nullptr)
        {
            tail = new Node(nullptr,value_a);
            head = tail;
            m_size++;
            return;
        }
        tail->next = new Node(nullptr,value_a);
        tail = tail->next;
        m_size++;
    }
    void push_front(T value_a)
    {
        if(head == nullptr)
        {
            head = new Node(nullptr,value_a);
            tail = head;
            m_size++;
            return;
        }
        head = new Node(head,value_a);
        m_size++;
    }
    void clear()
    {
        Node *buffer;
        for(int i = 0;i<m_size;i++)
        {
            buffer = head;
            head = head->next;
            delete buffer;
        }
        m_size = 0;
    }
    void remove(unsigned int index)
    {
        if(index >= m_size)
            throw indexTooLarge;
        Node *currectNode = head;
        for(int i = 0; i < index-1;i++)
            currectNode = currectNode->next;
        Node* buffer = currectNode->next;
        currectNode->next = currectNode->next->next;
        delete buffer;
        m_size--;
    }
    void remove(unsigned int index,unsigned int lenght)
    {
        if(index+lenght >= m_size)
            throw indexTooLarge;
        Node *currectNode = head;
        for(int i = 0; i < index-1; i++)
            currectNode = currectNode->next;
        Node* buffer = currectNode;
        currectNode = currectNode->next;
        for(int i = 0; i < lenght; i++ )
        {
            Node* buffer2 = currectNode;
            currectNode = currectNode->next;
            delete buffer2;
        }
        buffer->next = currectNode;
        m_size -= lenght;
    }
    T& operator[](unsigned const int &index)
    {
        if(index >= m_size)
            throw indexTooLarge;
        if(index == m_size-1)
            return tail->value;
        Node *currectNode = head;
        for(unsigned int i = 0; i < index;i++)
            currectNode = currectNode->next;
        return currectNode->value;
    }
    void operator=(const LincedList &C1)
    {
        head->value = C1.head->value;
        Node *currectNode = new Node(nullptr,C1[1]);
        Node *C1CurrectNode = C1[1];
        head->next = currectNode;
        for(int i = 2; i < m_size; i++)
        {
            C1CurrectNode = C1CurrectNode->next;
            currectNode->next = new Node(nullptr,C1CurrectNode->value);
            currectNode = currectNode->next;
        }
        tail->value = C1.tail->value;
    }
    unsigned int size()
    {
        return m_size;
    }
private:
    struct Node
    {
         Node* next;
         T value;
         Node(Node* next_a, T value_a) : next(next_a) , value(value_a)
         {}
    };
    unsigned int m_size = 0;
    Node* head = nullptr;
    Node* tail = nullptr;
};  

#include <iostream>
int main()
{
    LincedList<int> ll(0,1,2,3);
    std::cout << ll[1] << std::endl; //writes to console 1
    ll.remove(1); //removes the element at index 1
    std::cout << ll[1] << std::endl; //writes to console 2
    ll.push_back(4);//adds to the end 4
    ll.push_front(5);//adds 5 to the beginning
    std::cout << ll.size() << std::endl; //writes to console 5
    ll.remove(1,2); //remove all 2 to 3 elements
    std::cout << ll[1] << std::endl ; //writes to console 3
    return  0;
}
```
#define indexTooLarge -1
template <typename T>
class LincedList
{
public:
    LincedList(){}
    template<typename... Types>
    LincedList(T value,Types... values) :  LincedList(values...)
    {
        push_front(value);
    }
    LincedList(T value)
    {
        push_front(value);
    }
    ~LincedList()
    {
        clear();
    }
    void push_back(T value_a)
    {
        if(tail == nullptr)
        {
            tail = new Node(nullptr,value_a);
            head = tail;
            m_size++;
            return;
        }
        tail->next = new Node(nullptr,value_a);
        tail = tail->next;
        m_size++;
    }
    void push_front(T value_a)
    {
        if(head == nullptr)
        {
            head = new Node(nullptr,value_a);
            tail = head;
            m_size++;
            return;
        }
        head = new Node(head,value_a);
        m_size++;
    }
    void clear()
    {
        Node *buffer;
        for(int i = 0;i<m_size;i++)
        {
            buffer = head;
            head = head->next;
            delete buffer;
        }
        m_size = 0;
    }
    void remove(unsigned int index)
    {
        if(index >= m_size)
            throw indexTooLarge;
        Node *currectNode = head;
        for(int i = 0; i < index-1;i++)
            currectNode = currectNode->next;
        Node* buffer = currectNode->next;
        currectNode->next = currectNode->next->next;
        delete buffer;
        m_size--;
    }
    void remove(unsigned int index,unsigned int lenght)
    {
        if(index+lenght >= m_size)
            throw indexTooLarge;
        Node *currectNode = head;
        for(int i = 0; i < index-1; i++)
            currectNode = currectNode->next;
        Node* buffer = currectNode;
        currectNode = currectNode->next;
        for(int i = 0; i < lenght; i++ )
        {
            Node* buffer2 = currectNode;
            currectNode = currectNode->next;
            delete buffer2;
        }
        buffer->next = currectNode;
        m_size -= lenght;
    }
    T& operator[](unsigned const int &index)
    {
        if(index >= m_size)
            throw indexTooLarge;
        if(index == m_size-1)
            return tail->value;
        Node *currectNode = head;
        for(unsigned int i = 0; i < index;i++)
            currectNode = currectNode->next;
        return currectNode->value;
    }
    void operator=(const LincedList &C1)
    {
        head->value = C1.head->value;
        Node *currectNode = new Node(nullptr,C1[1]);
        Node *C1CurrectNode = C1[1];
        head->next = currectNode;
        for(int i = 2; i < m_size; i++)
        {
            C1CurrectNode = C1CurrectNode->next;
            currectNode->next = new Node(nullptr,C1CurrectNode->value);
            currectNode = currectNode->next;
        }
        tail->value = C1.tail->value;
    }
    unsigned int size()
    {
        return m_size;
    }
private:
    struct Node
    {
         Node* next;
         T value;
         Node(Node* next_a, T value_a) : next(next_a) , value(value_a)
         {}
    };
    unsigned int m_size = 0;
    Node* head = nullptr;
    Node* tail = nullptr;
}; 
#define indexTooLarge -1
template <typename T>
class LincedList
{
public:
    LincedList(){}
    template<typename... Types>
    LincedList(T value,Types... values) :  LincedList(values...)
    {
        push_front(value);
    }
    LincedList(T value)
    {
        push_front(value);
    }
    ~LincedList()
    {
        clear();
    }
    void push_back(T value_a)
    {
        if(tail == nullptr)
        {
            tail = new Node(nullptr,value_a);
            head = tail;
            m_size++;
            return;
        }
        tail->next = new Node(nullptr,value_a);
        tail = tail->next;
        m_size++;
    }
    void push_front(T value_a)
    {
        if(head == nullptr)
        {
            head = new Node(nullptr,value_a);
            tail = head;
            m_size++;
            return;
        }
        head = new Node(head,value_a);
        m_size++;
    }
    void clear()
    {
        Node *buffer;
        for(int i = 0;i<m_size;i++)
        {
            buffer = head;
            head = head->next;
            delete buffer;
        }
        m_size = 0;
    }
    void remove(unsigned int index)
    {
        if(index >= m_size)
            throw indexTooLarge;
        Node *currectNode = head;
        for(int i = 0; i < index-1;i++)
            currectNode = currectNode->next;
        Node* buffer = currectNode->next;
        currectNode->next = currectNode->next->next;
        delete buffer;
        m_size--;
    }
    void remove(unsigned int index,unsigned int lenght)
    {
        if(index+lenght >= m_size)
            throw indexTooLarge;
        Node *currectNode = head;
        for(int i = 0; i < index-1; i++)
            currectNode = currectNode->next;
        Node* buffer = currectNode;
        currectNode = currectNode->next;
        for(int i = 0; i < lenght; i++ )
        {
            Node* buffer2 = currectNode;
            currectNode = currectNode->next;
            delete buffer2;
        }
        buffer->next = currectNode;
        m_size -= lenght;
    }
    T& operator[](unsigned const int &index)
    {
        if(index >= m_size)
            throw indexTooLarge;
        if(index == m_size-1)
            return tail->value;
        Node *currectNode = head;
        for(unsigned int i = 0; i < index;i++)
            currectNode = currectNode->next;
        return currectNode->value;
    }
    void operator=(const LincedList &C1)
    {
        head->value = C1.head->value;
        Node *currectNode = new Node(nullptr,C1[1]);
        Node *C1CurrectNode = C1[1];
        head->next = currectNode;
        for(int i = 2; i < m_size; i++)
        {
            C1CurrectNode = C1CurrectNode->next;
            currectNode->next = new Node(nullptr,C1CurrectNode->value);
            currectNode = currectNode->next;
        }
        tail->value = C1.tail->value;
    }
    unsigned int size()
    {
        return m_size;
    }
private:
    struct Node
    {
         Node* next;
         T value;
         Node(Node* next_a, T value_a) : next(next_a) , value(value_a)
         {}
    };
    unsigned int m_size = 0;
    Node* head = nullptr;
    Node* tail = nullptr;
};  

#include <iostream>
int main()
{
    LincedList<int> ll(0,1,2,3);
    std::cout << ll[1] << std::endl; //writes to console 1
    ll.remove(1); //removes the element at index 1
    std::cout << ll[1] << std::endl; //writes to console 2
    ll.push_back(4);//adds to the end 4
    ll.push_front(5);//adds 5 to the beginning
    std::cout << ll.size() << std::endl; //writes to console 5
    ll.remove(1,2); //remove all 2 to 3 elements
    std::cout << ll[1] << std::endl ; //writes to console 3
    return  0;
}
```
Source Link
qela
  • 125
  • 1
  • 8
Loading