C PROGRAMMING
Create two void functions that 1) delete a specific value in a linked list and 2) delete all instances of that value. Both functions' only parameter is the number that will be erased.
Please find the code below.
CODE
/* Given a reference (pointer to pointer) to the head of a list
and a key, deletes the first occurrence of key in linked list */
void deleteNode(int key)
{
// Store head node
struct Node* temp = *head_ref, *prev;
// If head node itself holds the key to be deleted
if (temp != NULL && temp->data == key)
{
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}
// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL && temp->data != key)
{
prev = temp;
temp = temp->next;
}
// If key was not present in linked list
if (temp == NULL) return;
// Unlink the node from linked list
prev->next = temp->next;
free(temp); // Free memory
}
/* Given a reference (pointer to pointer) to the head of a list and
a key, deletes all occurrence of the given key in linked list */
void deleteKey(int key)
{
// Store head node
struct Node* temp = *head_ref, *prev;
// If head node itself holds the key or multiple occurrences of key
while (temp != NULL && temp->data == key)
{
*head_ref = temp->next; // Changed head
free(temp); // free old head
temp = *head_ref; // Change Temp
}
// Delete occurrences other than head
while (temp != NULL)
{
// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL && temp->data != key)
{
prev = temp;
temp = temp->next;
}
// If key was not present in linked list
if (temp == NULL) return;
// Unlink the node from linked list
prev->next = temp->next;
free(temp); // Free memory
//Update Temp for next iteration of outer loop
temp = prev->next;
}
}
NOTE:
I have provided the code assuming that the linked list node is represented as:
// A linked list node
struct Node
{
int data;
struct Node *next;
};
struct Node* head; // points to the head of the linked list
C PROGRAMMING Create two void functions that 1) delete a specific value in a linked list...
Please use C++
CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...
Create a linked list with the following features. A Node class that stores the data type of your choice. A constructor that creates a dummy header node. void display() -- A method for printing the list. void add(item) -- A method for adding a value to the beginning of the list void addEnd(item) -- A method of adding a value to the end of the list. bool contains(item) -- A method for finding a specified value in the list. int...
[C++] Create three functions for a singly linked list: - Function 1: Insert a string into the linked list - Function 1: Insert a node after a given node (Node* curNodeptr, Node* newNodePtr) - Function 2: Delete the node passed to it by a pointer, it will take in the head and curPtr '(Node*, Node*)' struct Node{ string data; Node *next; };
In C program
1. Create a Linked List of type Float. (including the functions specified below) a. Insertion i. at the Beginning, ii. at the End, iii. after the node containing the value (given as parameter) X. b. Deletion i. at the Beginning, ii. at the End, iii. of the node containing the value (given as parameter) X.
write any 2 c functions for a linked list and a doubly-linked sorted list create at least two data structures
1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is given below. struct node { int info; struct node *next; }; typedef struct node node; You can assume that all the nodes in the linked list are distinct and each node appears in the list at most once. Prototype of the functions are given below. node *delete(node *head, int k) node *recursivedelete(node *head, int k) • delete deletes the node with info k from...
implement delete node function in c++ language
I
just need a basic doubly linked list code for the delete node
portion of the code
// Delete node containing word from list if it is present void delNode (DLList list, char *str) (
// Delete node containing word from list if it is present void delNode (DLList list, char *str) (
The question I want answered is in C++. How can I pass a value to a function and create a new linked list out of it? For example I have: void getIntegerInput(List<int> list) { int s; std::cout << "Type a positive integer then click enter to add it to the linked list\n"; std::cout << "Type -1 to stop\nType -2 to delete an item from the list\n"; std::cin >> s; while (s != -1) { if (s == -2) { int...
Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...
in c++ Create a circular, doubly linked list from a file (input.txt), containing a list of names. The number of names is unknown. Prompt the user for the number of nodes to delete and then delete accordingly from the list. Assumption: The number inputted by the user will not exceed the actual number of nodes in the list.