SlideShare a Scribd company logo
1 of 12
Download to read offline
SOURCE CODE:
import java.util.Iterator;
public class CircularLinkedList implements Iterable {
Node head , tail;
int size;
CircularLinkedList() {
head = null;
tail = null;
size = 0;
}
public boolean add(E e) {
Node ele = new Node(e);
ele.next = head;
if(head == null)
{
head = ele;
ele.next = head;
tail = head;
}
else
{
tail.next = ele;
tail = ele;
}
size++;
return true;
}
public boolean add(int index, E e){
Node ele = new Node(e);
Node ptr = head;
index = index - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == index)
{
Node tmp = ptr.next;
ptr.next = ele;
ele.next = tmp;
break;
}
ptr = ptr.next;
}
size++ ;
return true;
}
private Node getNode(int index ) {
return null;
}
public E remove(int index) {
Node ret;
if (size == 1 && index == 1)
{
ret = head;
head = null;
tail = null;
size = 0;
return ret.getElement();
}
if (index == 1)
{
ret = head;
head = head.next;
tail.next = head;
size--;
return ret.getElement();
}
if (index == size)
{
Node s = head;
Node t = head;
while (s != tail)
{
t = s;
s = s.next;
}
ret = tail;
tail = head;
tail = t;
size --;
return ret.getElement();
}
Node ptr = head;
index = index - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == index)
{
Node tmp = ptr.next;
tmp = tmp.next;
ptr.next = tmp;
break;
}
ptr = ptr.next;
}
size-- ;
return ptr.getElement();
}
public String toString()
{
Node current = head;
String result = "";
if(size == 0){
return "";
}
if(size == 1) {
return head.getElement().toString();
}
else{
do{
result = result + current.getElement().toString();
result = result + " ==> ";
current = current.next;
} while(current != head);
}
return result;
}
public Iterator iterator() {
return new ListIterator();
}
private class ListIterator implements Iterator{
Node nextItem;
Node prev;
int index;
@SuppressWarnings("unchecked")
public ListIterator(){
nextItem = (Node) head;
index = 0;
}
public boolean hasNext() {
return size != 0;
}
public E next() {
prev = nextItem;
nextItem = nextItem.next;
index = (index + 1) % size;
return prev.getElement();
}
public void remove() {
int target;
if(nextItem == head) {
target = size - 1;
} else{
target = index - 1;
index--;
}
CircularLinkedList.this.remove(target); //calls the above class
}
}
// Solve the problem in the main method
// The answer of n = 13, k = 2 is
// the 11th person in the ring (index 10)
public static void main(String[] args){
CircularLinkedList l = new CircularLinkedList();
int n=13;
int k=2;
for(int i=1;i<=n;i++)
l.add(Integer.valueOf(i));
int j = 13,i=0;
while(j>0)
{
System.out.println(l.toString());
i = i + k;
if(i>j)
i = k;
l.remove(i);
j--;
}
}
}
OUTPUT:
1 ==> 2 ==> 3 ==> 4 ==> 5 ==> 6 ==> 7 ==> 8 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==>
1 ==> 3 ==> 4 ==> 5 ==> 6 ==> 7 ==> 8 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==>
1 ==> 3 ==> 4 ==> 6 ==> 7 ==> 8 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==>
1 ==> 3 ==> 4 ==> 6 ==> 7 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==>
1 ==> 3 ==> 4 ==> 6 ==> 7 ==> 9 ==> 10 ==> 12 ==> 13 ==>
1 ==> 4 ==> 6 ==> 7 ==> 9 ==> 10 ==> 12 ==> 13 ==>
1 ==> 4 ==> 6 ==> 9 ==> 10 ==> 12 ==> 13 ==>
1 ==> 4 ==> 6 ==> 9 ==> 10 ==> 13 ==>
1 ==> 6 ==> 9 ==> 10 ==> 13 ==>
1 ==> 6 ==> 9 ==> 13 ==>
1 ==> 9 ==> 13 ==>
1 ==> 13 ==>
1
Solution
SOURCE CODE:
import java.util.Iterator;
public class CircularLinkedList implements Iterable {
Node head , tail;
int size;
CircularLinkedList() {
head = null;
tail = null;
size = 0;
}
public boolean add(E e) {
Node ele = new Node(e);
ele.next = head;
if(head == null)
{
head = ele;
ele.next = head;
tail = head;
}
else
{
tail.next = ele;
tail = ele;
}
size++;
return true;
}
public boolean add(int index, E e){
Node ele = new Node(e);
Node ptr = head;
index = index - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == index)
{
Node tmp = ptr.next;
ptr.next = ele;
ele.next = tmp;
break;
}
ptr = ptr.next;
}
size++ ;
return true;
}
private Node getNode(int index ) {
return null;
}
public E remove(int index) {
Node ret;
if (size == 1 && index == 1)
{
ret = head;
head = null;
tail = null;
size = 0;
return ret.getElement();
}
if (index == 1)
{
ret = head;
head = head.next;
tail.next = head;
size--;
return ret.getElement();
}
if (index == size)
{
Node s = head;
Node t = head;
while (s != tail)
{
t = s;
s = s.next;
}
ret = tail;
tail = head;
tail = t;
size --;
return ret.getElement();
}
Node ptr = head;
index = index - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == index)
{
Node tmp = ptr.next;
tmp = tmp.next;
ptr.next = tmp;
break;
}
ptr = ptr.next;
}
size-- ;
return ptr.getElement();
}
public String toString()
{
Node current = head;
String result = "";
if(size == 0){
return "";
}
if(size == 1) {
return head.getElement().toString();
}
else{
do{
result = result + current.getElement().toString();
result = result + " ==> ";
current = current.next;
} while(current != head);
}
return result;
}
public Iterator iterator() {
return new ListIterator();
}
private class ListIterator implements Iterator{
Node nextItem;
Node prev;
int index;
@SuppressWarnings("unchecked")
public ListIterator(){
nextItem = (Node) head;
index = 0;
}
public boolean hasNext() {
return size != 0;
}
public E next() {
prev = nextItem;
nextItem = nextItem.next;
index = (index + 1) % size;
return prev.getElement();
}
public void remove() {
int target;
if(nextItem == head) {
target = size - 1;
} else{
target = index - 1;
index--;
}
CircularLinkedList.this.remove(target); //calls the above class
}
}
// Solve the problem in the main method
// The answer of n = 13, k = 2 is
// the 11th person in the ring (index 10)
public static void main(String[] args){
CircularLinkedList l = new CircularLinkedList();
int n=13;
int k=2;
for(int i=1;i<=n;i++)
l.add(Integer.valueOf(i));
int j = 13,i=0;
while(j>0)
{
System.out.println(l.toString());
i = i + k;
if(i>j)
i = k;
l.remove(i);
j--;
}
}
}
OUTPUT:
1 ==> 2 ==> 3 ==> 4 ==> 5 ==> 6 ==> 7 ==> 8 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==>
1 ==> 3 ==> 4 ==> 5 ==> 6 ==> 7 ==> 8 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==>
1 ==> 3 ==> 4 ==> 6 ==> 7 ==> 8 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==>
1 ==> 3 ==> 4 ==> 6 ==> 7 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==>
1 ==> 3 ==> 4 ==> 6 ==> 7 ==> 9 ==> 10 ==> 12 ==> 13 ==>
1 ==> 4 ==> 6 ==> 7 ==> 9 ==> 10 ==> 12 ==> 13 ==>
1 ==> 4 ==> 6 ==> 9 ==> 10 ==> 12 ==> 13 ==>
1 ==> 4 ==> 6 ==> 9 ==> 10 ==> 13 ==>
1 ==> 6 ==> 9 ==> 10 ==> 13 ==>
1 ==> 6 ==> 9 ==> 13 ==>
1 ==> 9 ==> 13 ==>
1 ==> 13 ==>
1

