Question

1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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

Add a comment
Know the answer?
Add Answer to:
1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that...

    Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...

  • C programming Write an iterative and recursive version of a function to print out a linked...

    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;

  • Using the provided Linked List template, add the following recursive functions and demonstrate them in a...

    Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...

  • This is a recursive function to delete all even nodes from a linked list. node *deleteEven(node...

    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...

    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...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    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,...

    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...

    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...

    /*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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT