SlideShare a Scribd company logo
This is problem is same problem which i submitted on 2/20/17, I just modify
TestCirCularLinkedList1
import java.util.Iterator;
class CircularLinkedList implements Iterable {
// Your variables
// You can include a reference to a tail if you want
Node head;
int size; // BE SURE TO KEEP TRACK OF THE SIZE
// implement this constructor
public CircularLinkedList() {
head=null;
}
// writing helper functions for add and remove, like the book did can help
// but remember, the last element's next node will be the head!
// attach a node to the end of the list
// Be sure to handle the adding to an empty list
// always returns true
public boolean add(E e) {
Node newNode=new Node(e);
if(size==0){
head=newNode;
}
else{
Node last=getNode(size-1);
last.next=newNode;
}
newNode.next=head; //last element node is set to head
size++;
return true;
}
// need to handle
// out of bounds
// empty list
// adding to front
// adding to middle
// adding to "end"
// REMEMBER TO INCREMENT THE SIZE
public boolean add(int index, E e){
if(index>size) return false;
Node tmp=new Node(e);
if(index==0){
tmp.next=head;
Node last=getNode(size-1);
head=tmp;
last.next=head;
}
else {
Node curr=getNode(index-1);
tmp.next=curr.next;
curr.next=tmp;
}
size++;
return true;
}
// I highly recommend using this helper method
// Return Node found at the specified index
// be sure to handle out of bounds cases
private Node getNode(int index ) {
Node prev=head;
for(int i=0;isize) {
e=null;
}
else if(index==0){
e = head.getElement();
Node last=getNode(size-1);
head=head.next;
last.next=head;
size--;
}
else{
Node prev=getNode(index-1);
Node curr=getNode(index);
e=curr.getElement();
prev.next=curr.next;
size--;
}
return e;
}
// Turns your list into a string
// Useful for debugging
public String toString(){
Node current = head;
StringBuilder result = new StringBuilder();
if(size == 0){
return "";
}
if(size == 1) {
return head.getElement().toString();
}
else{
do{
result.append(current.getElement());
result.append(" ==> ");
current = current.next;
} while(current != head);
}
return result.toString();
}
public Iterator iterator() {
return new ListIterator();
}
// provided code
// read the comments to figure out how this works and see how to use it
// you should not have to change this
// change at your own risk!
private class ListIterator implements Iterator{
Node nextItem;
Node prev;
int index;
@SuppressWarnings("unchecked")
//Creates a new iterator that starts at the head of the list
public ListIterator(){
nextItem = (Node) head;
index = 0;
}
// returns true if there is a next node
// this is always should return true if the list has something in it
public boolean hasNext() {
// TODO Auto-generated method stub
return size != 0;
}
// advances the iterator to the next item
// handles wrapping around back to the head automatically for you
public E next() {
// TODO Auto-generated method stub
prev = nextItem;
nextItem = nextItem.next;
index = (index + 1) % size;
return prev.getElement();
}
// removed the last node was visted by the .next() call
// for example if we had just created a iterator
// the following calls would remove the item at index 1 (the second person in the ring)
// next() next() remove()
public void remove() {
int target;
if(nextItem == head) {
target = size - 1;
} else{
target = index - 1;
index--;
}
CircularLinkedList.this.remove(target); //calls the above class
}
}
}
class Node {
E element;
Node next;
public Node() {
this.element = null;
this.next = null;
}
public Node(E e) {
this.element = e;
this.next = null;
}
public E getElement() {
return this.element;
}
public void setElement(E element) {
this.element= element;
}
}
import java.util.Iterator;
public class TestCircularLinkedList1 {
// 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;
int k;
for (int n = 0; n<5; n++){
l.add(n);
}
l.add(1);
l.add(2);
l.add(3);
l.add(4);
System.out.println(l.toString());
l.add(3,5);
// System.out.println(l.toString());
// l.add(0,16);
System.out.println(l.toString());
l.remove(2);
System.out.println(l.toString());
// use the iterator to iterate around the list
Iterator iter = l.iterator();
while(l.size >1){
for(int i=0; i<2; i++){
iter.next();
}
System.out.println("Element:"+iter.next());
iter.remove();
}
}
}
this code is print like this way CircularLinkedlist
0 ==> 1 ==> 2 ==> 3 ==> 4 ==> 1 ==> 2 ==> 3 ==> 4 ==>
0 ==> 1 ==> 2 ==> 5 ==> 3 ==> 4 ==> 1 ==> 2 ==> 3 ==> 4 ==>
0 ==> 1 ==> 5 ==> 3 ==> 4 ==> 1 ==> 2 ==> 3 ==> 4 ==>
But i need to print like down
For a ring of n = 5 and the count k = 2:1 1 ==> 2 ==> 3 ==> 4 ==> 5 ==>
1 ==> 3 ==> 4 ==> 5 ==>
1 ==> 3 ==> 5 ==>
3 ==> 5 ==>
3
Solution
import java.util.Iterator;
class CircularLinkedList implements Iterable {
// Your variables
// You can include a reference to a tail if you want
Node head;
int size; // BE SURE TO KEEP TRACK OF THE SIZE
// implement this constructor
public CircularLinkedList() {
head = null;
}
// writing helper functions for add and remove, like the book did can help
// but remember, the last element's next node will be the head!
// attach a node to the end of the list
// Be sure to handle the adding to an empty list
// always returns true
public boolean add(E e) {
Node newNode = new Node(e);
if (size == 0) {
head = newNode;
} else {
Node last = getNode(size - 1);
last.next = newNode;
}
newNode.next = head; // last element node is set to head
size++;
return true;
}
// need to handle
// out of bounds
// empty list
// adding to front
// adding to middle
// adding to "end"
// REMEMBER TO INCREMENT THE SIZE
public boolean add(int index, E e) {
if (index > size)
return false;
Node tmp = new Node(e);
if (index == 0) {
tmp.next = head;
Node last = getNode(size - 1);
head = tmp;
last.next = head;
} else {
Node curr = getNode(index - 1);
tmp.next = curr.next;
curr.next = tmp;
}
size++;
return true;
}
// I highly recommend using this helper method
// Return Node found at the specified index
// be sure to handle out of bounds cases
private Node getNode(int index) {
Node prev = head;
for (int i = 0; i < size && index < size; i++) {
if (i == index) {
return prev;
}
prev = prev.next;
}
return null;
}
// remove must handle the following cases
// out of bounds
// removing the only thing in the list
// removing the first thing in the list (need to adjust the last thing in
// the list to point to the beginning)
// removing the last thing (if you have a tail)
// removing any other node.
// REMEMBER TO DECREMENT THE SIZE
public E remove(int index) {
E e;
if (index > size) {
e = null;
} else if (index == 0) {
e = head.getElement();
Node last = getNode(size - 1);
head = head.next;
last.next = head;
size--;
} else {
Node prev = getNode(index - 1);
Node curr = getNode(index);
e = curr.getElement();
prev.next = curr.next;
size--;
}
return e;
}
// Turns your list into a string
// Useful for debugging
public String toString() {
Node current = head;
StringBuilder result = new StringBuilder();
if (size == 0) {
return "";
}
if (size == 1) {
return head.getElement().toString();
} else {
do {
result.append(current.getElement());
result.append(" ==> ");
current = current.next;
} while (current != head);
}
return result.toString();
}
public Iterator iterator() {
return new ListIterator();
}
// provided code
// read the comments to figure out how this works and see how to use it
// you should not have to change this
// change at your own risk!
private class ListIterator implements Iterator {
Node nextItem;
Node prev;
int index;
@SuppressWarnings("unchecked")
// Creates a new iterator that starts at the head of the list
public ListIterator() {
nextItem = (Node) head;
index = 0;
}
// returns true if there is a next node
// this is always should return true if the list has something in it
public boolean hasNext() {
// TODO Auto-generated method stub
return size != 0;
}
// advances the iterator to the next item
// handles wrapping around back to the head automatically for you
public E next() {
// TODO Auto-generated method stub
prev = nextItem;
nextItem = nextItem.next;
index = (index + 1) % size;
return prev.getElement();
}
// removed the last node was visted by the .next() call
// for example if we had just created a iterator
// the following calls would remove the item at index 1 (the second
// person in the ring)
// next() next() remove()
public void remove() {
int target;
if (nextItem == head) {
target = size - 1;
} else {
target = index - 1;
index--;
}
CircularLinkedList.this.remove(target); // calls the above class
}
}
}
*****************************************************************************
************************************
class Node {
E element;
Node next;
public Node() {
this.element = null;
this.next = null;
}
public Node(E e) {
this.element = e;
this.next = null;
}
public E getElement() {
return this.element;
}
public void setElement(E element) {
this.element = element;
}
}
*****************************************************************************
********************************
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
CircularLinkedList l = new CircularLinkedList();
// int n;
int k;
for (int n = 1; n <= 5; n++)
l.add(n);
System.out.println(l.toString());
l.remove(1);
System.out.println(l.toString());
l.remove(2);
System.out.println(l.toString());
l.remove(0);
System.out.println(l.toString());
l.remove(1);
System.out.println(l.toString());
/*
* // use the iterator to iterate around the list Iterator iter
* = l.iterator(); while (l.size > 1) {
*
* for (int i = 0; i < 2; i++) iter.next();
*
* System.out.println("Element:" + iter.next());
*
* iter.remove(); }
*/}
}
output
1 ==> 2 ==> 3 ==> 4 ==> 5 ==>
1 ==> 3 ==> 4 ==> 5 ==>
1 ==> 3 ==> 5 ==>
3 ==> 5 ==>
3

