FOLLWING IS THE CODE YOU NEED TO FILL UP : public class CircularLinkedList<AnyTy
ID: 3592706 • Letter: F
Question
FOLLWING IS THE CODE YOU NEED TO FILL UP :
public class CircularLinkedList<AnyType> implements List<AnyType>
{
private static class Node<AnyType>
{
private AnyType data;
private Node<AnyType> next;
public Node(AnyType d, Node<AnyType> n)
{
setData(d);
setNext(n);
}
public AnyType getData() { return data; }
public void setData(AnyType d) { data = d; }
public Node<AnyType> getNext() { return next; }
public void setNext(Node<AnyType> n) { next = n; }
}
private int theSize;
private int modCount;
private Node<AnyType> tail;
public CircularLinkedList()
{
}
public void clear()
{
}
public int size()
{
}
public boolean isEmpty()
{
}
public AnyType get(int index)
{
}
public AnyType set(int index, AnyType newValue)
{
}
public boolean add(AnyType newValue)
{
add(size(), newValue);
return true;
}
public void add(int index, AnyType newValue)
{
}
public AnyType remove(int index)
{
}
public void rotate()
{
}
public Iterator<AnyType> iterator()
{
return new LinkedListIterator();
}
private Node<AnyType> getNode(int index)
{
return (getNode(index, 0, size()-1));
}
private Node<AnyType> getNode(int index, int lower, int upper)
{
}
private class LinkedListIterator implements Iterator<AnyType>
{
private Node<AnyType> previous;
private Node<AnyType> current;
private int expectedModCount;
private boolean okToRemove;
LinkedListIterator()
{
}
public boolean hasNext()
{
}
public AnyType next()
{
}
public void remove()
{
}
}
}
import java.util.Iterator;
public interface List<AnyType>
{
void clear();
int size();
boolean isEmpty();
AnyType get(int index);
AnyType set(int index, AnyType newValue);
boolean add(AnyType newValue);
void add(int index, AnyType newValue);
AnyType remove(int index);
Iterator<AnyType> iterator();
}
Explanation / Answer
Assignment1.txt
clear
add 1 San Francisco
size
is_empty
get 1
set 4 Salt Lake City
add 7 San Francisco
add_last Washington, D.C.
remove 5
rotate
print_list
Assignment1.java
package com.list;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
public class Assignment1 {
public static void main(String[] args) {
BufferedReader br=null;
FileInputStream fstream=null;
try {
fstream=new FileInputStream("assignment1.txt");
CircularLinkedList<String> circularLinkedList= new CircularLinkedList<String>();
//BufferedInputStream bInputStream= new BufferedInputStream(fstream);
//bInputStream.
br = new BufferedReader(new InputStreamReader(fstream));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
int i=1;
while (null!=line) {
sb.append(line);
sb.append(System.lineSeparator());
//System.out.println(line);
executeCommand(line,circularLinkedList);
line = br.readLine();
i++;
}
//String everything = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br!=null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void executeCommand(String line, CircularLinkedList<String> circularLinkedList) {
String result=null;
//System.out.println("=============="+line);
String[] commands=line.split(" ");
if("clear".equals(commands[0])){
System.out.println("The circular linked list is now clear");
}
else if("size".equals(commands[0])){
int size=circularLinkedList.size();
System.out.println("The circular linked list contains "+size+" strings.");
}else if("is_empty".equals(commands[0])){
boolean isEmpty=circularLinkedList.isEmpty();
if(isEmpty){
System.out.println("The circular linked list is empty.");
}else{
System.out.println("The circular linked list is not empty.");
}
}
else if("get".equals(commands[0])){
String isEmpty=circularLinkedList.get(Integer.parseInt(commands[1]));
System.out.println("The string at position "+commands[1]+" in the circular linked list is ""+isEmpty+""");
}
else if("set".equals(commands[0])){
result=circularLinkedList.set(Integer.parseInt(commands[1]),commands[2]);
System.out.println("The string "+commands[2]+" replaces the string "+result+ "st position "+commands[1]+" in the circular linked list.");
}
else if("add".equals(commands[0])){
circularLinkedList.add(Integer.parseInt(commands[1]), commands[2]);
System.out.println("Added the string "+commands[2]+" at position "+commands[1]+" in the circular linked list.");
}
else if("add_last".equals(commands[0])){
boolean isEmpty=circularLinkedList.add(commands[1]);
System.out.println("Added the string "+commands[1]+" in the last");
}
else if("remove".equals(commands[0])){
result=circularLinkedList.remove(Integer.parseInt(commands[1]));
System.out.println("Removed the string "+result+" at position "+commands[1]+" from the cicular linked list");
}
else if("rotate".equals(commands[0])){
circularLinkedList.rotate();
System.out.println("The value at the head of the circular linked list was rotated to the tail of the Circular linked list.");
}
else if("print_list".equals(commands[0])){
if(circularLinkedList!=null){
System.out.println("The circular linked list contains:");
Iterator<String> st=circularLinkedList.iterator();
while(st.hasNext()){
System.out.println(st);
}
}else{
System.out.println("The cicular linked list is empty");
}
}
}
}
CircularLinkedList.java
package com.list;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class CircularLinkedList<E> implements List<E> {
private static class Node<E>{
private E data;
private Node<E> next;
public Node(E d,Node<E>n){
setData(d);
setNext(n);
}
public E getData(){
return data;
}
public void setNext(Node<E> n) {
next=n;
}
public void setData(E d) {
data=d;
}
public Node<E> getNext(){
return next;
}
}
private int theSize;
private int modCount;
private Node<E> tail;
public CircularLinkedList() {
}
@Override
public void clear() {
if(size()!=0){
for(int i=size()-1;i>=0;i--){
Node<E> node=getNode(i);
node.next=null;
node.data=null;
}
}
theSize=0;
modCount++;
}
@Override
public int size() {
return theSize;
}
@Override
public boolean isEmpty() {
if(theSize==0){
return true;
}
return false;
}
@Override
public E get(int arg0) {
return null;
}
@Override
public E set(int arg0, E arg1) {
return null;
}
@Override
public boolean add(E newValue) {
add(size(),newValue);
return true;
//No need to implement
//return false;
}
@Override
public void add(int arg0, E arg1) {
theSize++;
}
@Override
public boolean addAll(Collection<? extends E> arg0) {
return false;
}
@Override
public boolean addAll(int arg0, Collection<? extends E> arg1) {
return false;
}
@Override
public boolean contains(Object arg0) {
return false;
}
@Override
public boolean containsAll(Collection<?> arg0) {
return false;
}
@Override
public int indexOf(Object arg0) {
return 0;
}
@Override
public Iterator<E> iterator() {
return new LinkedListIterator();
}
@Override
public int lastIndexOf(Object arg0) {
return 0;
}
@Override
public ListIterator<E> listIterator() {
return null;
}
@Override
public ListIterator<E> listIterator(int arg0) {
return null;
}
@Override
public boolean remove(Object arg0) {
return false;
}
@Override
public E remove(int arg0) {
return null;
}
private Node<E> getNode(int index){
return (getNode(index,0,size()-1));
}
private Node<E> getNode(int index,int lower,int upper){
return null;
}
public void rotate(){
}
@Override
public boolean removeAll(Collection<?> arg0) {
return false;
}
@Override
public boolean retainAll(Collection<?> arg0) {
return false;
}
@Override
public List<E> subList(int arg0, int arg1) {
return null;
}
@Override
public Object[] toArray() {
return null;
}
@Override
public <T> T[] toArray(T[] arg0) {
return null;
}
private class LinkedListIterator implements Iterator<E>{
private Node<E> previous;
private Node<E> current;
private int expectedModCount;
private boolean okToRemove;
public LinkedListIterator() {
}
@Override
public boolean hasNext() {
return false;
}
@Override
public E next() {
return null;
}
public void remove(){
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.