More Related Content

Similar to SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf

i am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdfi am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdfsonunotwani
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfmail931892
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfinfo430661
 
package ex2- public class Exercise2-E- { private static class N.pdf
package ex2- public class Exercise2-E- {        private static class N.pdfpackage ex2- public class Exercise2-E- {        private static class N.pdf
package ex2- public class Exercise2-E- { private static class N.pdfarcellzone
 
Submit1) Java Files2) Doc file with the following contents.pdf
Submit1) Java Files2) Doc file with the following contents.pdfSubmit1) Java Files2) Doc file with the following contents.pdf
Submit1) Java Files2) Doc file with the following contents.pdfakaluza07
 
Works Applications Test - Chinmay Chauhan
Works Applications Test - Chinmay ChauhanWorks Applications Test - Chinmay Chauhan
Works Applications Test - Chinmay ChauhanChinmay Chauhan
 
Link list part 2
Link list part 2Link list part 2
Link list part 2Anaya Zafar
 
using set identitiesSolutionimport java.util.Scanner; c.pdf
using set identitiesSolutionimport java.util.Scanner;  c.pdfusing set identitiesSolutionimport java.util.Scanner;  c.pdf
using set identitiesSolutionimport java.util.Scanner; c.pdfexcellentmobilesabc
 
For this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docxFor this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docxmckellarhastings
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfannaelctronics
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. StreamsDEVTYPE
 
