Part I:
Create a doubly linked circular list class named LinkedItemList that implements the following interface:
/**
* An ordered list of items.
*/
public interface ItemList<E> {
/**
* Append an item to the end of the list
*
* @param item – item to be appended
*/
public void append(E item);
/**
* Insert an item at a specified index position
*
* @param item – item to be inserted
* @param index – index position where to insert the item
* @throw IndexOutOfBoundsException if index is < 0 or > no of items
*/
public void insert(E item, int index);
/**
* Return an item at a specified index
*
* @param index – index of the item to return
* @return the item at the specified index
* @throw IndexOutOfBoundsException if index is < 0 or >= no of items
*/
public E get(int index);
/**
* Remove an item at a specified index
*
* @param index – index of the item to be removed
* @return the removed item
* @throw IndexOutOfBoundsException if index is < 0 or >= no of items
*/
public E removeAtIndex (int index);
/**
* Return the number of items currently in the list
*
* @return the number of items in the list
*/
public int size();
/**
* Determine if the list is empty
*
* @return true if the list is empty, otherwise false
*/
public boolean isEmpty();
/**
* Clear the list. The list becomes empty
*
*/
public void clear();
}
You will need the appropriate constructors. In addition, you may also implement other methods that may be useful for the assignment (where possible define code in just one place).
Part II:
Write JUnit test program to thoroughly test all of the methods of your LinkedItemList class
Additional Requirements:
The LinkedItemList must be implemented as a doubly linked circular list with a dummy node in place of a first node and last node reference variables.
Provide a fast implementation for append(), e.g., does not need to walk the list to get to the end.
Define and use a private method appendAfter(Node node, E item) which adds a Node to the linked list for the item. Call appendAfter from the append and insert methods. The append and insert methods must not directly create a node.
Define and use a private method removeNode(Node node) which removes a Node from the linked list. Call removeNode from the removeAtIndex method. The removeAtIndex method must not directly remove a node.
Define and use a private method getNode(int index) which returns a node at a given index position. Use getNode in the get, removeAtIndex, and insert methods
Provide a fast implementation for size(), e.g., does not need to walk the list to count the number of items
==========>ItemList<E>.java
package doublelnkdlst;
public interface ItemList<E> {
public void clean();
public void insrt(E item, int index);
public bool Empty();
public E clearAtIndex(int index);
public void apnd(E item);
public int size();
public E get(int index);
}
=======>LinkedItemList<E>.java
package doublelnkdlst;
public class LinkedItemList<E> implements ItemList<E> {
private Node head;
private Node tail;
private int size;
private class Node {
E item;
Node next;
Node prev;
public Node(E item, Node next, Node prev) {
this.item = item;
this.next = next;
this.prev = prev;
}
}
@Override
public void apnd(E item) {
Node tmp = new Node(item, head, null);
if (head != null) {
head.prev = tmp;
}
head = tmp;
if (tail == null) {
tail = tmp;
}
size++;
}
@Override
public void insrt(E item, int index) {
if (index < 0 && index >= size)
throw new IndexOutOfBoundsException();
Node current = head;
Node previous = null;
while (index >= 0) {
previous = current;
current = current.next;
index--;
}
Node node = new Node(item, head, null);
node.next = current;
node.prev = previous;
current.prev = node;
if (previous != null) {
previous.next = node;
}
if (index == 1)
head = node;
}
@Override
public E get(int index) {
if (index < 0 && index >= size)
throw new IndexOutOfBoundsException();
Node temp = head;
while (index > 0) {
temp = temp.next;
index--;
}
return temp.item;
}
@Override
public E clearAtIndex(int index) {
if (index < 0 && index >= size)
throw new IndexOutOfBoundsException();
Node current = head;
Node previous = null;
while (index > 0) {
previous = current;
current = current.next;
index--;
}
if (previous != null) {
previous.next = current.next;
if (current.next != null)
current.next.prev = previous;
}
else {
head = current.next;
head.prev = null;
}
return current.item;
}
@Override
public int size() {
return size;
}
@Override
public bool Empty() {
if (size == 0) {
return true;
}
return false;
}
@Override
public void clean() {
head = null;
size = 0;
}
}
============>Junit test case class
package doublelnkdlst;
import org.junit.runners.JUnit4;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(JUnit4.class)
public class TestLinkedItemList {
@Test
public void testInsrtAndGet() {
ItemList<Integer> list = new LinkedItemList<>();
list.apnd(1);
list.apnd(2);
list.apnd(3);
Assert.assertEquals(list.get(0), Integer.valueOf(3));
Assert.assertEquals(list.get(1), Integer.valueOf(2));
Assert.assertEquals(list.get(2), Integer.valueOf(1));
}
@Test
public void testclearAtIndex() {
ItemList<Integer> list = new LinkedItemList<>();
list.apnd(1);
list.apnd(2);
list.apnd(3);
Assert.assertEquals(list.clearAtIndex(0), Integer.valueOf(3));
Assert.assertEquals(list.clearAtIndex(1), Integer.valueOf(1));
}
@Test
public void testCleanAllAndEmpty() {
ItemList<Integer> list = new LinkedItemList<>();
list.apnd(1);
list.apnd(2);
list.apnd(3);
list.clean();
Assert.assertEquals(Bool.valueOf(list.Empty()), Bool.valueOf(true));
}}
Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size) { this.size = size; } /** Reference to list head. */ private Node<E> head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E...
This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...
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...
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...
Create a complete LinkedList class which implements all of the methods listed below using dynamic memory allocation. You will need a Node class to represent each node in the list. The list should store String values. Include a main method which tests all the methods of your list. Also answer these questions: What is the time complexity (using big-O notation) of these operations in your linked list? add(String val) add(int index, String val) get(int index) remove(String val) Submit three files:...
Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...
C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...
JAVA - Circular Doubly Linked
List
Does anybody could help me with this method below(previous)?
public E previous() {
// Returns the previous Element
return null;
}
Explanation:
We have this class with these two implemented
inferfaces:
The interfaces are:
package edu.ics211.h04;
/**
* Interface for a List211.
*
* @author Cam Moore
* @param the generic type of the Lists.
*/
public interface IList211 {
/**
* Gets the item at the given index.
* @param index the index....
Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...
Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...