samedi 31 octobre 2020

C++ Linked list using template and overloading [closed]

Hi everyone. I couldn't solve this problem myself. Who can help? I'm stuck with this task whole day. May be for some of you it will be easy, but I spend most of time in my day doing this. I'll appreciate if there's someone who can solve it. A lot of thanks for that person who will do it. This is sinly linked list and I need to do it. After that I need also to do doubly linked, but for now I want at least singly linked list.

        #include<iostream>
        using namespace std;
        
        //<T>
        //Linked List
        template <typename T> class SingleList {
            class Node {
            public: T data; Node* next;
                Node(T data = T(), Node* next = nullptr) {
                    this->data = data;
                    this->next = next;
                }
            };
            int size;
            Node* head;
        public:
            SingleList() {
                size = 0;
                head = nullptr;
            }
            int length() {
                return size;
            }
            void push_front(T data) {//O(1)
                if (head == nullptr)
                    head = new Node(data);
                else {
                    Node* temp = new Node(data, head);
                    head = temp;
                }
                size++;
            }
            void pop_front() {
                if (size <= 0)
                    throw out_of_range("");
                else {
                    Node* temp = head;
                    head = head->next;
                    delete temp;
                }
                size--;
            }
            void clearAll() {
                while (size != 0)
                    pop_front();
                cout << "List is empty, size = " << size << endl;
            }
            T operator [] (int index) {
                T res = T();
                int counter = 0;
                Node* cur = head;
                while (cur != nullptr) {
                    if (counter == index) {
                        res = cur->data;
                        break;
                    }
                    cur = cur->next;
                    counter++;
                }
                return res;
            }
        
            void push_back(T data){
                Node* temp = new Node(data);
                Node* cur = head;
                if (head == nullptr)
                    head = new Node(data);
                else {
                    while (cur != nullptr) {
                        if (cur->next == nullptr) {
                            cur->next = temp;
                            break;
                        }
                        cur = cur->next;
                    }
                }
                size++;
        
            }
            void pop_back(){
                Node* cur = head;
                if (size <= 0)
                    throw out_of_range("");
                else {
                    while (cur->next != nullptr) {
                        cur = cur->next;
                    }
                    delete cur->next;
                    cur->next = NULL;
                }
                size--;
            };
        
            void add(T element, int index){
                int counter = 0;
                Node* cur = head;
                Node* temp = new Node(element);
                while (cur != nullptr) {
                    if (counter == index) {
                        temp->next = cur->next;
                        cur->next = temp;
                        break;
                    }
                    cur = cur->next;
                    counter++;
                }
                size++;
            };
        
            void set(T element, int index){
                int counter = 0;
                Node* cur = head;
                while (cur != nullptr) {
                    if (counter == index) {
                        cur->data = element;
        
                        break;
                    }
                    cur = cur->next;
                    counter++;
                }
            };
        
            void remove(int index){
                int counter = 0;
                Node* cur = head;
                while (cur!= nullptr){
                    if (counter == index){
        
                        cur->next = NULL;
                        delete cur-> next;
                        size--;
                        break;
                    }
                    cur = head->next;
                    counter++;
        
                }
        
        
            };
        
            T operator == (int index){
        
                T res = T();
                int counter = 0;
                Node* cur = head;
                while (cur != nullptr) {
                    if (counter == index) {
                        res = cur->data;
                        break;
                    }
                    cur = cur->next;
                    counter++;
                }
                return res;
            }
            T operator != (int index){
        
            }
            T operator >> (int index){
        
            }
            T operator << (int index){
        
            }
        
            /*~SingleList(){
                clearAll();
            }*/
        };
        
        /*
         TODO List:
         1. methods: push_back(T element), pop_back(), add(T element, int index),
            set(T element, int index), remove(int index)
         2. operators: ==, !=, >>, <<
         */
        
        int main() {
            //1 2 3 4
            //add(5, 2)
            //Output: 1 2 5 3 4
            // set(10, 0)
            //Output: 10 2 3 4
            SingleList<int> sl;
            sl.push_front(4);//4
            sl.push_front(3);//3,4
            sl.push_front(2);//2,3,4
            sl.push_front(1);//1,2,3,4
        //    sl.pop_front();//2,3,4;
        //    sl.add(5,2);// 1 2 3 5 4
        //    sl.pop_back();//1,2,3
        //    sl.set(10,0); //10 2 3 4
            sl.remove(2);
            for (int i = 0; i < sl.length(); i++)
                cout << sl[i] << " ";
            cout << endl;
            return 0;
        }

P.s. Some parts of the task are solved myself. But anyway I stuck doing it.

Aucun commentaire:

Enregistrer un commentaire