Question

Hi, can you make me rotate method for this class just I need rotate method? this...

Hi, can you make me rotate method for this class just I need rotate method?

this is Circularly Linked Lists code.


public class Node {

   int data;
   Node next;

   public Node(int data) {

       this.data = data;
   }
}

// this is implement class
public class CircularLinkList {
  
   public int size = 0;
   public Node head = null;
   public Node tail = null;
  
   // add to the front of the CLL
  
   public void addToFront(int data) {
      
       Node n = new Node(data);
      
       if(size == 0) {
           head = n;
          
           tail = n;
          
           n.next = head;
       }
      
       else
       {
           Node temp = head;
          
           n.next = temp;
          
           head = n;
           tail.next = head;
      
       }
      
       size++;
   }
  
   public void show() {
      
       Node temp = head;
      
       if(size ==0) {
          
           System.out.println("the List is empty!");
       }
      
       else
       {
           do
           {
               System.out.print(" " + temp.data);
              
               temp = temp.next;
              
           }
           while(temp != head);
              
              
          
  
   }

   }
  

  
   public void removeFront() {
      
       head = head.next;
      
      
       tail.next = head;
      
   }
  
}

// this is a test class

public class CllTest {

   public static void main(String[] args) {


       CircularLinkList c1 = new CircularLinkList();
      
       c1.addToFront(5);
       c1.addToFront(4);
       c1.addToFront(3);
       c1.addToFront(2);
       c1.addToFront(1);
       //c1.removeFront();
  
       c1.show();
      
       System.out.println("\nCLL size is: " +c1.size);
   }

}

// please greate to me rotate class thank you.

Homework Answers

Answer #1

// only rotate method

public void rotate(){
System.out.println("Enter number of nodes you need to rotate");
Scanner scanner=new Scanner(System.in);
//read number of nodes to rotate
int n=scanner.nextInt();
if(n==0)
return;

//no need to iterate
else{
n=n%size;
//example if size is 10 and n=100 we need to iterate through 100 items to
//make shift but by modular division we will get the remainder of how many
//iteartions left after previous complete iteration

//if n is greater than size then we need to iterate so many times to
//make the shift if we do like this no of iterations will decrease
if(n==0)
return;

//no need to iterate
Node currentPosition =head;
for(int i=0;i<n;i++){
//traversing to the node to which we need to rotate
currentPosition=currentPosition.next;
}
//since it is a cll there is no need to change any address just we need to
//make head to that position that finishes the rotation
head=currentPosition;
}


}

//this is the complete program
import java.util.Scanner;
class Node {

int data;
Node next;

public Node(int data) {

this.data = data;
}
}

// this is implement class
class CircularLinkList {
  
public int size = 0;
public Node head = null;
public Node tail = null;
  
// add to the front of the CLL
  
public void addToFront(int data) {
  
Node n = new Node(data);
  
if(size == 0) {
head = n;
  
tail = n;
  
n.next = head;
}
  
else
{
Node temp = head;
  
n.next = temp;
  
head = n;
tail.next = head;
  
}
  
size++;
}
  
public void show() {
  
Node temp = head;
  
if(size ==0) {
  
System.out.println("the List is empty!");
}
  
else
{
do
{
System.out.print(" " + temp.data);
  
temp = temp.next;
  
}
while(temp != head);
  
  
  
  
}

}
  

  
public void removeFront() {
  
head = head.next;
  
  
tail.next = head;
//reduce the size while removing
size--;
  
}
public void rotate(){
System.out.println("Enter number of nodes you need to rotate");
Scanner scanner=new Scanner(System.in);
//read number of nodes to rotate
int n=scanner.nextInt();
if(n==0)
return;
else{
n=n%size;
//example if size is 10 and n=100 we need to itearate through 100 items to
//make shift by modular division we will get the remander of how many we
//need to iterate after the last iteartion

//if n is greater than size then we need to iterate so many times to
//make the shift if we do like this no of iterations will decrease
if(n==0)
return;
Node currentPosition =head;
for(int i=0;i<n;i++){
//traversing to the node to which we need to rotate
currentPosition=currentPosition.next;
}
//since it is a cll there is no need to change any address just we need to
//make head to that position that finishes the rotation
head=currentPosition;
}


}
  
}

// this is a test class

public class Main {

public static void main(String[] args) {


CircularLinkList c1 = new CircularLinkList();
  
c1.addToFront(5);
c1.addToFront(4);
c1.addToFront(3);
c1.addToFront(2);
c1.addToFront(1);
c1.removeFront();
  
c1.show();
//call rotate function
c1.rotate();
c1.show();

  
  
System.out.println("\nCLL size is: " +c1.size);
}

}

Know the answer?
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
Similar Questions
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked list with the contents of the original list reversed. Make sure not to use any methods like addToHead() or addToTail(). In addition, consider any special cases that might arise. What is the big-O complexity of your method in terms of the list size n Supplementary Exercise for Programming (Coding) [Singly Linked Lists] Download and unpack (unzip) the file SinglyLinkedList.rar. Compile and execute the class...
(a)   Write a method in class SLL<T> called public SLL<T> reverse() that produces a new linked...
(a)   Write a method in class SLL<T> called public SLL<T> reverse() that produces a new linked list with the contents of the original list reversed. Make sure not to use any methods like addToHead() or addToTail(). In addition, consider any special cases that might arise. (b)   What is the big-O complexity of your method in terms of the list size n. ****** //************************ SLLNode.java ******************************* //           node in a generic singly linked list class public class SLLNode<T> {     public...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return...
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return make; } } class Node<T>{ public T data; public Node next; public Node(T data){ this.data = data; this.next = null; } } public class StackLinkedList<T>{ //Single field of type Node    //to represent the front of the stack //operation push() /* The push operation is equivalent to the inserting a node at the head of the list. */ public void push(T data){ } //operation...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
This is based on LinkedLists. There is a type mismatch in my first method and I...
This is based on LinkedLists. There is a type mismatch in my first method and I dont know how to get around it. I've commented on the line with the type mismatch public class Node {    public T info; public Node link; public Node(T i, Node l) {    info = i; link = l;    } } class LinkedList> {    protected Node head = null; public LinkedList add(T el) { head = new Node(el, head); return this;...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT