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

ONLY looking for part B!! a. Using C++, define a node structure of the linked list...
ONLY looking for part B!! a. Using C++, define a node structure of the linked list (e.g. value is an integer, next is a node type pointer), construct a linked list of 10 nodes and assign random numbers as the nodes’ values. Use loop to track and print from the first node to the last and output all nodes’ values. Finally, free all memories of the linked list. b. Based on 2.a, (1) define a function which takes the header...
What is are the correct code implementations to remove a node from a circular doubly linked...
What is are the correct code implementations to remove a node from a circular doubly linked list from the beginning, middle, and end? Using only a dummy node and a pointer in the code implementation for the removal of the last/end node.
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 C++, Create a singly Linked List of patients list so that you can sort and...
Using C++, Create a singly Linked List of patients list so that you can sort and search the list by last 4 digits of the patient's Social Security number. Implement Node insertion, deletion, update and display functionality in the Linked List. Each patient's record or node should have the following data: Patient Name: Age: Last 4 digits of Social Security Number: The program should first display a menu that gives the user the option to: 1) Add a Patient's record...
(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)
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(); } /*...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT