SlideShare a Scribd company logo
To complete the task, you need to fill in the missing code. I’ve included code to create an
Iterator. An Iterator is an object that iterates over another object – in this case, a circular linked
list. You can use the .next() method to advance the Iterator to the next item (the first time you
call it, the iterator will travel to node at index 0). Using iterator’s .remove() removes the node the
iterator is currently at. Say that we had a CircularLinkedList that looked like this: A ==> B ==>
C ==> D ==> E ==> Calling .next() three times will advance the iterator to index 2. Calling
.remove() once will remove the node at index 2. A ==> B ==> D ==> E ==> Calling .remove()
once more will remove the node now at index 2. A ==> B ==> E ==> The Iterator methods
handle wrapping around the CircularLinkedList. Be sure to create the iterator using l.iterator()
and after you’ve added all the nodes to the list, where l is your CircularLinkedList
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;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
}
}
// 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;
l.add(10);
l.add(20);
l.add(21);
l.add(41);
System.out.println(l.toString());
l.add(2,15);
System.out.println(l.toString());
l.add(0,16);
System.out.println(l.toString());
l.remove(0);
System.out.println(l.toString());
// use the iterator to iterate around the list
Iterator iter = l.iterator();
while(iter.hasNext()){
System.out.println("Element:"+iter.next());
iter.remove();
}
}
}
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;
}
}
Online URL for output : http://ideone.com/KNv0G6

More Related Content

Similar to To complete the task, you need to fill in the missing code. I’ve inc.pdf

public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
accostinternational
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
ankit11134
 
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
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
poblettesedanoree498
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
mail931892
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
DaniyalAli81
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
arorastores
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
Himadri Sen Gupta
 
Implement the unsorted single linked list as we did in the class and .pdf
Implement the unsorted single linked list as we did in the class and .pdfImplement the unsorted single linked list as we did in the class and .pdf
Implement the unsorted single linked list as we did in the class and .pdf
arihantstoneart
 
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
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
Masud Parvaze
 
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
 
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
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
vishalateen
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
Stewart29UReesa
 
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
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
climatecontrolsv
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
arjunstores123
 
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdfSOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
arccreation001
 

Similar to To complete the task, you need to fill in the missing code. I’ve inc.pdf (20)

public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.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
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Implement the unsorted single linked list as we did in the class and .pdf
Implement the unsorted single linked list as we did in the class and .pdfImplement the unsorted single linked list as we did in the class and .pdf
Implement the unsorted single linked list as we did in the class and .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
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
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
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.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
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.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
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
 
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdfSOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
 

More from ezycolours78

Find the rate 32. of change in luxury purchases in China with respect.pdf
Find the rate 32. of change in luxury purchases in China with respect.pdfFind the rate 32. of change in luxury purchases in China with respect.pdf
Find the rate 32. of change in luxury purchases in China with respect.pdf
ezycolours78
 
Given f(x) = x^2x^2 + 3 Find (a) the domain and range of f, (b) the.pdf
Given f(x) = x^2x^2 + 3 Find (a) the domain and range of f, (b) the.pdfGiven f(x) = x^2x^2 + 3 Find (a) the domain and range of f, (b) the.pdf
Given f(x) = x^2x^2 + 3 Find (a) the domain and range of f, (b) the.pdf
ezycolours78
 
Help with Starting out with visual basic 7th edition chapter 6 Progr.pdf
Help with Starting out with visual basic 7th edition chapter 6 Progr.pdfHelp with Starting out with visual basic 7th edition chapter 6 Progr.pdf
Help with Starting out with visual basic 7th edition chapter 6 Progr.pdf
ezycolours78
 
Give short answers for the following questionsi.Define ‘Accountin.pdf
Give short answers for the following questionsi.Define ‘Accountin.pdfGive short answers for the following questionsi.Define ‘Accountin.pdf
Give short answers for the following questionsi.Define ‘Accountin.pdf
ezycolours78
 
For the reaction NO+03--NO2+O2, the reaction was found to be first .pdf
For the reaction NO+03--NO2+O2, the reaction was found to be first .pdfFor the reaction NO+03--NO2+O2, the reaction was found to be first .pdf
For the reaction NO+03--NO2+O2, the reaction was found to be first .pdf
ezycolours78
 