Solve using Java programming language- ----------------------------.pdf
Solve using Java programming language-   ----------------------------.pdfSolve using Java programming language-   ----------------------------.pdf
Solve using Java programming language- ----------------------------.pdfaksahnan
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Codemotion
 
Create a new java class called ListNode. Implement ListNode as a gen.pdf
Create a new java class called ListNode. Implement ListNode as a gen.pdfCreate a new java class called ListNode. Implement ListNode as a gen.pdf
Create a new java class called ListNode. Implement ListNode as a gen.pdfmohamednihalshahru
 
STAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfSTAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfbabitasingh698417
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfalphaagenciesindia
 
public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfaccostinternational
 
Can someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfCan someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfABHISHEKREADYMADESKO
 

Similar to SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf (20)

i am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdfi am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdf
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdf
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
 
package ex2- public class Exercise2-E- { private static class N.pdf
package ex2- public class Exercise2-E- {        private static class N.pdfpackage ex2- public class Exercise2-E- {        private static class N.pdf
package ex2- public class Exercise2-E- { private static class N.pdf
 
Submit1) Java Files2) Doc file with the following contents.pdf
Submit1) Java Files2) Doc file with the following contents.pdfSubmit1) Java Files2) Doc file with the following contents.pdf
Submit1) Java Files2) Doc file with the following contents.pdf
 
Works Applications Test - Chinmay Chauhan
Works Applications Test - Chinmay ChauhanWorks Applications Test - Chinmay Chauhan
Works Applications Test - Chinmay Chauhan
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
 
using set identitiesSolutionimport java.util.Scanner; c.pdf
using set identitiesSolutionimport java.util.Scanner;  c.pdfusing set identitiesSolutionimport java.util.Scanner;  c.pdf
using set identitiesSolutionimport java.util.Scanner; c.pdf
 
For this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docxFor this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docx
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdf
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. Streams
 
Solve using Java programming language- ----------------------------.pdf
Solve using Java programming language-   ----------------------------.pdfSolve using Java programming language-   ----------------------------.pdf
Solve using Java programming language- ----------------------------.pdf
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
 
Create a new java class called ListNode. Implement ListNode as a gen.pdf
Create a new java class called ListNode. Implement ListNode as a gen.pdfCreate a new java class called ListNode. Implement ListNode as a gen.pdf
Create a new java class called ListNode. Implement ListNode as a gen.pdf
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.pdf
 
STAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfSTAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
 
public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
 
Can someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfCan someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdf
 

More from arccreation001

2) In Autocrine signalling, reception of a signal released by the sa.pdf
2) In Autocrine signalling, reception of a signal released by the sa.pdf2) In Autocrine signalling, reception of a signal released by the sa.pdf
2) In Autocrine signalling, reception of a signal released by the sa.pdfarccreation001
 
Volatility Distillation separates out different .pdf
                     Volatility  Distillation separates out different .pdf                     Volatility  Distillation separates out different .pdf
Volatility Distillation separates out different .pdfarccreation001
 
The first one has Van der Waals interactions sinc.pdf
                     The first one has Van der Waals interactions sinc.pdf                     The first one has Van der Waals interactions sinc.pdf
The first one has Van der Waals interactions sinc.pdfarccreation001
 
Supersaturated actually. There is more solvent th.pdf
                     Supersaturated actually. There is more solvent th.pdf                     Supersaturated actually. There is more solvent th.pdf
Supersaturated actually. There is more solvent th.pdfarccreation001
 
In optics, a virtual image is an image in which t.pdf
                     In optics, a virtual image is an image in which t.pdf                     In optics, a virtual image is an image in which t.pdf
In optics, a virtual image is an image in which t.pdfarccreation001
 