More Related Content

Similar to This is problem is same problem which i submitted on 22017, I just.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
babitasingh698417
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
arrowmobile
 
public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
accostinternational
 
Help please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdfHelp please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdf
jyothimuppasani1
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
EricvtJFraserr
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
archgeetsenterprises
 
Please correct my errors upvote Clears our entire .pdf
Please correct my errors upvote      Clears our entire .pdfPlease correct my errors upvote      Clears our entire .pdf
Please correct my errors upvote Clears our entire .pdf
kitty811
 
Please need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfPlease need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdf
nitinarora01
 
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
annaelctronics
 
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
mckellarhastings
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
afgt2012
 
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfin Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
sauravmanwanicp
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
mail931892
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
farrahkur54
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
JamesPXNNewmanp
 
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
BANSALANKIT1077
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
sales98
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
mail931892
 

Similar to This is problem is same problem which i submitted on 22017, I just.pdf (20)

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
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
 
Help please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdfHelp please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdf
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
 
Please correct my errors upvote Clears our entire .pdf
Please correct my errors upvote      Clears our entire .pdfPlease correct my errors upvote      Clears our entire .pdf
Please correct my errors upvote Clears our entire .pdf
 
Please need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfPlease need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdf
 
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
 
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
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
 
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfin Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
 
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
 

More from fcaindore

You are the CIO of a medium size company tasked with modernizing the.pdf
You are the CIO of a medium size company tasked with modernizing the.pdfYou are the CIO of a medium size company tasked with modernizing the.pdf
You are the CIO of a medium size company tasked with modernizing the.pdf
fcaindore
 
Why are some goods only provided by the government Why are .pdf
Why are some goods only provided by the government Why are .pdfWhy are some goods only provided by the government Why are .pdf
Why are some goods only provided by the government Why are .pdf
fcaindore
 
When a supervisor comes to the HR manager to evaluate disciplinary a.pdf
When a supervisor comes to the HR manager to evaluate disciplinary a.pdfWhen a supervisor comes to the HR manager to evaluate disciplinary a.pdf
When a supervisor comes to the HR manager to evaluate disciplinary a.pdf
fcaindore
 
What is the role of an ethical leader in corporate cultures a. A le.pdf
What is the role of an ethical leader in corporate cultures a. A le.pdfWhat is the role of an ethical leader in corporate cultures a. A le.pdf
What is the role of an ethical leader in corporate cultures a. A le.pdf
fcaindore
 
What is the main purpose of a project management planSolution.pdf
What is the main purpose of a project management planSolution.pdfWhat is the main purpose of a project management planSolution.pdf
What is the main purpose of a project management planSolution.pdf
fcaindore
 
What is the difference between a balanace sheet and a net income she.pdf
What is the difference between a balanace sheet and a net income she.pdfWhat is the difference between a balanace sheet and a net income she.pdf
What is the difference between a balanace sheet and a net income she.pdf
fcaindore
 
What is involved in personalization and codification of tacit to exp.pdf
What is involved in personalization and codification of tacit to exp.pdfWhat is involved in personalization and codification of tacit to exp.pdf
What is involved in personalization and codification of tacit to exp.pdf
fcaindore
 
What dimensions of quality were highlighted in the Delta Airlines ba.pdf
What dimensions of quality were highlighted in the Delta Airlines ba.pdfWhat dimensions of quality were highlighted in the Delta Airlines ba.pdf
What dimensions of quality were highlighted in the Delta Airlines ba.pdf
fcaindore
 
What are the similarities and differences between the ETC of Photosy.pdf
What are the similarities and differences between the ETC of Photosy.pdfWhat are the similarities and differences between the ETC of Photosy.pdf
What are the similarities and differences between the ETC of Photosy.pdf
fcaindore
 
What are non-tax costs of tax planningSolutionFollowings are .pdf
What are non-tax costs of tax planningSolutionFollowings are .pdfWhat are non-tax costs of tax planningSolutionFollowings are .pdf
What are non-tax costs of tax planningSolutionFollowings are .pdf
fcaindore
 
There is a host of sociological and cultural research that paints a r.pdf
There is a host of sociological and cultural research that paints a r.pdfThere is a host of sociological and cultural research that paints a r.pdf
There is a host of sociological and cultural research that paints a r.pdf
fcaindore
 
True or False With argument passage by reference, the address of the.pdf
True or False With argument passage by reference, the address of the.pdfTrue or False With argument passage by reference, the address of the.pdf
True or False With argument passage by reference, the address of the.pdf
fcaindore
 
SOme of functions of the eukaryotic orgenelles are performed in bact.pdf
SOme of functions of the eukaryotic orgenelles are performed in bact.pdfSOme of functions of the eukaryotic orgenelles are performed in bact.pdf
SOme of functions of the eukaryotic orgenelles are performed in bact.pdf
fcaindore
 
please explain the global entreprenurship revolution for a flatter w.pdf
please explain the global entreprenurship revolution for a flatter w.pdfplease explain the global entreprenurship revolution for a flatter w.pdf
please explain the global entreprenurship revolution for a flatter w.pdf
fcaindore
 
ourse O D. growth rate of currency in circulation-growth rate of the .pdf
ourse O D. growth rate of currency in circulation-growth rate of the .pdfourse O D. growth rate of currency in circulation-growth rate of the .pdf
ourse O D. growth rate of currency in circulation-growth rate of the .pdf
fcaindore
 
Modify the project so tat records are inserted into the random acess.pdf
Modify the project so tat records are inserted into the random acess.pdfModify the project so tat records are inserted into the random acess.pdf
Modify the project so tat records are inserted into the random acess.pdf
fcaindore
 
Many hospitals have systems in place and are now or will in the futu.pdf
Many hospitals have systems in place and are now or will in the futu.pdfMany hospitals have systems in place and are now or will in the futu.pdf
Many hospitals have systems in place and are now or will in the futu.pdf
fcaindore
 
List the S + D and the organism that causes a vesicle, Gumma, purule.pdf
List the S + D and the organism that causes a vesicle, Gumma, purule.pdfList the S + D and the organism that causes a vesicle, Gumma, purule.pdf
List the S + D and the organism that causes a vesicle, Gumma, purule.pdf
fcaindore
 
Let k be the number of ON-state devices in a group of n devices on a .pdf
Let k be the number of ON-state devices in a group of n devices on a .pdfLet k be the number of ON-state devices in a group of n devices on a .pdf
Let k be the number of ON-state devices in a group of n devices on a .pdf
fcaindore
 
In Java, write an assignment statement that takes the fifth power of.pdf
In Java, write an assignment statement that takes the fifth power of.pdfIn Java, write an assignment statement that takes the fifth power of.pdf
In Java, write an assignment statement that takes the fifth power of.pdf
fcaindore
 

More from fcaindore (20)

You are the CIO of a medium size company tasked with modernizing the.pdf
You are the CIO of a medium size company tasked with modernizing the.pdfYou are the CIO of a medium size company tasked with modernizing the.pdf
You are the CIO of a medium size company tasked with modernizing the.pdf
 
Why are some goods only provided by the government Why are .pdf
Why are some goods only provided by the government Why are .pdfWhy are some goods only provided by the government Why are .pdf
Why are some goods only provided by the government Why are .pdf
 
When a supervisor comes to the HR manager to evaluate disciplinary a.pdf
When a supervisor comes to the HR manager to evaluate disciplinary a.pdfWhen a supervisor comes to the HR manager to evaluate disciplinary a.pdf
When a supervisor comes to the HR manager to evaluate disciplinary a.pdf
 
What is the role of an ethical leader in corporate cultures a. A le.pdf
What is the role of an ethical leader in corporate cultures a. A le.pdfWhat is the role of an ethical leader in corporate cultures a. A le.pdf
What is the role of an ethical leader in corporate cultures a. A le.pdf
 
What is the main purpose of a project management planSolution.pdf
What is the main purpose of a project management planSolution.pdfWhat is the main purpose of a project management planSolution.pdf
What is the main purpose of a project management planSolution.pdf
 
What is the difference between a balanace sheet and a net income she.pdf
What is the difference between a balanace sheet and a net income she.pdfWhat is the difference between a balanace sheet and a net income she.pdf
What is the difference between a balanace sheet and a net income she.pdf
 
What is involved in personalization and codification of tacit to exp.pdf
What is involved in personalization and codification of tacit to exp.pdfWhat is involved in personalization and codification of tacit to exp.pdf
What is involved in personalization and codification of tacit to exp.pdf
 
What dimensions of quality were highlighted in the Delta Airlines ba.pdf
What dimensions of quality were highlighted in the Delta Airlines ba.pdfWhat dimensions of quality were highlighted in the Delta Airlines ba.pdf
What dimensions of quality were highlighted in the Delta Airlines ba.pdf
 
What are the similarities and differences between the ETC of Photosy.pdf
What are the similarities and differences between the ETC of Photosy.pdfWhat are the similarities and differences between the ETC of Photosy.pdf
What are the similarities and differences between the ETC of Photosy.pdf
 
What are non-tax costs of tax planningSolutionFollowings are .pdf
What are non-tax costs of tax planningSolutionFollowings are .pdfWhat are non-tax costs of tax planningSolutionFollowings are .pdf
What are non-tax costs of tax planningSolutionFollowings are .pdf
 
There is a host of sociological and cultural research that paints a r.pdf
There is a host of sociological and cultural research that paints a r.pdfThere is a host of sociological and cultural research that paints a r.pdf
There is a host of sociological and cultural research that paints a r.pdf
 
