new in linked list c++ code example
Example 1: how to use a new node c++
struct node(){
int key;
node *next;
node(x){
key = x;
next = NULL;
}}
// after you take a input of a linked list .
// creating a new node.
node *temp = new node(key); // tem pointer pointing towards the new node.
temp -> next = head; // inserting the mew node in the start .
Example 2: linked list class c++ basic implementation
template<typename T>
class list {
public:
struct node {
T value;
node* next;
};
list() : hd(nullptr), tl(nullptr) {}
node* head() { return hd; }
node* tail() { return tl; }
void insert(node* prior, T value);
node* at(int i);
void erase(node* prior);
void clear();
void push_back(T value);
void pop_back();
void push_front(T value);
void pop_front();
int size();
private:
node* hd, tl;
}