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.
// 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);
}
}
Get Answers For Free
Most questions answered within 1 hours.