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 the linked list and returns the new linked list. It returns the linked list without modification if k does not appear in the list.
• recursivedelete is a recursive function that deletes the node with info k from the linked list and returns the new linked list. It returns the linked list without modification if k does not appear in the list. Complete the body of functions. If your functions are correctly implemented, output should be as follows.
Original Lists
List: 15 21 49 92 86 35 93 15 77 86 83 8 List: 15 21 49 92 86 35 93 15 77 86 83 8
After Deleting 8
List: 15 21 49 92 86 35 93 15 77 86 83 List: 15 21 49 92 86 35 93 15 77 86 83
After Deleting 15
List: 21 49 92 86 35 93 15 77 86 83 List: 21 49 92 86 35 93 15 77 86 83
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node *next;
};
typedef struct node node;
// these two functions are for testing
node *insert(node *head, int d); // to insert elements
void print(node *head); // to print list
node *delete(node *head, int k);
node *recursivedelete(node *head, int k);
int main()
{
// building a test list
node *head = NULL;
head = insert(head, 8);
head = insert(head, 83);
head = insert(head, 86);
head = insert(head, 77);
head = insert(head, 15);
head = insert(head, 93);
head = insert(head, 35);
head = insert(head, 86);
head = insert(head, 92);
head = insert(head, 49);
head = insert(head, 21);
head = insert(head, 15);
// printing
printf("Original List\n");
print(head);
// deleting 8 iteratively
head = delete(head, 8);
// printing list after deleting 8
printf("\nAfter Deleting 8\n");
print(head);
// deleting 15 recursively
head = recursivedelete(head, 15);
printf("\nAfter Deleting 15\n");
print(head);
return 0;
}
node *insert(node *head, int d)
{
node *new_node = (node*)malloc(sizeof(node));
new_node->info = d;
new_node->next = head;
head = new_node;
return head;
}
void print(node *head)
{
while (head != NULL)
{
printf("%d ", head->info);
head = head->next;
}
printf("\n");
}
node *delete(node *head, int k)
{
node *temp = head;
node *prev;
// if data to be deleted is first element
if (temp != NULL && temp->info == k)
{
head = temp->next;
free(temp);
return head;
}
// else search for data
while (temp != NULL && temp->info !=
k)
{
prev = temp;
temp = temp->next;
}
// if data to be deleted was not present
if (temp == NULL)
{
return head;
}
// else change links
prev->next = temp->next;
free(temp);
return head;
}
node *recursivedelete(node *head, int k)
{
if (head == NULL) // if data was not found or list is
empty
{
return NULL;
}
if (head->info == k) // if data found
{
node *temp = head->next;
free(head);
return temp;
}
// else
head->next = recursivedelete(head->next, k);
return head;
}
OUTPUT
1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is...
C programming Write an iterative and recursive version of a function to print out a linked list from head to tail and then do the same for printing a linked list from tail to head. Assume a singly linked list in all cases and access only to a head pointer at the time of the function call. struct node; typedef struct node Node; struct node int data; Node next;
This is a recursive function to delete all even nodes from a linked list. node *deleteEven(node *head) if (head == NULL) return head; if (head->data % 2 == 0) { node *temp = head; free(temp); return deleteEven(head->next); } else { head->next = deleteEven(head->next); return head; } } What is the purpose of heaving to set "head->next = deleteEven(head->next);" instead of just having "head = head->next;" in one of the lines above and just calling "deleteEven(head->next);"?
In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...
Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...
Exercise: Delete First Element from a Linked List (pair) This is a pair exercise to complete with your lab partner. Download list_delete_first.c here, or copy it to your CSE account using the following command: cp -n /web/dp1091/20T1/activities/list_delete_first/list_delete_first.c . Your task is to add code to this function in list_delete_first.c: // // Delete the first node in list. // The deleted node is freed. // The head of the list is returned. // struct node *delete_first(struct node *head) { // PUT...
/*Given the head of a linked list, find and return the kth node of the linked list *Assume head is the first node *If k is larger than the size of the linked list, return NULL * c++ * Example: n1 -> n2 -> n3 -> n4 -> n5, k = 3 * Return &n3 */ Node* findKthNode(Node *head, int k){ return NULL; //STUB: edit with the correct output, according to the lab instructions, using recursion } must use recursive...
Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...
[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; };
need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...