Discuss how you would go about setting up a human source collection .pdf
Discuss how you would go about setting up a human source collection .pdfDiscuss how you would go about setting up a human source collection .pdf
Discuss how you would go about setting up a human source collection .pdf
ezycolours78
 
Describe how meiotic and mitotic nondisjunctions occur and their pos.pdf
Describe how meiotic and mitotic nondisjunctions occur and their pos.pdfDescribe how meiotic and mitotic nondisjunctions occur and their pos.pdf
Describe how meiotic and mitotic nondisjunctions occur and their pos.pdf
ezycolours78
 
CASE 16 Southwest Airlines Andrew Inkpen and ex.pdf
CASE 16 Southwest Airlines Andrew Inkpen and ex.pdfCASE 16 Southwest Airlines Andrew Inkpen and ex.pdf
CASE 16 Southwest Airlines Andrew Inkpen and ex.pdf
ezycolours78
 
An epic bribe scandal at Petrobras, a major oil company controlled b.pdf
An epic bribe scandal at Petrobras, a major oil company controlled b.pdfAn epic bribe scandal at Petrobras, a major oil company controlled b.pdf
An epic bribe scandal at Petrobras, a major oil company controlled b.pdf
ezycolours78
 
6. Edit the following for the format of a reference listREFERENCE.pdf
6. Edit the following for the format of a reference listREFERENCE.pdf6. Edit the following for the format of a reference listREFERENCE.pdf
6. Edit the following for the format of a reference listREFERENCE.pdf
ezycolours78
 
2.Distinguish between assigned and emergent leaders. Give an example.pdf
2.Distinguish between assigned and emergent leaders. Give an example.pdf2.Distinguish between assigned and emergent leaders. Give an example.pdf
2.Distinguish between assigned and emergent leaders. Give an example.pdf
ezycolours78
 
3. What lines of evidence do we have that evolution has taken place.pdf
3. What lines of evidence do we have that evolution has taken place.pdf3. What lines of evidence do we have that evolution has taken place.pdf
3. What lines of evidence do we have that evolution has taken place.pdf
ezycolours78
 
4. Where in your home would endospores most likely be found Wh b. .pdf
4. Where in your home would endospores most likely be found Wh b. .pdf4. Where in your home would endospores most likely be found Wh b. .pdf
4. Where in your home would endospores most likely be found Wh b. .pdf
ezycolours78
 
25. Radium is called a _________ and replaces calcium in bones and c.pdf
25. Radium is called a _________ and replaces calcium in bones and c.pdf25. Radium is called a _________ and replaces calcium in bones and c.pdf
25. Radium is called a _________ and replaces calcium in bones and c.pdf
ezycolours78
 
What skills does law enforcement need to combat cyber crimesSol.pdf
What skills does law enforcement need to combat cyber crimesSol.pdfWhat skills does law enforcement need to combat cyber crimesSol.pdf
What skills does law enforcement need to combat cyber crimesSol.pdf
ezycolours78
 
Using Microsoft Visual Basics (.NET) ONLY!(This assignment encompa.pdf
Using Microsoft Visual Basics (.NET) ONLY!(This assignment encompa.pdfUsing Microsoft Visual Basics (.NET) ONLY!(This assignment encompa.pdf
Using Microsoft Visual Basics (.NET) ONLY!(This assignment encompa.pdf
ezycolours78
 
were CPA involved in Enron scandal act with integrity as defined by .pdf
were CPA involved in Enron scandal act with integrity as defined by .pdfwere CPA involved in Enron scandal act with integrity as defined by .pdf
were CPA involved in Enron scandal act with integrity as defined by .pdf
ezycolours78
 
16) What are the two types of current What is different about the fl.pdf
16) What are the two types of current What is different about the fl.pdf16) What are the two types of current What is different about the fl.pdf
16) What are the two types of current What is different about the fl.pdf
ezycolours78
 