1) Political orientation and ideas are the main reasons behind count.pdf
1) Political orientation and ideas are the main reasons behind count.pdf1) Political orientation and ideas are the main reasons behind count.pdf
1) Political orientation and ideas are the main reasons behind count.pdfarccreation001
 
Molarity = moles volume in L Molarity = 0.0342.pdf
                     Molarity = moles  volume in L Molarity = 0.0342.pdf                     Molarity = moles  volume in L Molarity = 0.0342.pdf
Molarity = moles volume in L Molarity = 0.0342.pdfarccreation001
 
The tracheal system in insects and gills in fishes are both helps th.pdf
The tracheal system in insects and gills in fishes are both helps th.pdfThe tracheal system in insects and gills in fishes are both helps th.pdf
The tracheal system in insects and gills in fishes are both helps th.pdfarccreation001
 
The answer is D. ChemoorganoheterotrophCow is a chemoorganohetero.pdf
The answer is D. ChemoorganoheterotrophCow is a chemoorganohetero.pdfThe answer is D. ChemoorganoheterotrophCow is a chemoorganohetero.pdf
The answer is D. ChemoorganoheterotrophCow is a chemoorganohetero.pdfarccreation001
 
The A and B chains of human insulin are cloned in separate plasmids .pdf
The A and B chains of human insulin are cloned in separate plasmids .pdfThe A and B chains of human insulin are cloned in separate plasmids .pdf
The A and B chains of human insulin are cloned in separate plasmids .pdfarccreation001
 
Question 1How many parameters does a default constructor haveAn.pdf
Question 1How many parameters does a default constructor haveAn.pdfQuestion 1How many parameters does a default constructor haveAn.pdf
Question 1How many parameters does a default constructor haveAn.pdfarccreation001
 
Rio de Janeiro is a city located on Brazils south-east coast. It i.pdf
Rio de Janeiro is a city located on Brazils south-east coast. It i.pdfRio de Janeiro is a city located on Brazils south-east coast. It i.pdf
Rio de Janeiro is a city located on Brazils south-east coast. It i.pdfarccreation001
 
Othello.javapackage othello;import core.Game; import userInter.pdf
Othello.javapackage othello;import core.Game; import userInter.pdfOthello.javapackage othello;import core.Game; import userInter.pdf
Othello.javapackage othello;import core.Game; import userInter.pdfarccreation001
 
1.Types of Computer Information SystemsThere are four basic type.pdf
1.Types of Computer Information SystemsThere are four basic type.pdf1.Types of Computer Information SystemsThere are four basic type.pdf
1.Types of Computer Information SystemsThere are four basic type.pdfarccreation001
 
Microbiome of human bodyResearchers currently studying human norm.pdf
Microbiome of human bodyResearchers currently studying human norm.pdfMicrobiome of human bodyResearchers currently studying human norm.pdf
Microbiome of human bodyResearchers currently studying human norm.pdfarccreation001
 
Na2SeO3 --- 2 Na+ + SeO32- 0.08              0.16       0.08 .pdf
Na2SeO3 --- 2 Na+ + SeO32- 0.08              0.16       0.08 .pdfNa2SeO3 --- 2 Na+ + SeO32- 0.08              0.16       0.08 .pdf
Na2SeO3 --- 2 Na+ + SeO32- 0.08              0.16       0.08 .pdfarccreation001
 
It is an important to understand the early stages of reproduction. W.pdf
It is an important to understand the early stages of reproduction. W.pdfIt is an important to understand the early stages of reproduction. W.pdf
It is an important to understand the early stages of reproduction. W.pdfarccreation001
 
Let the normal gene be represented by P, and the two mutations be re.pdf
Let the normal gene be represented by P, and the two mutations be re.pdfLet the normal gene be represented by P, and the two mutations be re.pdf
Let the normal gene be represented by P, and the two mutations be re.pdfarccreation001
 
Imagine you are an Information Systems Security Officer for a medium.pdf
Imagine you are an Information Systems Security Officer for a medium.pdfImagine you are an Information Systems Security Officer for a medium.pdf
Imagine you are an Information Systems Security Officer for a medium.pdfarccreation001
 
Each water molecule has 4 hydrogen bonds holding .pdf
                     Each water molecule has 4 hydrogen bonds holding .pdf                     Each water molecule has 4 hydrogen bonds holding .pdf
Each water molecule has 4 hydrogen bonds holding .pdfarccreation001
 

