Doubly linked list C++ [on hold]












0














To become more familiar with templates I attempted a doubly linked list implementation in C++. Is this a valid use for a template?



template <class T>
struct LinkedListNode {
LinkedListNode<T>* m_previous;
T m_data;
LinkedListNode<T>* m_next;
};

template <class T>
class LinkedList {
public:
LinkedList() {
m_head = nullptr;
}

LinkedListNode<T>* m_head;

void InsertEnd(T data);
void InsertFront(T data);

};

template<class T>
inline void LinkedList<T>::InsertFront(T data)
{
LinkedListNode<T>* insertedNode = new LinkedListNode<T>();
LinkedListNode<T>* firstNode = m_head;

if (!firstNode) {
insertedNode->m_data = data;
insertedNode->m_next = nullptr;
insertedNode->m_previous = nullptr;
m_head = insertedNode;
}
else {
while (firstNode->m_previous != nullptr) {
firstNode = firstNode->m_previous;
}

insertedNode->m_next = firstNode;
insertedNode->m_previous = nullptr;
insertedNode->m_data = data;

firstNode->m_previous = insertedNode;

m_head = insertedNode;
}

}


template<class T>
inline void LinkedList<T>::InsertEnd(T data)
{
LinkedListNode<T>* insertedNode = new LinkedListNode<T>();
LinkedListNode<T>* lastNode = m_head;

if (!lastNode) {
insertedNode->m_data = data;
insertedNode->m_next = nullptr;
insertedNode->m_previous = nullptr;
m_head = insertedNode;

}
else {
while (lastNode->m_next != nullptr) {
lastNode = lastNode->m_next;
}


lastNode->m_next = insertedNode;

insertedNode->m_previous = lastNode;
insertedNode->m_next = nullptr;
insertedNode->m_data = data;
m_head = insertedNode;
}
}


main.cpp



LinkedList<int>* linkedList = new LinkedList<int>();
linkedList->InsertEnd(2);
linkedList->InsertEnd(5);
linkedList->InsertEnd(9);
linkedList->InsertFront(1);
linkedList->InsertEnd(10);

LinkedList<std::string>* stringList = new LinkedList<std::string>();
stringList->InsertFront("one");
stringList->InsertEnd("two");
stringList->InsertFront("three");


Ordering should be
linkedList
1,2,5,9,10
stringList
three,one,two










share|improve this question









New contributor




jjmcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Edward, Graipher, πάντα ῥεῖ, t3chb0t, Snowhawk 12 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Edward, Graipher, πάντα ῥεῖ, t3chb0t, Snowhawk

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. Specifically InsertEnd is faulty. Have you tested this code?
    – Edward
    yesterday












  • @Edward Hi, thanks for the reply. I have tested the code and it runs fine for me and I have debugged this to ensure the ordering is correct, unless there are other cases that I have missed that produces incorrect results. I am not looking for help in fixing the code, but rather whether it is a correct usage for templates, and/or if anything could be done better.
    – jjmcc
    yesterday






  • 1




    Strengths of a doubly linked list are constant time front, back and random insertion/lookup/removal. The given implementation does neither of those operations in constant time (no lookup/removal at all, no random insertion, front and back insertion in linear time). (Just voicing my expectations when I hear "doubly linked list" - this doesn't mean your code is wrong, just that the implementation didn't match my expectations from the title).
    – hoffmale
    yesterday






  • 2




    It would be useful if you augmented the question with the code you used to test.
    – Edward
    yesterday






  • 1




    @bruglesco I will finish the implementation then. Thank you
    – jjmcc
    yesterday
















0














To become more familiar with templates I attempted a doubly linked list implementation in C++. Is this a valid use for a template?



template <class T>
struct LinkedListNode {
LinkedListNode<T>* m_previous;
T m_data;
LinkedListNode<T>* m_next;
};

template <class T>
class LinkedList {
public:
LinkedList() {
m_head = nullptr;
}

LinkedListNode<T>* m_head;

void InsertEnd(T data);
void InsertFront(T data);

};

