Delete first node in linked list in C

In this method, first node in the linked list with specified key [value] is deleted. For example - if the given List is 10->20->30->10->20 and the first occurrence of 20 is deleted, the Linked List becomes 10->30->10->20.

First, the head of the linked list is checked for null value. If the head is not null and the value stored in it is equal to the key, make head next as head and delete previous head. Else, traverse to the node previous to the node with value equal to key, and adjust links.

The function pop_first is created for this purpose. It is a 3-step process.

void pop_first[int key] { Node* temp = head; //1. check if the head is not null if[temp != NULL] { //2. if head is not null and value stored at head // is equal to the key, make head next as head // and delete previous head if[temp->data == key] { Node* nodeToDelete = head; head = head->next; free[nodeToDelete]; } else { //3. Else, traverse to the node previous to the // node with value equal to key, and adjust links while[temp->next != NULL] { if[temp->next->data == key] { Node* nodeToDelete = temp->next; temp->next = temp->next->next; free[nodeToDelete]; break; } temp = temp->next; } } } }

void pop_first[struct Node** head_ref, int key] { struct Node* temp = *head_ref; //1. check if the head is not null if[temp != NULL] { //2. if head is not null and value stored at head // is equal to the key, make head next as head // and delete previous head if[temp->data == key] { struct Node* nodeToDelete = *head_ref; *head_ref = [*head_ref]->next; free[nodeToDelete]; } else { //3. Else, traverse to the node previous to the // node with value equal to key, and adjust links while[temp->next != NULL] { if[temp->next->data == key] { struct Node* nodeToDelete = temp->next; temp->next = temp->next->next; free[nodeToDelete]; break; } temp = temp->next; } } } }

def pop_first[self, key]: temp = self.head #1. check if the head is not null if[temp != None]: #2. if head is not null and value stored at head # is equal to the key, make head next as head # and delete previous head if[temp.data == key]: nodeToDelete = self.head self.head = self.head.next nodeToDelete = None else: #3. Else, traverse to the node previous to the # node with value equal to key, and adjust links while[temp.next != None]: if[temp.next.data == key]: nodeToDelete = temp.next temp.next = temp.next.next nodeToDelete = None break temp = temp.next

void pop_first[int key] { Node temp = head; //1. check if the head is not null if[temp != null] { //2. if head is not null and value stored at head // is equal to the key, make head next as head // and delete previous head if[temp.data == key] { Node nodeToDelete = head; head = head.next; nodeToDelete = null; } else { //3. Else, traverse to the node previous to the // node with value equal to key, and adjust links while[temp.next != null] { if[temp.next.data == key] { Node nodeToDelete = temp.next; temp.next = temp.next.next; nodeToDelete = null; break; } temp = temp.next; } } } }

public void pop_first[int key] { Node temp = head; //1. check if the head is not null if[temp != null] { //2. if head is not null and value stored at head // is equal to the key, make head next as head // and delete previous head if[temp.data == key] { Node nodeToDelete = head; head = head.next; nodeToDelete = null; } else { //3. Else, traverse to the node previous to the // node with value equal to key, and adjust links while[temp.next != null] { if[temp.next.data == key] { Node nodeToDelete = temp.next; temp.next = temp.next.next; nodeToDelete = null; break; } temp = temp.next; } } } }

public function pop_first[$key] { $temp = $this->head; //1. check if the head is not null if[$temp != null] { //2. if head is not null and value stored at head // is equal to the key, make head next as head // and delete previous head if[$temp->data == $key] { $nodeToDelete = $this->head; $this->head = $this->head->next; $nodeToDelete = null; } else { //3. Else, traverse to the node previous to the // node with value equal to key, and adjust links while[$temp->next != null] { if[$temp->next->data == $key] { $nodeToDelete = $temp->next; $temp->next = $temp->next->next; $nodeToDelete = null; break; } $temp = $temp->next; } } } }


The below is a complete program that uses above discussed concept to delete first occurrence of the specified key [if exists] of the linked list.

#include #include //node structure struct Node { int data; struct Node* next; }; //Add new element at the end of the list void push_back[struct Node** head_ref, int newElement] { struct Node *newNode, *temp; newNode = [struct Node*]malloc[sizeof[struct Node]]; newNode->data = newElement; newNode->next = NULL; if[*head_ref == NULL] { *head_ref = newNode; } else { temp = *head_ref; while[temp->next != NULL] { temp = temp->next; } temp->next = newNode; } } //Delete first node by key void pop_first[struct Node** head_ref, int key] { struct Node* temp = *head_ref; if[temp != NULL] { if[temp->data == key] { struct Node* nodeToDelete = *head_ref; *head_ref = [*head_ref]->next; free[nodeToDelete]; } else { while[temp->next != NULL] { if[temp->next->data == key] { struct Node* nodeToDelete = temp->next; temp->next = temp->next->next; free[nodeToDelete]; break; } temp = temp->next; } } } } //display the content of the list void PrintList[struct Node* head_ref] { struct Node* temp = head_ref; if[head_ref != NULL] { printf["The list contains: "]; while [temp != NULL] { printf["%i ",temp->data]; temp = temp->next; } printf["\n"]; } else { printf["The list is empty.\n"]; } } // test the code int main[] { struct Node* MyList = NULL; //Add five elements at the end of the list. push_back[&MyList, 10]; push_back[&MyList, 20]; push_back[&MyList, 30]; push_back[&MyList, 10]; push_back[&MyList, 20]; PrintList[MyList]; //Delete first occurrence of 20 pop_first[&MyList, 20]; PrintList[MyList]; return 0; }


The above code will give the following output:

The list contains: 10 20 30 10 20 The list contains: 10 30 10 20

In this method, the first node of the linked list is deleted. For example - if the given list is 10->20->30->40 and the first node is deleted, the list becomes 20->30->40.

Deleting the first node of the Linked List is very easy. If the head is not null then create a temp node pointing to head and move head to the next of head. Then delete the temp node.

The function pop_front is created for this purpose. It is a 3-step process.

void pop_front[] { if[head != NULL] { //1. if head is not null, create a // temp node pointing to head Node* temp = head; //2. move head to next of head head = head->next; //3. delete temp node free[temp]; } }


The below is a complete program that uses above discussed concept of deleting the first node of the linked list.

#include using namespace std; //node structure struct Node { int data; Node* next; }; class LinkedList { private: Node* head; public: LinkedList[]{ head = NULL; } //Add new element at the end of the list void push_back[int newElement] { Node* newNode = new Node[]; newNode->data = newElement; newNode->next = NULL; if[head == NULL] { head = newNode; } else { Node* temp = head; while[temp->next != NULL] temp = temp->next; temp->next = newNode; } } //Delete first node of the list void pop_front[] { if[head != NULL] { Node* temp = head; head = head->next; free[temp]; } } //display the content of the list void PrintList[] { Node* temp = head; if[temp != NULL] { cout

Chủ Đề