Question

In: Computer Science

C++ Remove every other node in a circular linked list USING RECURSION. You can only use...

C++ Remove every other node in a circular linked list USING RECURSION. You can only use the preprocessor directives <iostream> and using namespace std;

You must use this prototype:

int remove_every_other(node *&rear)

Solutions

Expert Solution

#include<bits/stdc++.h>
using namespace std;

struct node
{
        int data;
        struct node *next;
};

struct node *addToEmpty(struct node *last, int data)
{
        // This function is only for empty list
        if (last != NULL)
                return last;

        // Creating a node dynamically.
        struct node *temp = (struct node*)malloc(sizeof(struct node));

        // Assigning the data.
        temp -> data = data;
        last = temp;

        // Creating the link.
        last -> next = last;

        return last;
}


struct node *addEnd(struct node *last, int data)
{
        if (last == NULL)
                return addToEmpty(last, data);

        struct node *temp =
            (struct node *)malloc(sizeof(struct node));

        temp -> data = data;
        temp -> next = last -> next;
        last -> next = temp;
        last = temp;

        return last;
}



void traverse(struct node *last)
{
        struct node *p;

        // If list is empty, return.
        if (last == NULL)
        {
                cout << "List is empty." << endl;
                return;
        }

        // Pointing to first node of the list.
        p = last -> next;

        // Traversing the list.
        do
        {
                cout << p -> data << " ";
                p = p -> next;

        }
        while (p != last->next);

}
struct node *last = NULL;
//  Removing every other node
int remove_every_other(node *&rear) {
        if (rear == NULL)
                return -1;

        if(rear->next == last || rear == last)
                return 0;

        rear->next = rear->next->next;

        remove_every_other(rear->next);

}
// Driven Program
int main()
{

        last = addToEmpty(last, 1);
        last = addEnd(last, 2);
        last = addEnd(last, 3);
        last = addEnd(last, 4);
        last = addEnd(last, 5);
        last = addEnd(last, 6);
        last = addEnd(last, 7);
        last = addEnd(last, 8);
        last = addEnd(last, 9);
        last = addEnd(last, 10);
        last = addEnd(last, 11);

        cout<<"Before removing every other node:\n";
        traverse(last);
        cout << endl;

        remove_every_other(last->next);

        cout<<"After removing every other node:\n";
        traverse(last);
        cout << endl;

        return 0;
}

OUTPUT:


Related Solutions

How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_); shared_ptr<node> temp(new node); if(cursor == NULL) { temp = cursor-> next; cursor= temp; if (temp = NULL) { temp->next = NULL; } } else if (cursor-> next != NULL) { temp = cursor->next->next; cursor-> next = temp; if (temp != NULL) { temp->next = cursor; } } }
In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
In C++, write a member method delete() that deletes a node from a linked list at...
In C++, write a member method delete() that deletes a node from a linked list at a random position. (It should first randomly generate that position. and then delete that node).
how to do circular linked list in c++ (inset and delet and display)
how to do circular linked list in c++ (inset and delet and display)
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
Consider a “Remove” operation from a doubly linked list other than front and end. Explain the...
Consider a “Remove” operation from a doubly linked list other than front and end. Explain the implementation of “Remove” operation using a drawing by showing appropriate links. Using the explanation of (a) write the statements to implement the “Remove” operation. Using (b) show that the complexity of “Remove” operation is O(1).
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT