Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

create a doubly-linked array list with a Node class that contains the methods: p

ID: 3891256 • Letter: C

Question

create a doubly-linked array list with a Node class that contains the methods:

public node(int v) – class constructor, v = data value

public node( ) – class constructor

public int get( ) – returns the data value from the node

public void set(int v) – saves the data value v in the node

and the the doubly-linked array class with the methods

public BetterArray(int n) – class constructor, n is the initial allocation (the starting array size).

public BetterArray( ) – class constructor, use default initial allocation

public int getSize( ) – returns the current size of the array

public int get(int index) – uses the index to access a specific array element (node) and returns the value to the caller.

public void put(int value, int index) – uses the index to find the specific array element (using index) and saves the data value in the node. If the requested node is outside the current size of the array (linked-list), expand the list to accommodate the new element.

public void insertBefore(int value, int index) – uses the index to find the specific array element (node) and saves the data value in the preceding node. If the requested node is outside the current size of the array (linked-list), expand the list to accommodate the new element.

public void insertAfter(int value, int index) – uses the index to find the specific array element (node) and saves the data value in the following node. If the requested node is outside the current size of the array (linked-list), expand the list to accommodate the new element.

public void remove(int index) – uses the index to find the specific array element (node) and deletes it from the list.

Explanation / Answer

import java.util.Arrays;

//double linked list class

public class DoubleLinkedList {

private static class node {

  

//nextptr points to next element address and preptr points to previous element address

int data;

node nextptr;

node prevptr;

//constuctor

node(node prevptr, int v, node nextptr) {

this.prevptr = prevptr;

this.data = v;

this.nextptr = nextptr;

}

}

private final node firstelement;

private final node lastelement;

private int size = 0;

//crating header and footer nodes

public DoubleLinkedList() {

firstelement = new node (null,0, null); // header node, data ignored

lastelement = new node (null, 0, null); // footer node, data ignored

firstelement.nextptr = lastelement;

lastelement.prevptr = firstelement;

}

/**

* @param index: -1 to size-1 (-1 = header indexition)

* @return

*/

  

//this is for move up to position

private node moveUpintoindexition(int index) {

node n = firstelement;

for (int i = 0; i <= index; ++i) {

n = n.nextptr;

}

return n;

}

//this function is to add the element

public void add(int index, int v) {

if (index < 0 || index > size) {

throw new IndexOutOfBoundsException(index + " is out of bounds");

}

node n = moveUpintoindexition(index-1);

node newnode = new node (null, v, null);

newnode.prevptr = n;

newnode.nextptr = n.nextptr;

n.nextptr.prevptr = newnode;

n.nextptr = newnode;

size++;

}

//to add in the first poisition

public void addfirstelement(int v) {

add(0, v);

}

//this is to add the element to last

public void addlastelement(int v) {

node n = lastelement.prevptr;

node newnode = new node (null, v, null);

newnode.prevptr = n;

newnode.nextptr = n.nextptr;

n.nextptr.prevptr = newnode;

n.nextptr = newnode;

size++;

}

//adding the element

public boolean add(int v) {

addlastelement(v);

return true;

}

//remove the first element of the linked list and make the second element as head node

public int removefirstelement() {

if (size == 0) {

throw new java.util.NoSuchElementException();

}

node n = firstelement.nextptr;

int data = n.data;

n.nextptr.prevptr = firstelement;

firstelement.nextptr = n.nextptr;

--size;

return data;

}

//remove last element

public int removelastelement() {

if (size == 0) {

throw new java.util.NoSuchElementException();

}

node n = lastelement.prevptr;

int data = n.data;

n.prevptr.nextptr = lastelement;

lastelement.prevptr = n.prevptr;

--size;

return data;

}

//the get the node of the particular index

public int get(int index) {

if (index < 0 || index > size-1) {

throw new IndexOutOfBoundsException(index + " is out of bounds");

}

node n = moveUpintoindexition(index);

return n.data;

}

//put the particular value in the particular index

public int put(int index, int v) {

if (index < 0 || index > size-1) {

throw new IndexOutOfBoundsException(index + " is out of bounds");

}

node n = moveUpintoindexition(index);

int prevptr = n.data;

n.data = v;

return prevptr;

}

//converting linkedlist to array

public Object[] toArray() {

Object data[] = new Object[size];

node n = firstelement;

for (int i = 0; i < size; ++i) {

n = n.nextptr;

data[i] = n.data;

}

return data;

}

@Override

public String toString() {

return Arrays.toString(this.toArray());

}

  

//this is main driver function to test my all functions

//i have test only only function all are working perfectly

public static void main(String args[]){

DoubleLinkedList obj=new DoubleLinkedList();

//intesting my function You can test all my functions

//I am intesting only one function

System.out.print("Just intesting my Funtions ");

obj.add(4);

System.out.println(obj.get(0));

}

}