True or False With argument passage by reference, the address of the.pdf
True or False With argument passage by reference, the address of the.pdfTrue or False With argument passage by reference, the address of the.pdf
True or False With argument passage by reference, the address of the.pdf
 
SOme of functions of the eukaryotic orgenelles are performed in bact.pdf
SOme of functions of the eukaryotic orgenelles are performed in bact.pdfSOme of functions of the eukaryotic orgenelles are performed in bact.pdf
SOme of functions of the eukaryotic orgenelles are performed in bact.pdf
 
please explain the global entreprenurship revolution for a flatter w.pdf
please explain the global entreprenurship revolution for a flatter w.pdfplease explain the global entreprenurship revolution for a flatter w.pdf
please explain the global entreprenurship revolution for a flatter w.pdf
 
ourse O D. growth rate of currency in circulation-growth rate of the .pdf
ourse O D. growth rate of currency in circulation-growth rate of the .pdfourse O D. growth rate of currency in circulation-growth rate of the .pdf
ourse O D. growth rate of currency in circulation-growth rate of the .pdf
 
Modify the project so tat records are inserted into the random acess.pdf
Modify the project so tat records are inserted into the random acess.pdfModify the project so tat records are inserted into the random acess.pdf
Modify the project so tat records are inserted into the random acess.pdf
 
Many hospitals have systems in place and are now or will in the futu.pdf
Many hospitals have systems in place and are now or will in the futu.pdfMany hospitals have systems in place and are now or will in the futu.pdf
Many hospitals have systems in place and are now or will in the futu.pdf
 
List the S + D and the organism that causes a vesicle, Gumma, purule.pdf
List the S + D and the organism that causes a vesicle, Gumma, purule.pdfList the S + D and the organism that causes a vesicle, Gumma, purule.pdf
List the S + D and the organism that causes a vesicle, Gumma, purule.pdf
 
Let k be the number of ON-state devices in a group of n devices on a .pdf
Let k be the number of ON-state devices in a group of n devices on a .pdfLet k be the number of ON-state devices in a group of n devices on a .pdf
Let k be the number of ON-state devices in a group of n devices on a .pdf
 
In Java, write an assignment statement that takes the fifth power of.pdf
In Java, write an assignment statement that takes the fifth power of.pdfIn Java, write an assignment statement that takes the fifth power of.pdf
In Java, write an assignment statement that takes the fifth power of.pdf
 

Recently uploaded

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 

Recently uploaded (20)

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 

This is problem is same problem which i submitted on 22017, I just.pdf

  • 1. This is problem is same problem which i submitted on 2/20/17, I just modify TestCirCularLinkedList1 import java.util.Iterator; class CircularLinkedList implements Iterable { // Your variables // You can include a reference to a tail if you want Node head; int size; // BE SURE TO KEEP TRACK OF THE SIZE // implement this constructor public CircularLinkedList() { head=null; } // writing helper functions for add and remove, like the book did can help // but remember, the last element's next node will be the head! // attach a node to the end of the list // Be sure to handle the adding to an empty list // always returns true public boolean add(E e) { Node newNode=new Node(e); if(size==0){ head=newNode; } else{ Node last=getNode(size-1); last.next=newNode; } newNode.next=head; //last element node is set to head size++; return true; } // need to handle
  • 2. // out of bounds // empty list // adding to front // adding to middle // adding to "end" // REMEMBER TO INCREMENT THE SIZE public boolean add(int index, E e){ if(index>size) return false; Node tmp=new Node(e); if(index==0){ tmp.next=head; Node last=getNode(size-1); head=tmp; last.next=head; } else { Node curr=getNode(index-1); tmp.next=curr.next; curr.next=tmp; } size++; return true; } // I highly recommend using this helper method // Return Node found at the specified index // be sure to handle out of bounds cases private Node getNode(int index ) { Node prev=head; for(int i=0;isize) { e=null; } else if(index==0){ e = head.getElement(); Node last=getNode(size-1); head=head.next; last.next=head;
  • 3. size--; } else{ Node prev=getNode(index-1); Node curr=getNode(index); e=curr.getElement(); prev.next=curr.next; size--; } return e; } // Turns your list into a string // Useful for debugging public String toString(){ Node current = head; StringBuilder result = new StringBuilder(); if(size == 0){ return ""; } if(size == 1) { return head.getElement().toString(); } else{ do{ result.append(current.getElement()); result.append(" ==> "); current = current.next; } while(current != head); } return result.toString(); }
  • 4. public Iterator iterator() { return new ListIterator(); } // provided code // read the comments to figure out how this works and see how to use it // you should not have to change this // change at your own risk! private class ListIterator implements Iterator{ Node nextItem; Node prev; int index; @SuppressWarnings("unchecked") //Creates a new iterator that starts at the head of the list public ListIterator(){ nextItem = (Node) head; index = 0; } // returns true if there is a next node // this is always should return true if the list has something in it public boolean hasNext() { // TODO Auto-generated method stub return size != 0; } // advances the iterator to the next item // handles wrapping around back to the head automatically for you public E next() { // TODO Auto-generated method stub prev = nextItem; nextItem = nextItem.next; index = (index + 1) % size;
  • 5. return prev.getElement(); } // removed the last node was visted by the .next() call // for example if we had just created a iterator // the following calls would remove the item at index 1 (the second person in the ring) // next() next() remove() public void remove() { int target; if(nextItem == head) { target = size - 1; } else{ target = index - 1; index--; } CircularLinkedList.this.remove(target); //calls the above class } } } class Node { E element; Node next; public Node() { this.element = null; this.next = null; } public Node(E e) { this.element = e; this.next = null; }
  • 6. public E getElement() { return this.element; } public void setElement(E element) { this.element= element; } } import java.util.Iterator; public class TestCircularLinkedList1 { // 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; int k; for (int n = 0; n<5; n++){ l.add(n); } l.add(1); l.add(2); l.add(3); l.add(4); System.out.println(l.toString()); l.add(3,5); // System.out.println(l.toString()); // l.add(0,16); System.out.println(l.toString()); l.remove(2); System.out.println(l.toString());
  • 7. // use the iterator to iterate around the list Iterator iter = l.iterator(); while(l.size >1){ for(int i=0; i<2; i++){ iter.next(); } System.out.println("Element:"+iter.next()); iter.remove(); } } } this code is print like this way CircularLinkedlist 0 ==> 1 ==> 2 ==> 3 ==> 4 ==> 1 ==> 2 ==> 3 ==> 4 ==> 0 ==> 1 ==> 2 ==> 5 ==> 3 ==> 4 ==> 1 ==> 2 ==> 3 ==> 4 ==> 0 ==> 1 ==> 5 ==> 3 ==> 4 ==> 1 ==> 2 ==> 3 ==> 4 ==> But i need to print like down For a ring of n = 5 and the count k = 2:1 1 ==> 2 ==> 3 ==> 4 ==> 5 ==> 1 ==> 3 ==> 4 ==> 5 ==> 1 ==> 3 ==> 5 ==> 3 ==> 5 ==> 3 Solution import java.util.Iterator; class CircularLinkedList implements Iterable { // Your variables // You can include a reference to a tail if you want Node head; int size; // BE SURE TO KEEP TRACK OF THE SIZE
  • 8. // implement this constructor public CircularLinkedList() { head = null; } // writing helper functions for add and remove, like the book did can help // but remember, the last element's next node will be the head! // attach a node to the end of the list // Be sure to handle the adding to an empty list // always returns true public boolean add(E e) { Node newNode = new Node(e); if (size == 0) { head = newNode; } else { Node last = getNode(size - 1); last.next = newNode; } newNode.next = head; // last element node is set to head size++; return true; } // need to handle // out of bounds // empty list // adding to front // adding to middle // adding to "end" // REMEMBER TO INCREMENT THE SIZE public boolean add(int index, E e) { if (index > size) return false; Node tmp = new Node(e); if (index == 0) { tmp.next = head; Node last = getNode(size - 1); head = tmp;
  • 9. last.next = head; } else { Node curr = getNode(index - 1); tmp.next = curr.next; curr.next = tmp; } size++; return true; } // I highly recommend using this helper method // Return Node found at the specified index // be sure to handle out of bounds cases private Node getNode(int index) { Node prev = head; for (int i = 0; i < size && index < size; i++) { if (i == index) { return prev; } prev = prev.next; } return null; } // remove must handle the following cases // out of bounds // removing the only thing in the list // removing the first thing in the list (need to adjust the last thing in // the list to point to the beginning) // removing the last thing (if you have a tail) // removing any other node. // REMEMBER TO DECREMENT THE SIZE public E remove(int index) { E e; if (index > size) { e = null; } else if (index == 0) { e = head.getElement();
  • 10. Node last = getNode(size - 1); head = head.next; last.next = head; size--; } else { Node prev = getNode(index - 1); Node curr = getNode(index); e = curr.getElement(); prev.next = curr.next; size--; } return e; } // Turns your list into a string // Useful for debugging public String toString() { Node current = head; StringBuilder result = new StringBuilder(); if (size == 0) { return ""; } if (size == 1) { return head.getElement().toString(); } else { do { result.append(current.getElement()); result.append(" ==> "); current = current.next; } while (current != head); } return result.toString(); } public Iterator iterator() { return new ListIterator(); } // provided code
  • 11. // read the comments to figure out how this works and see how to use it // you should not have to change this // change at your own risk! private class ListIterator implements Iterator { Node nextItem; Node prev; int index; @SuppressWarnings("unchecked") // Creates a new iterator that starts at the head of the list public ListIterator() { nextItem = (Node) head; index = 0; } // returns true if there is a next node // this is always should return true if the list has something in it public boolean hasNext() { // TODO Auto-generated method stub return size != 0; } // advances the iterator to the next item // handles wrapping around back to the head automatically for you public E next() { // TODO Auto-generated method stub prev = nextItem; nextItem = nextItem.next; index = (index + 1) % size; return prev.getElement(); } // removed the last node was visted by the .next() call // for example if we had just created a iterator // the following calls would remove the item at index 1 (the second // person in the ring) // next() next() remove() public void remove() { int target; if (nextItem == head) {
  • 12. target = size - 1; } else { target = index - 1; index--; } CircularLinkedList.this.remove(target); // calls the above class } } } ***************************************************************************** ************************************ class Node { E element; Node next; public Node() { this.element = null; this.next = null; } public Node(E e) { this.element = e; this.next = null; } public E getElement() { return this.element; } public void setElement(E element) { this.element = element; } } ***************************************************************************** ******************************** import java.util.Iterator; public class Test { public static void main(String[] args) { CircularLinkedList l = new CircularLinkedList(); // int n;
  • 13. int k; for (int n = 1; n <= 5; n++) l.add(n); System.out.println(l.toString()); l.remove(1); System.out.println(l.toString()); l.remove(2); System.out.println(l.toString()); l.remove(0); System.out.println(l.toString()); l.remove(1); System.out.println(l.toString()); /* * // use the iterator to iterate around the list Iterator iter * = l.iterator(); while (l.size > 1) { * * for (int i = 0; i < 2; i++) iter.next(); * * System.out.println("Element:" + iter.next()); * * iter.remove(); } */} } output 1 ==> 2 ==> 3 ==> 4 ==> 5 ==> 1 ==> 3 ==> 4 ==> 5 ==> 1 ==> 3 ==> 5 ==> 3 ==> 5 ==> 3