template<class T>
inline void LinkedList<T>::InsertFront(T data)
{
LinkedListNode<T>* insertedNode = new LinkedListNode<T>();
LinkedListNode<T>* firstNode = m_head;

if (!firstNode) {
insertedNode->m_data = data;
insertedNode->m_next = nullptr;
insertedNode->m_previous = nullptr;
m_head = insertedNode;
}
else {
while (firstNode->m_previous != nullptr) {
firstNode = firstNode->m_previous;
}

insertedNode->m_next = firstNode;
insertedNode->m_previous = nullptr;
insertedNode->m_data = data;

firstNode->m_previous = insertedNode;

m_head = insertedNode;
}

}


template<class T>
inline void LinkedList<T>::InsertEnd(T data)
{
LinkedListNode<T>* insertedNode = new LinkedListNode<T>();
LinkedListNode<T>* lastNode = m_head;

if (!lastNode) {
insertedNode->m_data = data;
insertedNode->m_next = nullptr;
insertedNode->m_previous = nullptr;
m_head = insertedNode;

}
else {
while (lastNode->m_next != nullptr) {
lastNode = lastNode->m_next;
}


lastNode->m_next = insertedNode;

insertedNode->m_previous = lastNode;
insertedNode->m_next = nullptr;
insertedNode->m_data = data;
m_head = insertedNode;
}
}


main.cpp



LinkedList<int>* linkedList = new LinkedList<int>();
linkedList->InsertEnd(2);
linkedList->InsertEnd(5);
linkedList->InsertEnd(9);
linkedList->InsertFront(1);
linkedList->InsertEnd(10);

LinkedList<std::string>* stringList = new LinkedList<std::string>();
stringList->InsertFront("one");
stringList->InsertEnd("two");
stringList->InsertFront("three");


Ordering should be
linkedList
1,2,5,9,10
stringList
three,one,two










share|improve this question









New contributor




jjmcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Edward, Graipher, πάντα ῥεῖ, t3chb0t, Snowhawk 12 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Edward, Graipher, πάντα ῥεῖ, t3chb0t, Snowhawk

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. Specifically InsertEnd is faulty. Have you tested this code?
    – Edward
    yesterday












  • @Edward Hi, thanks for the reply. I have tested the code and it runs fine for me and I have debugged this to ensure the ordering is correct, unless there are other cases that I have missed that produces incorrect results. I am not looking for help in fixing the code, but rather whether it is a correct usage for templates, and/or if anything could be done better.
    – jjmcc
    yesterday






  • 1




    Strengths of a doubly linked list are constant time front, back and random insertion/lookup/removal. The given implementation does neither of those operations in constant time (no lookup/removal at all, no random insertion, front and back insertion in linear time). (Just voicing my expectations when I hear "doubly linked list" - this doesn't mean your code is wrong, just that the implementation didn't match my expectations from the title).
    – hoffmale
    yesterday






  • 2




    It would be useful if you augmented the question with the code you used to test.
    – Edward
    yesterday






  • 1




    @bruglesco I will finish the implementation then. Thank you
    – jjmcc
    yesterday














0












0








0


1





To become more familiar with templates I attempted a doubly linked list implementation in C++. Is this a valid use for a template?



template <class T>
struct LinkedListNode {
LinkedListNode<T>* m_previous;
T m_data;
LinkedListNode<T>* m_next;
};

template <class T>
class LinkedList {
public:
LinkedList() {
m_head = nullptr;
}

LinkedListNode<T>* m_head;

void InsertEnd(T data);
void InsertFront(T data);

};

template<class T>
inline void LinkedList<T>::InsertFront(T data)
{
LinkedListNode<T>* insertedNode = new LinkedListNode<T>();
LinkedListNode<T>* firstNode = m_head;

if (!firstNode) {
insertedNode->m_data = data;
insertedNode->m_next = nullptr;
insertedNode->m_previous = nullptr;
m_head = insertedNode;
}
else {
while (firstNode->m_previous != nullptr) {
firstNode = firstNode->m_previous;
}

insertedNode->m_next = firstNode;
insertedNode->m_previous = nullptr;
insertedNode->m_data = data;

firstNode->m_previous = insertedNode;

m_head = insertedNode;
}

}


