I am writing a code which will store elements in linked list, however when i'm adding an element at the end of the list the output is always like this...
enter image description here i've already search from many tutorials and copying their code to see if it works on mine but it doesn't..
#include <iostream>
#define tab '\t'
using std::endl;
struct node {
int data;
node* next;
};
class Linkedlist {
private:
node* head = new node;
public:
Linkedlist(){
head->next = NULL;
}
void addStart(int val){
node* temp = new node;
temp->data = val;
temp->next = head;
head = temp;
}
void addEnd(int val){
node* temp = new node;
temp->data = val;
temp->next = NULL;
node* curr = new node;
curr = head;
while(curr->next != NULL){
curr = curr->next;
}
curr->next = temp;
}
void display(){
if (head == NULL){
std::cout << endl << "Linked List is EMPTY.";
} else {
int i = 1;
node* curr = new node;
curr = head;
std::cout << endl << "The linked list contains..." << endl;
while(curr->next != NULL){
std::cout << "Elements " << i << ':'
<< tab << curr->data << endl;
curr = curr->next;
i++;
}
}
}
};
The Output should be.. Element 1: 1 Element 2: 2 Element 3: 3 Element 4: 4