What are some weakness of the NERC standard CPS1 and CPS2.Please lis.pdf
What are some weakness of the NERC standard CPS1 and CPS2.Please lis.pdfWhat are some weakness of the NERC standard CPS1 and CPS2.Please lis.pdf
What are some weakness of the NERC standard CPS1 and CPS2.Please lis.pdf
ezycolours78
 
The correct sequence of the phases of the cell cycle are A. prophase.pdf
The correct sequence of the phases of the cell cycle are  A. prophase.pdfThe correct sequence of the phases of the cell cycle are  A. prophase.pdf
The correct sequence of the phases of the cell cycle are A. prophase.pdf
ezycolours78
 

More from ezycolours78 (20)

Find the rate 32. of change in luxury purchases in China with respect.pdf
Find the rate 32. of change in luxury purchases in China with respect.pdfFind the rate 32. of change in luxury purchases in China with respect.pdf
Find the rate 32. of change in luxury purchases in China with respect.pdf
 
Given f(x) = x^2x^2 + 3 Find (a) the domain and range of f, (b) the.pdf
Given f(x) = x^2x^2 + 3 Find (a) the domain and range of f, (b) the.pdfGiven f(x) = x^2x^2 + 3 Find (a) the domain and range of f, (b) the.pdf
Given f(x) = x^2x^2 + 3 Find (a) the domain and range of f, (b) the.pdf
 
Help with Starting out with visual basic 7th edition chapter 6 Progr.pdf
Help with Starting out with visual basic 7th edition chapter 6 Progr.pdfHelp with Starting out with visual basic 7th edition chapter 6 Progr.pdf
Help with Starting out with visual basic 7th edition chapter 6 Progr.pdf
 
Give short answers for the following questionsi.Define ‘Accountin.pdf
Give short answers for the following questionsi.Define ‘Accountin.pdfGive short answers for the following questionsi.Define ‘Accountin.pdf
Give short answers for the following questionsi.Define ‘Accountin.pdf
 
For the reaction NO+03--NO2+O2, the reaction was found to be first .pdf
For the reaction NO+03--NO2+O2, the reaction was found to be first .pdfFor the reaction NO+03--NO2+O2, the reaction was found to be first .pdf
For the reaction NO+03--NO2+O2, the reaction was found to be first .pdf
 
Discuss how you would go about setting up a human source collection .pdf
Discuss how you would go about setting up a human source collection .pdfDiscuss how you would go about setting up a human source collection .pdf
Discuss how you would go about setting up a human source collection .pdf
 
Describe how meiotic and mitotic nondisjunctions occur and their pos.pdf
Describe how meiotic and mitotic nondisjunctions occur and their pos.pdfDescribe how meiotic and mitotic nondisjunctions occur and their pos.pdf
Describe how meiotic and mitotic nondisjunctions occur and their pos.pdf
 
CASE 16 Southwest Airlines Andrew Inkpen and ex.pdf
CASE 16 Southwest Airlines Andrew Inkpen and ex.pdfCASE 16 Southwest Airlines Andrew Inkpen and ex.pdf
CASE 16 Southwest Airlines Andrew Inkpen and ex.pdf
 
An epic bribe scandal at Petrobras, a major oil company controlled b.pdf
An epic bribe scandal at Petrobras, a major oil company controlled b.pdfAn epic bribe scandal at Petrobras, a major oil company controlled b.pdf
An epic bribe scandal at Petrobras, a major oil company controlled b.pdf
 
6. Edit the following for the format of a reference listREFERENCE.pdf
6. Edit the following for the format of a reference listREFERENCE.pdf6. Edit the following for the format of a reference listREFERENCE.pdf
6. Edit the following for the format of a reference listREFERENCE.pdf
 
2.Distinguish between assigned and emergent leaders. Give an example.pdf
2.Distinguish between assigned and emergent leaders. Give an example.pdf2.Distinguish between assigned and emergent leaders. Give an example.pdf
2.Distinguish between assigned and emergent leaders. Give an example.pdf
 
3. What lines of evidence do we have that evolution has taken place.pdf
3. What lines of evidence do we have that evolution has taken place.pdf3. What lines of evidence do we have that evolution has taken place.pdf
3. What lines of evidence do we have that evolution has taken place.pdf
 
