Skip to main content

templated Templated linked list

I have created a templated linked list to take different objects. I was told to put this up for code review as it could use some improvements. To be honest, I am not completely sure when to use the 'new' keyword for all instances. Also, I am not completely confident on my use of the template syntax. I did get it to work though.

The linked list is not completely finished, but it is functional enough to get a review.

templated linked list

I have created a templated linked list to take different objects. I was told to put this up for code review as it could use some improvements. To be honest, I am not completely sure when to use the 'new' keyword for all instances. Also I am not completely confident on my use of the template syntax. I did get it to work though.

The linked list is not completely finished but it is functional enough to get a review.

Templated linked list

I have created a templated linked list to take different objects. I was told to put this up for code review as it could use some improvements. To be honest, I am not completely sure when to use the 'new' keyword for all instances. Also, I am not completely confident on my use of the template syntax. I did get it to work though.

The linked list is not completely finished, but it is functional enough to get a review.

Became Hot Network Question
Source Link

templated linked list

I have created a templated linked list to take different objects. I was told to put this up for code review as it could use some improvements. To be honest, I am not completely sure when to use the 'new' keyword for all instances. Also I am not completely confident on my use of the template syntax. I did get it to work though.

#ifndef TEMPLATEDLINKEDLIST_LINKEDLISTOBJS_H
#define TEMPLATEDLINKEDLIST_LINKEDLISTOBJS_H
using namespace std;
template <typename T> class Node {
public:
    T dataStored;
    Node* next = NULL;
};
int counter = 0;
template <typename T>
class LinkedList {
public:
    Node<T> *head;
    Node<T> *tail;

    LinkedList() {
        head = NULL;
        tail = NULL;
    }

    void AddToFront(T data);
    void display();
    void AddToEnd(T data);
    void AddAtIndex(T data, int index);
    void RemoveFromFront();
    void RemoveFromEnd();
    void RemoveTheFirst(T data);
    void RemoveAllOf(T data);
    bool ElementExists(T data);
    T Find(T data);
    int IndexOf(T data);
    string RetrieveFront();
    string RetrieveEnd();
    string Retrieve(int index);
    int Length();
    ~LinkedList();
};
     template <typename T>
 void LinkedList<T>:: AddToFront(T data) {
         Node<T>* node = new Node<T>;
         node->dataStored = data;
         if(head == NULL) {
             head = tail = node;
         }
         else{
                node->next = head;
                head = node;
         }
         counter++;
     }
template <typename T>
void LinkedList<T>::display() {
         Node<T>* iter;
         iter = head;
         while(iter != NULL) {
             cout<<iter->dataStored<<" ";
             iter = iter->next;
         }
     }
     template <typename T> void LinkedList<T>::AddToEnd(T data) {
         Node<T>* node = new Node<T>;
         node->dataStored = data;
         if(head==nullptr) {
             head = tail = node;
         }
         else {
             tail->next = node;
             tail = node;
         }
         counter++;
     }
    template<typename T> int LinkedList<T>::Length()
     {
         return counter;
     }
template<typename T>
LinkedList<T>::~LinkedList() {
        Node<T>* current = head;
        while (current != NULL) {
            current = current->next;
            delete current;
            head = current;
        }

    }
    template<typename T>
    void LinkedList<T>::AddAtIndex(T data, int index) {
        if(index>counter){
            cout<<"Out of bounds";
        }
        else if(index == 0) {
//            node = head->next;
//            head = node;
            AddToFront(data);
        }
        else if(index == counter) {
//            tail->next = node;
//            tail = node;
              AddToEnd(data);
        }
        else{
            int x = 0;
            Node<T>* node = new Node<T>;
            node->dataStored = data;
            Node<T>* temp = head;
            Node<T>* prev;
            while(x!=index){
                prev = temp;
                temp = temp->next;
                x++;
            }
            prev->next = node;
            node->next = temp;
        }
        counter++;
    }
template<typename T>
void LinkedList<T>::RemoveFromFront() {
    if(head==nullptr){
        cout<<"Empty";
    }
    else {
        Node<T> *node;
        node = head;
        head = head->next;
        delete node;
        counter--;
    }
}
template<typename T>
void LinkedList<T>::RemoveFromEnd() {
    if(head==tail) {
        cout<<"Empty";
    }
    else {
        Node<T> *node;
        node = head;
        Node<T> *temp;
        temp = tail;
        while (node->next != tail) {
            node = node->next;
        }
        tail = node;
        delete temp;
        counter--;
    }
}
//template<typename T>
//void LinkedList<T>::RemoveTheFirst(T data) {
//    Node<T>* node;
//    node = head;
//    if(head->dataStored==data) {
//        RemoveFromFront();
//        counter--;
//    }
//    else{
//        Node<T>* temp;
//        while(node->dataStored!=data || node!=tail) {
//            node = node->next;
//            if(node->data==data) {
//                temp = node;
//                break;
//            }
//        }
//        delete
//    }
//}

#endif //TEMPLATEDLINKEDLIST_LINKEDLISTOBJS_H

And main:

#include <iostream>
#include "LinkedListObjs.h"
using namespace std;
int main() {
//    Node<int>* node1 = new Node<int>;
//    node1->dataStored = 3;
//    cout<<node1->dataStored;
    LinkedList<int>* link = new LinkedList<int>;
    link->AddToFront(2);

    link->AddToFront(2);

    link->AddToFront(3);

    link->AddToEnd(1);

    link->AddAtIndex(5,3);
    link->RemoveFromFront();
//    link->RemoveFromFront();
//    link->RemoveFromFront();
//    link->RemoveFromFront();
//    link->RemoveFromFront();
//    link->RemoveFromFront();
    link->display();
   // link->AddAtIndex(0,1);
    cout<<endl<<link->Length()<<endl;
    delete link;
    return 0;
}

The linked list is not completely finished but it is functional enough to get a review.