template<class T>
inline void LinkedList<T>::InsertEnd(T data)
{
LinkedListNode<T>* insertedNode = new LinkedListNode<T>();
LinkedListNode<T>* lastNode = m_head;

if (!lastNode) {
insertedNode->m_data = data;
insertedNode->m_next = nullptr;
insertedNode->m_previous = nullptr;
m_head = insertedNode;

}
else {
while (lastNode->m_next != nullptr) {
lastNode = lastNode->m_next;
}


lastNode->m_next = insertedNode;

insertedNode->m_previous = lastNode;
insertedNode->m_next = nullptr;
insertedNode->m_data = data;
m_head = insertedNode;
}
}


main.cpp



LinkedList<int>* linkedList = new LinkedList<int>();
linkedList->InsertEnd(2);
linkedList->InsertEnd(5);
linkedList->InsertEnd(9);
linkedList->InsertFront(1);
linkedList->InsertEnd(10);

LinkedList<std::string>* stringList = new LinkedList<std::string>();
stringList->InsertFront("one");
stringList->InsertEnd("two");
stringList->InsertFront("three");


Ordering should be
linkedList
1,2,5,9,10
stringList
three,one,two










share|improve this question









New contributor




jjmcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











To become more familiar with templates I attempted a doubly linked list implementation in C++. Is this a valid use for a template?



template <class T>
struct LinkedListNode {
LinkedListNode<T>* m_previous;
T m_data;
LinkedListNode<T>* m_next;
};

template <class T>
class LinkedList {
public:
LinkedList() {
m_head = nullptr;
}

LinkedListNode<T>* m_head;

void InsertEnd(T data);
void InsertFront(T data);

};

template<class T>
inline void LinkedList<T>::InsertFront(T data)
{
LinkedListNode<T>* insertedNode = new LinkedListNode<T>();
LinkedListNode<T>* firstNode = m_head;

if (!firstNode) {
insertedNode->m_data = data;
insertedNode->m_next = nullptr;
insertedNode->m_previous = nullptr;
m_head = insertedNode;
}
else {
while (firstNode->m_previous != nullptr) {
firstNode = firstNode->m_previous;
}

insertedNode->m_next = firstNode;
insertedNode->m_previous = nullptr;
insertedNode->m_data = data;

firstNode->m_previous = insertedNode;

m_head = insertedNode;
}

}


template<class T>
inline void LinkedList<T>::InsertEnd(T data)
{
LinkedListNode<T>* insertedNode = new LinkedListNode<T>();
LinkedListNode<T>* lastNode = m_head;

if (!lastNode) {
insertedNode->m_data = data;
insertedNode->m_next = nullptr;
insertedNode->m_previous = nullptr;
m_head = insertedNode;

}
else {
while (lastNode->m_next != nullptr) {
lastNode = lastNode->m_next;
}


lastNode->m_next = insertedNode;

insertedNode->m_previous = lastNode;
insertedNode->m_next = nullptr;
insertedNode->m_data = data;
m_head = insertedNode;
}
}


main.cpp



LinkedList<int>* linkedList = new LinkedList<int>();
linkedList->InsertEnd(2);
linkedList->InsertEnd(5);
linkedList->InsertEnd(9);
linkedList->InsertFront(1);
linkedList->InsertEnd(10);

LinkedList<std::string>* stringList = new LinkedList<std::string>();
stringList->InsertFront("one");
stringList->InsertEnd("two");
stringList->InsertFront("three");


Ordering should be
linkedList
1,2,5,9,10
stringList
three,one,two







c++ linked-list






share|improve this question









New contributor




jjmcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




jjmcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday





















New contributor




jjmcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









jjmcc

112




112




New contributor




jjmcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





jjmcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






jjmcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by Edward, Graipher, πάντα ῥεῖ, t3chb0t, Snowhawk 12 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Edward, Graipher, πάντα ῥεῖ, t3chb0t, Snowhawk

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by Edward, Graipher, πάντα ῥεῖ, t3chb0t, Snowhawk 12 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Edward, Graipher, πάντα ῥεῖ, t3chb0t, Snowhawk

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. Specifically InsertEnd is faulty. Have you tested this code?
    – Edward
    yesterday












  • @Edward Hi, thanks for the reply. I have tested the code and it runs fine for me and I have debugged this to ensure the ordering is correct, unless there are other cases that I have missed that produces incorrect results. I am not looking for help in fixing the code, but rather whether it is a correct usage for templates, and/or if anything could be done better.
    – jjmcc
    yesterday






  • 1




    Strengths of a doubly linked list are constant time front, back and random insertion/lookup/removal. The given implementation does neither of those operations in constant time (no lookup/removal at all, no random insertion, front and back insertion in linear time). (Just voicing my expectations when I hear "doubly linked list" - this doesn't mean your code is wrong, just that the implementation didn't match my expectations from the title).
    – hoffmale
    yesterday






  • 2




    It would be useful if you augmented the question with the code you used to test.
    – Edward
    yesterday






  • 1




    @bruglesco I will finish the implementation then. Thank you
    – jjmcc
    yesterday














  • 1




    Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. Specifically InsertEnd is faulty. Have you tested this code?
    – Edward
    yesterday












  • @Edward Hi, thanks for the reply. I have tested the code and it runs fine for me and I have debugged this to ensure the ordering is correct, unless there are other cases that I have missed that produces incorrect results. I am not looking for help in fixing the code, but rather whether it is a correct usage for templates, and/or if anything could be done better.
    – jjmcc
    yesterday






  • 1




    Strengths of a doubly linked list are constant time front, back and random insertion/lookup/removal. The given implementation does neither of those operations in constant time (no lookup/removal at all, no random insertion, front and back insertion in linear time). (Just voicing my expectations when I hear "doubly linked list" - this doesn't mean your code is wrong, just that the implementation didn't match my expectations from the title).
    – hoffmale
    yesterday






  • 2




    It would be useful if you augmented the question with the code you used to test.
    – Edward
    yesterday






  • 1




    @bruglesco I will finish the implementation then. Thank you
    – jjmcc
    yesterday








1




1




Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. Specifically InsertEnd is faulty. Have you tested this code?
– Edward
yesterday






Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. Specifically InsertEnd is faulty. Have you tested this code?
– Edward
yesterday














@Edward Hi, thanks for the reply. I have tested the code and it runs fine for me and I have debugged this to ensure the ordering is correct, unless there are other cases that I have missed that produces incorrect results. I am not looking for help in fixing the code, but rather whether it is a correct usage for templates, and/or if anything could be done better.
– jjmcc
yesterday




@Edward Hi, thanks for the reply. I have tested the code and it runs fine for me and I have debugged this to ensure the ordering is correct, unless there are other cases that I have missed that produces incorrect results. I am not looking for help in fixing the code, but rather whether it is a correct usage for templates, and/or if anything could be done better.
– jjmcc
yesterday




1




1




Strengths of a doubly linked list are constant time front, back and random insertion/lookup/removal. The given implementation does neither of those operations in constant time (no lookup/removal at all, no random insertion, front and back insertion in linear time). (Just voicing my expectations when I hear "doubly linked list" - this doesn't mean your code is wrong, just that the implementation didn't match my expectations from the title).
– hoffmale
yesterday




Strengths of a doubly linked list are constant time front, back and random insertion/lookup/removal. The given implementation does neither of those operations in constant time (no lookup/removal at all, no random insertion, front and back insertion in linear time). (Just voicing my expectations when I hear "doubly linked list" - this doesn't mean your code is wrong, just that the implementation didn't match my expectations from the title).
– hoffmale
yesterday




2




2




It would be useful if you augmented the question with the code you used to test.
– Edward
yesterday




It would be useful if you augmented the question with the code you used to test.
– Edward
yesterday




1




1




@bruglesco I will finish the implementation then. Thank you
– jjmcc
yesterday




@bruglesco I will finish the implementation then. Thank you
– jjmcc
yesterday










2 Answers
2






active

