This code is practice for using Doubly Linked Lists using Generics in Java. Could someone show me how I'd complete the 'public void add(int index, T value)' method? It it supposed to add a value of a generic type to a certain index in the doubly linked list.
========================
LinkedList Class
public class LinkedList<T> extends List<T>{
private Node head;
private Node tail;
private int size;
public LinkedList() {
head = null;
tail = null;
size = 0;
}
public Node getHead() {
return head;
}
public Node getTail() {
return tail;
}
public boolean add(T value) {
Node newNode = new Node(value);
if (isEmpty()) {
head = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
}
tail = newNode;
size++;
return true;
}
public void add(int index, T value) { **
}
public String toString() {
String str = "[";
if (head == null) {
return null;
}
Node temp = head;
while (temp != tail) {
str += temp.value + ", ";
temp = temp.next;
}
str += temp.value + "]";
return str;
}
public void clear() {
head = null;
}
public T get(int index) {
return null;
}
public boolean contains(Object o) {
return true;
}
public boolean isEmpty() {
return size == 0;
}
public T remove(int index) {
return null;
}
public boolean remove(Object o) {
size--;
return true;
}
public int size() {
return size;
}
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.add(1);
ll.add(2);
ll.add(3);
System.out.println(ll.toString());
}
}
=======================
List Class
/**
* Specification for a List ADT.
* @param <T> Type
*/
public abstract class List<T> {
/**
* Appends the specified value to the end of this list.
*
* @param value T The value to add
* @return boolean True if the value is inserted, false otherwise
*/
abstract boolean add(T value);
/**
* Inserts the specified value at the specified position in this list.
*
* @param index Integer The index at which to insert
* @param value T The value to insert
*/
abstract void add(int index, T value);
/**
* Removes all of the elements from this list.
*/
abstract void clear();
/**
* Returns true if this list contains the specified element.
*
* @param o Object The element to check if present in the list
* @return boolean
*/
abstract boolean contains(Object o);
/**
* Returns the element at the specified position in this list.
*
* @param index Integer The index at which to insert
* @return T
*/
abstract T get(int index);
/**
* Removes the element at the specified position in this list.
* Returns the element from the list or null if index is invalid.
*
* @param index the index of the element to be removed
* @return the element previously at the specified position or null
*/
abstract T remove(int index);
/**
* Removes the first occurrence of the specified element from this
* list, if it is present.
* If this list does not contain the element, it is unchanged.
* Returns true if this list contained the specified element
* (or equivalently, if this list changed as a result of the call).
*
* @param o element to be removed from this list, if present
* @return true if this list contained the specified element
*/
abstract boolean remove(Object o);
/**
* Returns true if this list contains no elements.
*
* @return true if this list contains no elements
*/
abstract boolean isEmpty();
/**
* Returns the number of elements in this list.
* @return int
*/
abstract int size();
/**
* Inner class to represent a List node.
*/
public class Node {
T value;
Node prev;
Node next;
/**
* Constructor.
*
* @param value V The value
*/
public Node(T value) {
this.value = value;
this.prev = null;
this.next = null;
}
}
}
//List.java
/**
* Specification for a List ADT.
* @param <T> Type
*/
public abstract class List<T> {
/**
* Appends the specified value to the end of this list.
*
* @param value T The value to add
* @return boolean True if the value is inserted, false otherwise
*/
abstract boolean add(T value);
/**
* Inserts the specified value at the specified position in this list.
*
* @param index Integer The index at which to insert
* @param value T The value to insert
*/
abstract void add(int index, T value);
/**
* Removes all of the elements from this list.
*/
abstract void clear();
/**
* Returns true if this list contains the specified element.
*
* @param o Object The element to check if present in the list
* @return boolean
*/
abstract boolean contains(Object o);
/**
* Returns the element at the specified position in this list.
*
* @param index Integer The index at which to insert
* @return T
*/
abstract T get(int index);
/**
* Removes the element at the specified position in this list.
* Returns the element from the list or null if index is invalid.
*
* @param index the index of the element to be removed
* @return the element previously at the specified position or null
*/
abstract T remove(int index);
/**
* Removes the first occurrence of the specified element from this
* list, if it is present.
* If this list does not contain the element, it is unchanged.
* Returns true if this list contained the specified element
* (or equivalently, if this list changed as a result of the call).
*
* @param o element to be removed from this list, if present
* @return true if this list contained the specified element
*/
abstract boolean remove(Object o);
/**
* Returns true if this list contains no elements.
*
* @return true if this list contains no elements
*/
abstract boolean isEmpty();
/**
* Returns the number of elements in this list.
* @return int
*/
abstract int size();
/**
* Inner class to represent a List node.
*/
public class Node {
T value;
Node prev;
Node next;
/**
* Constructor.
*
* @param value V The value
*/
public Node(T value) {
this.value = value;
this.prev = null;
this.next = null;
}
}
}
//end of List.java
//LinkedList.java
public class LinkedList<T> extends List<T>{
private Node head;
private Node tail;
private int size;
public LinkedList() {
head = null;
tail = null;
size = 0;
}
public Node getHead() {
return head;
}
public Node getTail() {
return tail;
}
public boolean add(T value) {
Node newNode = new Node(value);
if (isEmpty()) {
head = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
}
tail = newNode;
size++;
return true;
}
public void add(int index, T value) {
Node newNode = new Node(value);
if(index < 0 || index > size)
return;
else if(index == 0)
{
newNode.next = head;
head = newNode;
if(head.next == null)
tail = newNode;
size++;
}else if(index == size)
{
add(value);
}else {
int i=0;
Node curr = head;
while(i < index)
{
curr = curr.next;
i++;
}
newNode.next = curr;
curr.prev.next = newNode;
newNode.prev = curr.prev;
curr.prev = newNode;
size++;
}
}
public String toString() {
String str = "[";
if (head == null) {
return null;
}
Node temp = head;
while (temp != tail) {
str += temp.value + ", ";
temp = temp.next;
}
str += temp.value + "]";
return str;
}
public void clear() {
head = null;
}
public T get(int index) {
return null;
}
public boolean contains(Object o) {
return true;
}
public boolean isEmpty() {
return size == 0;
}
public T remove(int index) {
return null;
}
public boolean remove(Object o) {
size--;
return true;
}
public int size() {
return size;
}
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.add(1);
ll.add(2);
ll.add(3);
ll.add(1, 4);
ll.add(0,5);
ll.add(5,6);
System.out.println(ll.toString());
}
}
//end of LinkedList.java
Output:

This code is practice for using Doubly Linked Lists using Generics in Java. Could someone show...