More from arccreation001 (20)

2) In Autocrine signalling, reception of a signal released by the sa.pdf
2) In Autocrine signalling, reception of a signal released by the sa.pdf2) In Autocrine signalling, reception of a signal released by the sa.pdf
2) In Autocrine signalling, reception of a signal released by the sa.pdf
 
Volatility Distillation separates out different .pdf
                     Volatility  Distillation separates out different .pdf                     Volatility  Distillation separates out different .pdf
Volatility Distillation separates out different .pdf
 
The first one has Van der Waals interactions sinc.pdf
                     The first one has Van der Waals interactions sinc.pdf                     The first one has Van der Waals interactions sinc.pdf
The first one has Van der Waals interactions sinc.pdf
 
Supersaturated actually. There is more solvent th.pdf
                     Supersaturated actually. There is more solvent th.pdf                     Supersaturated actually. There is more solvent th.pdf
Supersaturated actually. There is more solvent th.pdf
 
In optics, a virtual image is an image in which t.pdf
                     In optics, a virtual image is an image in which t.pdf                     In optics, a virtual image is an image in which t.pdf
In optics, a virtual image is an image in which t.pdf
 
1) Political orientation and ideas are the main reasons behind count.pdf
1) Political orientation and ideas are the main reasons behind count.pdf1) Political orientation and ideas are the main reasons behind count.pdf
1) Political orientation and ideas are the main reasons behind count.pdf
 
Molarity = moles volume in L Molarity = 0.0342.pdf
                     Molarity = moles  volume in L Molarity = 0.0342.pdf                     Molarity = moles  volume in L Molarity = 0.0342.pdf
Molarity = moles volume in L Molarity = 0.0342.pdf
 
The tracheal system in insects and gills in fishes are both helps th.pdf
The tracheal system in insects and gills in fishes are both helps th.pdfThe tracheal system in insects and gills in fishes are both helps th.pdf
The tracheal system in insects and gills in fishes are both helps th.pdf
 
The answer is D. ChemoorganoheterotrophCow is a chemoorganohetero.pdf
The answer is D. ChemoorganoheterotrophCow is a chemoorganohetero.pdfThe answer is D. ChemoorganoheterotrophCow is a chemoorganohetero.pdf
The answer is D. ChemoorganoheterotrophCow is a chemoorganohetero.pdf
 
The A and B chains of human insulin are cloned in separate plasmids .pdf
The A and B chains of human insulin are cloned in separate plasmids .pdfThe A and B chains of human insulin are cloned in separate plasmids .pdf
The A and B chains of human insulin are cloned in separate plasmids .pdf
 
Question 1How many parameters does a default constructor haveAn.pdf
Question 1How many parameters does a default constructor haveAn.pdfQuestion 1How many parameters does a default constructor haveAn.pdf
Question 1How many parameters does a default constructor haveAn.pdf
 
Rio de Janeiro is a city located on Brazils south-east coast. It i.pdf
Rio de Janeiro is a city located on Brazils south-east coast. It i.pdfRio de Janeiro is a city located on Brazils south-east coast. It i.pdf
Rio de Janeiro is a city located on Brazils south-east coast. It i.pdf
 
Othello.javapackage othello;import core.Game; import userInter.pdf
Othello.javapackage othello;import core.Game; import userInter.pdfOthello.javapackage othello;import core.Game; import userInter.pdf
Othello.javapackage othello;import core.Game; import userInter.pdf
 
1.Types of Computer Information SystemsThere are four basic type.pdf
1.Types of Computer Information SystemsThere are four basic type.pdf1.Types of Computer Information SystemsThere are four basic type.pdf
1.Types of Computer Information SystemsThere are four basic type.pdf
 
Microbiome of human bodyResearchers currently studying human norm.pdf
Microbiome of human bodyResearchers currently studying human norm.pdfMicrobiome of human bodyResearchers currently studying human norm.pdf
Microbiome of human bodyResearchers currently studying human norm.pdf
 
Na2SeO3 --- 2 Na+ + SeO32- 0.08              0.16       0.08 .pdf
Na2SeO3 --- 2 Na+ + SeO32- 0.08              0.16       0.08 .pdfNa2SeO3 --- 2 Na+ + SeO32- 0.08              0.16       0.08 .pdf
Na2SeO3 --- 2 Na+ + SeO32- 0.08              0.16       0.08 .pdf
 
