I have a simple implementation of a Linked list class, which has pointers to Node objects which I have also defined.
The function insertHead creates a new Node and inserts it at the head. However, whenever I call the function, the constructor seems to return a pointer to the same object every time. (I checked that using GDB)
I am pasting snippets of the code here. Can someone please let me know if something seems off?
void LinkedList::insertHead(int val){
Node temp(val);
if(head == NULL){
head = tail = &temp;
} else {
temp.setNext(head);
head = &temp;
}
}
Class definitions:
class LinkedList{
Node *head;
Node *tail;
...
class Node{
int val;
Node *next;
...
Node::Node(int x){
val = x;
next = NULL;
}