oldest

votes


















1














The biggest thing I can say about your code is don't use raw pointers, start getting into the habit of using smart pointers.




Smart pointers enable automatic, exception-safe, object lifetime management.







share|improve this answer





























    1














    InsertFront:



    m_head should always point to the first node. So while (firstNode->m_previous != nullptr) loop looks unnecessary to me. If you're concerned about having m_head changed by the caller then i would make m_head private and provide getter method for that. Than you can merge if/else blocks as they are very similar.



    InsertEnd:



    Instead of iterating to find the last node every time (which makes it O(n)), you may maintain another pointer which always points to the last node. After that you may merge if/else blocks.



    AFAIK following style of initializing class variables are more preferred.



    LinkedList(): m_head(nullptr) {
    }





    share|improve this answer




























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      The biggest thing I can say about your code is don't use raw pointers, start getting into the habit of using smart pointers.




      Smart pointers enable automatic, exception-safe, object lifetime management.







      share|improve this answer


























        1














        The biggest thing I can say about your code is don't use raw pointers, start getting into the habit of using smart pointers.




        Smart pointers enable automatic, exception-safe, object lifetime management.







        share|improve this answer
























          1












          1








          1






          The biggest thing I can say about your code is don't use raw pointers, start getting into the habit of using smart pointers.




          Smart pointers enable automatic, exception-safe, object lifetime management.







          share|improve this answer












          The biggest thing I can say about your code is don't use raw pointers, start getting into the habit of using smart pointers.




          Smart pointers enable automatic, exception-safe, object lifetime management.








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          tinstaafl

          6,4501827




          6,4501827

























              1














              InsertFront:



              m_head should always point to the first node. So while (firstNode->m_previous != nullptr) loop looks unnecessary to me. If you're concerned about having m_head changed by the caller then i would make m_head private and provide getter method for that. Than you can merge if/else blocks as they are very similar.



              InsertEnd:



              Instead of iterating to find the last node every time (which makes it O(n)), you may maintain another pointer which always points to the last node. After that you may merge if/else blocks.



              AFAIK following style of initializing class variables are more preferred.



              LinkedList(): m_head(nullptr) {
              }





              share|improve this answer


























                1














                InsertFront:



                m_head should always point to the first node. So while (firstNode->m_previous != nullptr) loop looks unnecessary to me. If you're concerned about having m_head changed by the caller then i would make m_head private and provide getter method for that. Than you can merge if/else blocks as they are very similar.



                InsertEnd:



                Instead of iterating to find the last node every time (which makes it O(n)), you may maintain another pointer which always points to the last node. After that you may merge if/else blocks.



                AFAIK following style of initializing class variables are more preferred.



                LinkedList(): m_head(nullptr) {
                }





                share|improve this answer
























                  1












                  1








                  1






                  InsertFront:



                  m_head should always point to the first node. So while (firstNode->m_previous != nullptr) loop looks unnecessary to me. If you're concerned about having m_head changed by the caller then i would make m_head private and provide getter method for that. Than you can merge if/else blocks as they are very similar.



                  InsertEnd:



                  Instead of iterating to find the last node every time (which makes it O(n)), you may maintain another pointer which always points to the last node. After that you may merge if/else blocks.



                  AFAIK following style of initializing class variables are more preferred.



                  LinkedList(): m_head(nullptr) {
                  }





                  share|improve this answer












                  InsertFront:



                  m_head should always point to the first node. So while (firstNode->m_previous != nullptr) loop looks unnecessary to me. If you're concerned about having m_head changed by the caller then i would make m_head private and provide getter method for that. Than you can merge if/else blocks as they are very similar.



                  InsertEnd:



                  Instead of iterating to find the last node every time (which makes it O(n)), you may maintain another pointer which always points to the last node. After that you may merge if/else blocks.



                  AFAIK following style of initializing class variables are more preferred.



                  LinkedList(): m_head(nullptr) {
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  sardok

                  1514




                  1514















                      Popular posts from this blog

                      How to make a Squid Proxy server?

                      Is this a new Fibonacci Identity?

                      19世紀