CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
class Node
{
public:
Node(int d) : m_data(d), m_next(nullptr) {}
~Node() { cout << m_data << " is destroyed\n"; }
int getValue() { return m_data; }
Node* getNext() { return m_next; }
void setNext(Node* next) { m_next = next; }
private:
int m_data;
Node* m_next;
};
class linklist
{
public:
linklist() : m_head(nullptr), m_tail(nullptr) {}
void addToTail(int d)
{
Node* newNode = new Node(d);
if (m_head == nullptr && m_tail == nullptr)
m_head = m_tail = newNode;
else
{
m_tail->setNext(newNode);
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run