ryan
ryan

Reputation: 645

Removing the head node of my linked list? C++

I have this somewhat unconventionally coded linked list and am trying to remove the head of it.

This is a hashtable, so it's an array of linked list

myStruct *pointer = HashTable[i];

1->2->3->NULL

How can I delete the 1 and make 2 the head of the list? I've tried pointer = pointer->next but when I re-display the table, it still is printing 1->2->3->NULL, not 2->3->NULL.

Upvotes: 1

Views: 16148

Answers (2)

T.Malo
T.Malo

Reputation: 512

I would recommend the image I made for you. I think it is safer than saying something like :

node *temp = head

head =head->next 

delete temp

Because depending on how you handle your pointers, you could run the risk of deleting your whole list. Doing it this way, you will lose the head and make sure you have the rest of the list. But do what you like! man - try things out. enter image description here

Upvotes: 12

Cem Aytekin
Cem Aytekin

Reputation: 109

void changethehead(node*&head) {
    node *tobedeleted = head;
    head = head->next;   // head is the next element
    delete tobedeleted; // delete the old head
}

Upvotes: 0

Related Questions