Plz help me with the code. And here are the requirement. Thanks!!
You are required to design and implement a circular list using
provided class and
interface. Please filling the blank in CircularList.java. This
circular list has a tail
node which points to the end of the list and a number indicating
how many
elements in the list. And fill out the blank of the code below.
public class CircularList<T> implements
ListInterface<T> {
protected CLNode<T> tail; // tail node that contains the last
element of circular list
protected int numElements = 0; // number of elements in this
list
// instance variables that you may need for your method
protected CLNode<T> previous; // preceding node of target
found
protected boolean found; // true if target found
protected int targetIndex; // index of the target found
// constructor
public CircularList(){
tail = null;
numElements = 0;
}
// show tail of the list
public T getTail() {
return tail.getInfo();
}
// check if list is empty
public boolean isEmpty() {
return numElements == 0;
}
// the number of elements in circular list
public int size() {
return numElements;
}
// toString method
public String toString() {
String circularListString = "[";
if (!isEmpty()){
CLNode<T> curr = tail;
do {
curr = curr.getLink();
if (curr != tail) {
circularListString += curr.getInfo() + ", ";
} else {
circularListString += curr.getInfo();
}
} while (curr != tail);
}
circularListString += "]";
return circularListString;
}
////////////////// Your lab assignment starts from here ///////////////////
/*
Add the element to the end of the list, that is, make the new
element to be tail.
*/
public void add(T element) {
/* 1. Fill your code here */
/* 1. End of your code */
}
/*
Find the first occurance of the element in this list.
Note: find process should start from the first element of the list,
not the tail
i.e. list [5,2,3,5], find(5) should find the leftmost 5 instead of
the 5 at the end.
*/
public void find(T element) {
/* 2. Fill your code here */
/* 2. End of your code */
}
/*
Remove the first occurance of the element in this list.
Return true if success, false if not found.
You may use find() for this implementation.
*/
public boolean remove(T element) {
/* 3. Fill you code here */
/* 3. End of your code */
}
/*
Throws IndexOutOfBoundsException if passed an index argument
such that index < 0 or index > size().
Otherwise, adds element to this list at position index; all
current
elements at that position or higher have 1 added to their
index
*/
@Override
public void add(int index, T element) {
/* 4. Fill your code here */
/* 4. End of your code */
}
/*
Throws IndexOutOfBoundsException if passed an index argument
such that index < 0 or index >= size().
Otherwise, replaces element on this list at position index
with
newElement and returns the replaced element.
*/
@Override
public T set(int index, T newElement) {
/* 5. Fill you code here */
/* 5. End of your code */
}
/*
Throws IndexOutOfBoundsException if passed an index argument
such that index < 0 or index >= size().
Otherwise, returns the element on this list at position
index.
*/
@Override
public T get(int index) {
/* 6. Fill your code here */
/* 6. End of your code */
}
/*
If this list contains an element e such that
e.equals(target),
then returns the index of the first such element.
Otherwise, returns -1
*/
@Override
public int indexOf(T target) {
/* 7. Fill your code here */
/* 7. End of your code */
}
/*
Throws IndexOutOfBoundsException if passed an index argument
such that index < 0 or index >= size().
Otherwise, removes element on this list at position index and
returns the removed element; all current elements at
positions
higher than index have 1 subtracted from their position
*/
@Override
public T remove(int index) {
/* 8. Fill your code here */
/* 8. End of your code */
}
}
Here are other code resources.
public class CLNode<T> {
protected CLNode<T> link;
protected T info;
public CLNode(T info) {
this.info = info;
link = null;
}
public void setInfo(T info) {
this.info = info;
}
public T getInfo() {
return info;
}
public void setLink(CLNode<T> link) {
this.link = link;
}
public CLNode<T> getLink() {
return link;
}
}
public interface ListInterface<T> {
void add(int index, T element);
// Throws IndexOutOfBoundsException if passed an index
argument
// such that index < 0 or index > size().
// Otherwise, adds element to this list at position index; all
current
// elements at that position or higher have 1 added to their
index.
T set(int index, T newElement);
// Throws IndexOutOfBoundsException if passed an index
argument
// such that index < 0 or index >= size().
// Otherwise, replaces element on this list at position index
with
// newElement and returns the replaced element.
T get(int index);
// Throws IndexOutOfBoundsException if passed an index
argument
// such that index < 0 or index >= size().
// Otherwise, returns the element on this list at position
index.
int indexOf(T target);
// If this list contains an element e such that
e.equals(target),
// then returns the index of the first such element.
// Otherwise, returns -1.
T remove(int index);
// Throws IndexOutOfBoundsException if passed an index
argument
// such that index < 0 or index >= size().
// Otherwise, removes element on this list at position index
and
// returns the removed element; all current elements at
positions
// higher than index have 1 subtracted from their position
}
Also here is an example of output.
The original list:
[a, b, c, d, e, c, d, f] (Tail = "f")
After removing c, h, and f:
[a, b, d, e, c, d] (Tail = "d")
[EXCEPTION] Illegal index of 8 passed to CircularList add method.
After adding to index 6 and 8:
[a, b, d, e, c, d, h] (Tail = "h")
[EXCEPTION] Illegal index of 9 passed to CircularList set method.
After setting k to index 3 and 9:
[a, b, d, k, c, d, h] (Tail = "h")
The index of d is 2
The index of p is -1
The index 5 is d
[EXCEPTION] Illegal index of 0 passed to CircularList remove method.
After several removes:
[]
Please put your code between /* #. Filling your code here */ and
/* #. End of code
*/. Follow the instructions in comments carefully. Do not
change the code or
structure outside the marked blocks
Thanks again!!
// CLNode.java
public class CLNode<T> {
protected CLNode<T> link;
protected T info;
public CLNode(T info) {
this.info = info;
link = null;
}
public void setInfo(T info) {
this.info = info;
}
public T getInfo() {
return info;
}
public void setLink(CLNode<T> link) {
this.link = link;
}
public CLNode<T> getLink() {
return link;
}
}
//end of CLNode.java
// ListInterface.java
public interface ListInterface<T> {
void add(int index, T element);
// Throws IndexOutOfBoundsException if passed an index argument
// such that index < 0 or index > size().
// Otherwise, adds element to this list at position index; all current
// elements at that position or higher have 1 added to their index.
T set(int index, T newElement);
// Throws IndexOutOfBoundsException if passed an index argument
// such that index < 0 or index >= size().
// Otherwise, replaces element on this list at position index with
// newElement and returns the replaced element.
T get(int index);
// Throws IndexOutOfBoundsException if passed an index argument
// such that index < 0 or index >= size().
// Otherwise, returns the element on this list at position index.
int indexOf(T target);
// If this list contains an element e such that e.equals(target),
// then returns the index of the first such element.
// Otherwise, returns -1.
T remove(int index);
// Throws IndexOutOfBoundsException if passed an index argument
// such that index < 0 or index >= size().
// Otherwise, removes element on this list at position index and
// returns the removed element; all current elements at positions
// higher than index have 1 subtracted from their
position
}
//end of ListInterface.java
//CircularList.java
public class CircularList<T> implements ListInterface<T> {
protected CLNode<T> tail; // tail node that contains the last element of circular list
protected int numElements = 0; // number of elements in this list
// instance variables that you may need for your method
protected CLNode<T> previous; // preceding node of target found
protected boolean found; // true if target found
protected int targetIndex; // index of the target found
// constructor
public CircularList(){
tail = null;
numElements = 0;
}
// show tail of the list
public T getTail() {
return tail.getInfo();
}
// check if list is empty
public boolean isEmpty() {
return numElements == 0;
}
// the number of elements in circular list
public int size() {
return numElements;
}
// toString method
public String toString() {
String circularListString = "[";
if (!isEmpty()){
CLNode<T> curr = tail;
do {
curr = curr.getLink();
if (curr != tail) {
circularListString += curr.getInfo() + ", ";
} else {
circularListString += curr.getInfo();
}
} while (curr != tail);
}
circularListString += "]";
return circularListString;
}
/////////////////// Your lab assignment starts from here
///////////////////
/*
Add the element to the end of the list, that is, make
the new element to be tail.
*/
public void add(T element) {
// create a new node with element
as the info
CLNode<T> node = new
CLNode(element);
if(tail == null) // empty list,
make tail point to node and link of tail point's to itself
{
tail =
node;
tail.setLink(tail);
}else
{
node.setLink(tail.getLink()); // make node point to link of
tail
tail.setLink(node); // make tail's link point to node
tail = node; //
make node the new tail
}
numElements++; // increment the
number of elements
}
/*
Find the first occurance of the element in this
list.
Note: find process should start from the first element
of the list, not the tail
i.e. list [5,2,3,5], find(5) should find the leftmost
5 instead of the 5 at the end.
*/
public void find(T element) {
CLNode<T> curr = tail;
previous = tail;
found = false;
targetIndex = -1;
// non=empty list
if(curr != null)
{
curr =
curr.getLink();
targetIndex =
0;
// loop to find
the element from start to tail
while(curr !=
tail)
{
// element found
if(((Comparable)curr.getInfo()).compareTo(element) == 0)
{
found = true;
break;
}
previous = curr;
curr = curr.getLink();
targetIndex++;
}
if(!found) // if
element not found, check if tail's info = element
{
if(((Comparable)tail.getInfo()).compareTo(element) == 0)
{
found = true;
}
}
if(!found) //
element not found, set targetIndex to -1
targetIndex = -1;
}
}
/*
Remove the first occurance of the element in this
list.
Return true if success, false if not found.
You may use find() for this implementation.
*/
public boolean remove(T element) {
find(element); // find the first
occurrence of element in list using find
if(found)
{
if(numElements
== 1) // if list contains one element, set tail to null
tail = null;
else
if(previous.getLink() == tail) // if element is present in tail,
update tail
{
previous.setLink(tail.getLink());
tail = previous;
}else // update
the link of previous
{
previous.setLink(previous.getLink().getLink());
}
numElements--;
// decrement number of elements
return
true;
}
return false; // element not
found
}
/*
Throws IndexOutOfBoundsException if passed an index
argument
such that index < 0 or index > size().
Otherwise, adds element to this list at position
index; all current
elements at that position or higher have 1 added to
their index
*/
@Override
public void add(int index, T element) {
// validate index
if(index >= 0 && index
<=size())
{
if(index ==
size()) // add at tail
{
add(element);
}else
{
CLNode<T> node = new
CLNode<T>(element);
CLNode<T> curr = tail.getLink();
CLNode<T> prev = tail;
int idx = 0;
// loop to get the position of insertion
while(idx < index)
{
prev = curr;
curr = curr.getLink();
idx++;
}
// insert the node
node.setLink(curr);
prev.setLink(node);
numElements++;
}
}else
throw new
IndexOutOfBoundsException("Illegal index of "+index+" passed to
CircularList add method.");
}
/*
Throws IndexOutOfBoundsException if passed an index
argument
such that index < 0 or index >= size().
Otherwise, replaces element on this list at position
index with
newElement and returns the replaced element.
*/
@Override
public T set(int index, T newElement) {
// validate index
if(index >= 0 && index
< size())
{
if(index ==
size()-1)
{
T data = tail.getInfo();
tail.setInfo(newElement);
return data;
}else
{
CLNode<T> curr = tail.getLink();
int idx = 0;
// loop to get the node to update
while(idx < index)
{
curr = curr.getLink();
idx++;
}
// update the node
T data = curr.getInfo();
curr.setInfo(newElement);
return data;
}
}else
throw new
IndexOutOfBoundsException("Illegal index of "+index+" passed to
CircularList set method.");
}
/*
Throws IndexOutOfBoundsException if passed an index
argument
such that index < 0 or index >= size().
Otherwise, returns the element on this list at
position index.
*/
@Override
public T get(int index) {
// validate index
if(index >= 0 && index
< size())
{
if(index ==
size()-1)
return tail.getInfo();
else
{
CLNode<T> curr = tail.getLink();
int idx = 0;
// loop to get the node at the index
while(idx < index)
{
curr = curr.getLink();
idx++;
}
// return the info at index
return curr.getInfo();
}
}else
throw new
IndexOutOfBoundsException("Illegal index of "+index+" passed to
CircularList get method.");
}
/*
If this list contains an element e such that
e.equals(target),
then returns the index of the first such
element.
Otherwise, returns -1
*/
@Override
public int indexOf(T target) {
find(target); // find the first
occurrence of target in list
return targetIndex; // return the
index where target was found else -1 if not found
}
/*
Throws IndexOutOfBoundsException if passed an index
argument
such that index < 0 or index >= size().
Otherwise, removes element on this list at position
index and
returns the removed element; all current elements at
positions
higher than index have 1 subtracted from their
position
*/
@Override
public T remove(int index) {
// validate index
if(index >= 0 && index
< size())
{
CLNode<T>
curr = tail.getLink();
CLNode<T>
prev = tail;
int idx =
0;
// loop to get
the node to delete
while(idx <
index)
{
prev = curr;
curr = curr.getLink();
idx++;
}
// remove tail
node
if(index ==
size()-1)
{
if(tail.getLink() == tail) // list contains 1
element
{
tail = null;
}else
{
prev.setLink(curr.getLink());
tail = prev;
}
}else // remove
curr node
{
prev.setLink(curr.getLink());
}
numElements--;
// decrement the number of elements
return
curr.getInfo();
}else
throw new
IndexOutOfBoundsException("Illegal index of "+index+" passed to
CircularList remove method.");
}
}
//end of CircularList.java
Plz help me with the code. And here are the requirement. Thanks!! You are required to...
NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...
Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....
Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....
Given the Interface Code write a java class that implements this interface and show the working functionality in the main method: public interface CustomList<T> { /** * This method should add a new item into the <code>CustomList</code> and should * return <code>true</code> if it was successfully able to insert an item. * @param item the item to be added to the <code>CustomList</code> * @return <code>true</code> if item was successfully added, <code>false</code> if the item was not successfully added (note: it...
Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...
Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation for adding and removing items. You should reference the SortedArrayCollection class provided for how these algorithms should be implemented. What needs to change here? Is it a lot of code or not much? Include a toString method that creates and returns a string that correctly represents the current collection. Include a test driver application that demonstrates your class correctly. //--------------------------------------------------------------------------- // LinkedCollection.java // //...
I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the following constructor and methods: public KWArrayList() public boolean add(E anEntry) public E get(int index) { public E set(int index, E newValue) public E remove(int index) private void reallocate() public int size() public int indexOf(Object item) Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise Provide a constructor...
Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.String toString() creates and returns a string that correctly represents the current collection. 1. Such a method could prove useful for testing and debugging...
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1. You are given a list of phrases each ending with a pound sign: ‘#’. 2. Create a single String object from this list. 3. Then, split the String of phrases into an array of phrases...
Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...