It is an important to understand the early stages of reproduction. W.pdf
It is an important to understand the early stages of reproduction. W.pdfIt is an important to understand the early stages of reproduction. W.pdf
It is an important to understand the early stages of reproduction. W.pdf
 
Let the normal gene be represented by P, and the two mutations be re.pdf
Let the normal gene be represented by P, and the two mutations be re.pdfLet the normal gene be represented by P, and the two mutations be re.pdf
Let the normal gene be represented by P, and the two mutations be re.pdf
 
Imagine you are an Information Systems Security Officer for a medium.pdf
Imagine you are an Information Systems Security Officer for a medium.pdfImagine you are an Information Systems Security Officer for a medium.pdf
Imagine you are an Information Systems Security Officer for a medium.pdf
 
Each water molecule has 4 hydrogen bonds holding .pdf
                     Each water molecule has 4 hydrogen bonds holding .pdf                     Each water molecule has 4 hydrogen bonds holding .pdf
Each water molecule has 4 hydrogen bonds holding .pdf
 

Recently uploaded

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf

  • 1. SOURCE CODE: import java.util.Iterator; public class CircularLinkedList implements Iterable { Node head , tail; int size; CircularLinkedList() { head = null; tail = null; size = 0; } public boolean add(E e) { Node ele = new Node(e); ele.next = head; if(head == null) { head = ele; ele.next = head; tail = head; } else { tail.next = ele; tail = ele; } size++; return true; } public boolean add(int index, E e){ Node ele = new Node(e); Node ptr = head; index = index - 1 ;
  • 2. for (int i = 1; i < size - 1; i++) { if (i == index) { Node tmp = ptr.next; ptr.next = ele; ele.next = tmp; break; } ptr = ptr.next; } size++ ; return true; } private Node getNode(int index ) { return null; } public E remove(int index) { Node ret; if (size == 1 && index == 1) { ret = head; head = null; tail = null; size = 0; return ret.getElement(); } if (index == 1) { ret = head; head = head.next; tail.next = head; size--; return ret.getElement(); }
  • 3. if (index == size) { Node s = head; Node t = head; while (s != tail) { t = s; s = s.next; } ret = tail; tail = head; tail = t; size --; return ret.getElement(); } Node ptr = head; index = index - 1 ; for (int i = 1; i < size - 1; i++) { if (i == index) { Node tmp = ptr.next; tmp = tmp.next; ptr.next = tmp; break; } ptr = ptr.next; } size-- ; return ptr.getElement(); } public String toString() { Node current = head; String result = ""; if(size == 0){
  • 4. return ""; } if(size == 1) { return head.getElement().toString(); } else{ do{ result = result + current.getElement().toString(); result = result + " ==> "; current = current.next; } while(current != head); } return result; } public Iterator iterator() { return new ListIterator(); } private class ListIterator implements Iterator{ Node nextItem; Node prev; int index; @SuppressWarnings("unchecked") public ListIterator(){ nextItem = (Node) head; index = 0; } public boolean hasNext() { return size != 0; }
  • 5. public E next() { prev = nextItem; nextItem = nextItem.next; index = (index + 1) % size; return prev.getElement(); } public void remove() { int target; if(nextItem == head) { target = size - 1; } else{ target = index - 1; index--; } CircularLinkedList.this.remove(target); //calls the above class } } // Solve the problem in the main method // The answer of n = 13, k = 2 is // the 11th person in the ring (index 10) public static void main(String[] args){ CircularLinkedList l = new CircularLinkedList(); int n=13; int k=2; for(int i=1;i<=n;i++) l.add(Integer.valueOf(i)); int j = 13,i=0; while(j>0) { System.out.println(l.toString());
  • 6. i = i + k; if(i>j) i = k; l.remove(i); j--; } } } OUTPUT: 1 ==> 2 ==> 3 ==> 4 ==> 5 ==> 6 ==> 7 ==> 8 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==> 1 ==> 3 ==> 4 ==> 5 ==> 6 ==> 7 ==> 8 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==> 1 ==> 3 ==> 4 ==> 6 ==> 7 ==> 8 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==> 1 ==> 3 ==> 4 ==> 6 ==> 7 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==> 1 ==> 3 ==> 4 ==> 6 ==> 7 ==> 9 ==> 10 ==> 12 ==> 13 ==> 1 ==> 4 ==> 6 ==> 7 ==> 9 ==> 10 ==> 12 ==> 13 ==> 1 ==> 4 ==> 6 ==> 9 ==> 10 ==> 12 ==> 13 ==> 1 ==> 4 ==> 6 ==> 9 ==> 10 ==> 13 ==> 1 ==> 6 ==> 9 ==> 10 ==> 13 ==> 1 ==> 6 ==> 9 ==> 13 ==> 1 ==> 9 ==> 13 ==> 1 ==> 13 ==> 1 Solution SOURCE CODE: import java.util.Iterator; public class CircularLinkedList implements Iterable { Node head , tail; int size; CircularLinkedList() { head = null; tail = null;
  • 7. size = 0; } public boolean add(E e) { Node ele = new Node(e); ele.next = head; if(head == null) { head = ele; ele.next = head; tail = head; } else { tail.next = ele; tail = ele; } size++; return true; } public boolean add(int index, E e){ Node ele = new Node(e); Node ptr = head; index = index - 1 ; for (int i = 1; i < size - 1; i++) { if (i == index) { Node tmp = ptr.next; ptr.next = ele; ele.next = tmp; break; } ptr = ptr.next; }
  • 8. size++ ; return true; } private Node getNode(int index ) { return null; } public E remove(int index) { Node ret; if (size == 1 && index == 1) { ret = head; head = null; tail = null; size = 0; return ret.getElement(); } if (index == 1) { ret = head; head = head.next; tail.next = head; size--; return ret.getElement(); } if (index == size) { Node s = head; Node t = head; while (s != tail) { t = s; s = s.next; } ret = tail; tail = head;
  • 9. tail = t; size --; return ret.getElement(); } Node ptr = head; index = index - 1 ; for (int i = 1; i < size - 1; i++) { if (i == index) { Node tmp = ptr.next; tmp = tmp.next; ptr.next = tmp; break; } ptr = ptr.next; } size-- ; return ptr.getElement(); } public String toString() { Node current = head; String result = ""; if(size == 0){ return ""; } if(size == 1) { return head.getElement().toString(); } else{ do{ result = result + current.getElement().toString(); result = result + " ==> "; current = current.next;
  • 10. } while(current != head); } return result; } public Iterator iterator() { return new ListIterator(); } private class ListIterator implements Iterator{ Node nextItem; Node prev; int index; @SuppressWarnings("unchecked") public ListIterator(){ nextItem = (Node) head; index = 0; } public boolean hasNext() { return size != 0; } public E next() { prev = nextItem; nextItem = nextItem.next; index = (index + 1) % size; return prev.getElement(); } public void remove() { int target;
  • 11. if(nextItem == head) { target = size - 1; } else{ target = index - 1; index--; } CircularLinkedList.this.remove(target); //calls the above class } } // Solve the problem in the main method // The answer of n = 13, k = 2 is // the 11th person in the ring (index 10) public static void main(String[] args){ CircularLinkedList l = new CircularLinkedList(); int n=13; int k=2; for(int i=1;i<=n;i++) l.add(Integer.valueOf(i)); int j = 13,i=0; while(j>0) { System.out.println(l.toString()); i = i + k; if(i>j) i = k; l.remove(i); j--; } } } OUTPUT: 1 ==> 2 ==> 3 ==> 4 ==> 5 ==> 6 ==> 7 ==> 8 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==>
  • 12. 1 ==> 3 ==> 4 ==> 5 ==> 6 ==> 7 ==> 8 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==> 1 ==> 3 ==> 4 ==> 6 ==> 7 ==> 8 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==> 1 ==> 3 ==> 4 ==> 6 ==> 7 ==> 9 ==> 10 ==> 11 ==> 12 ==> 13 ==> 1 ==> 3 ==> 4 ==> 6 ==> 7 ==> 9 ==> 10 ==> 12 ==> 13 ==> 1 ==> 4 ==> 6 ==> 7 ==> 9 ==> 10 ==> 12 ==> 13 ==> 1 ==> 4 ==> 6 ==> 9 ==> 10 ==> 12 ==> 13 ==> 1 ==> 4 ==> 6 ==> 9 ==> 10 ==> 13 ==> 1 ==> 6 ==> 9 ==> 10 ==> 13 ==> 1 ==> 6 ==> 9 ==> 13 ==> 1 ==> 9 ==> 13 ==> 1 ==> 13 ==> 1