4. Where in your home would endospores most likely be found Wh b. .pdf
4. Where in your home would endospores most likely be found Wh b. .pdf4. Where in your home would endospores most likely be found Wh b. .pdf
4. Where in your home would endospores most likely be found Wh b. .pdf
 
25. Radium is called a _________ and replaces calcium in bones and c.pdf
25. Radium is called a _________ and replaces calcium in bones and c.pdf25. Radium is called a _________ and replaces calcium in bones and c.pdf
25. Radium is called a _________ and replaces calcium in bones and c.pdf
 
What skills does law enforcement need to combat cyber crimesSol.pdf
What skills does law enforcement need to combat cyber crimesSol.pdfWhat skills does law enforcement need to combat cyber crimesSol.pdf
What skills does law enforcement need to combat cyber crimesSol.pdf
 
Using Microsoft Visual Basics (.NET) ONLY!(This assignment encompa.pdf
Using Microsoft Visual Basics (.NET) ONLY!(This assignment encompa.pdfUsing Microsoft Visual Basics (.NET) ONLY!(This assignment encompa.pdf
Using Microsoft Visual Basics (.NET) ONLY!(This assignment encompa.pdf
 
were CPA involved in Enron scandal act with integrity as defined by .pdf
were CPA involved in Enron scandal act with integrity as defined by .pdfwere CPA involved in Enron scandal act with integrity as defined by .pdf
were CPA involved in Enron scandal act with integrity as defined by .pdf
 
16) What are the two types of current What is different about the fl.pdf
16) What are the two types of current What is different about the fl.pdf16) What are the two types of current What is different about the fl.pdf
16) What are the two types of current What is different about the fl.pdf
 
What are some weakness of the NERC standard CPS1 and CPS2.Please lis.pdf
What are some weakness of the NERC standard CPS1 and CPS2.Please lis.pdfWhat are some weakness of the NERC standard CPS1 and CPS2.Please lis.pdf
What are some weakness of the NERC standard CPS1 and CPS2.Please lis.pdf
 
The correct sequence of the phases of the cell cycle are A. prophase.pdf
The correct sequence of the phases of the cell cycle are  A. prophase.pdfThe correct sequence of the phases of the cell cycle are  A. prophase.pdf
The correct sequence of the phases of the cell cycle are A. prophase.pdf
 

Recently uploaded

Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
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
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
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
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
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
 
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
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
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
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 

Recently uploaded (20)

Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
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
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
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
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
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
 
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 Á...
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
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...
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 

To complete the task, you need to fill in the missing code. I’ve inc.pdf

  • 1. To complete the task, you need to fill in the missing code. I’ve included code to create an Iterator. An Iterator is an object that iterates over another object – in this case, a circular linked list. You can use the .next() method to advance the Iterator to the next item (the first time you call it, the iterator will travel to node at index 0). Using iterator’s .remove() removes the node the iterator is currently at. Say that we had a CircularLinkedList that looked like this: A ==> B ==> C ==> D ==> E ==> Calling .next() three times will advance the iterator to index 2. Calling .remove() once will remove the node at index 2. A ==> B ==> D ==> E ==> Calling .remove() once more will remove the node now at index 2. A ==> B ==> E ==> The Iterator methods handle wrapping around the CircularLinkedList. Be sure to create the iterator using l.iterator() and after you’ve added all the nodes to the list, where l is your CircularLinkedList 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){
  • 2. 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
  • 3. // 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) {
  • 4. 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
  • 5. 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 } } // Solve the problem in the main method // The answer of n = 13, k = 2 is
  • 6. // the 11th person in the ring (index 10) public static void main(String[] args){ CircularLinkedList l = new CircularLinkedList(); int n; int k; l.add(10); l.add(20); l.add(21); l.add(41); System.out.println(l.toString()); l.add(2,15); System.out.println(l.toString()); l.add(0,16); System.out.println(l.toString()); l.remove(0); System.out.println(l.toString()); // use the iterator to iterate around the list Iterator iter = l.iterator(); while(iter.hasNext()){ System.out.println("Element:"+iter.next()); iter.remove(); } } } class Node { E element; Node next;
  • 7. 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; } } Online URL for output : http://ideone.com/KNv0G6