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; } } }
One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only),...
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only), the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined. p1=23x 9 + 18x 7+3 1. Class Node ● Private member variables: coefficient (double), exponents (integer), and next pointer. ● Setter and getter functions to set and get all member variables ● constructor 2. Class PolynomialLinkedList ● Private member variable to represent linked list (head)...
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)
C++ Write a C++ program that implements a tree using a linked representation Each node will...
C++ Write a C++ program that implements a tree using a linked representation Each node will contain a single integer data element. Initialize the tree to contain 10 nodes. The program should allow for the insertion and deletion of data. The program should allow the user to output data in Preorder, Inorder and Postorder.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT