I have done a bare bones implementation of a Singly Linked List in C++, which works. Here’s my node class
template <typename T> class ListNode{ template <typename U > friend class SinglyLinkedList; private: T data_; ListNode<T>* next_; public: ListNode(void): data_{0}, next_{nullptr}{} ListNode(T data_): data_{data_}, next_{nullptr} {} };
Here’s the list class,
template <typename T> class SinglyLinkedList{ private: ListNode<T>* head_; std::size_t size_; public: SinglyLinkedList(): head_{nullptr}, size_{0}{} void insert(T item){ ListNode<T> * p{new ListNode<T>(item)}; if (size_ == 0){ head_ = p; }else{ p->next_ = head_; head_ = p; } size_++; } std::size_t getSize(){ return size_; } };
Here’s what I’m looking to learn and understand,
-
A review.
-
Where is the new’ed up Node in the insert deleted(released)?
-
Is there a way to implement this without new at all?
-
Is there a difference between the following lines?
ListNode(void): data_{0}, next_{nullptr}{}
and ListNode(void): data_(0), next_(nullptr){}