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 list.
•Overload the method add by a version of it that takes two parameters,index and value, and adds value at the location specified by index. If index is out-of-bound, throw the appropriate exception. (Hint: Note that you should be able to add a value at index of size.)
•Implement the method addAlternativethat takes a value and adds it after each already existing value in the list. If the list is empty, it should add the value as the first (and only)value in the list. Therefore, after calling it, if the list was empty it will have a size of 1, and if the list was not empty, then its new size is twice its previous size (every other element’s value is equal to the newly added value).
•Implement the method addAll that takes an index and an arbitrary number of values and adds those values at the given index. Note that you can get arbitrary an number of values by specifying “int... values” as the second argument in your method. Then, inside your method, you can treat values as an array of ints.
•Implement the method equals that takes another DoublyLinkedList and returns true if all element values in the current list and the given list are equal to each other, respectively.
•Implement the method toStringReverse that returns a String that is similar to the output of the toString method, but lists the values in the list in reverse order.
DoublyLinkedList.java code:
public class DoublyLinkedList {
private Node front; // first element in the
list
private Node last; // last element in the
list
private int size; // number of elements in the
list
// post: constructs an empty list
public DoublyLinkedList() {
front = last =
null;
}
// post: constructs a list with data in first
element and "list" after that
public DoublyLinkedList(int data,
DoublyLinkedList list) {
front = new Node(data);
front.next = list.front;
last = list.last;
}
// post: returns the integer at the given index
in the list
public int get(int index) {
checkIndex(index);
return
nodeAt(index).data;
}
// post: returns the position of the first
occurrence of the given value (-1 if not found)
public int indexOf(int value) {
int index = 0;
Node current =
front;
while (current != null)
{
if (current.data == value) {
return index;
}
index++;
current = current.next;
}
return -1;
}
// post: appends the given value to the end
of the list
public void add(int value) {
if (last != null) {
last.next = new Node(value);
last
= last.next;
}
else {
front = last =
new Node(value);
}
}
// post: removes value at the given
index
public void remove(int index) {
checkIndex(index);
if (index == 0) {
front = front.next;
} else {
Node current = nodeAt(index - 1);
current.next = current.next.next;
}
}
// post: returns a reference to the node at
the given index
private Node nodeAt(int index) {
checkIndex(index);
Node current =
front;
for (int i = 0; i <
index; i++) {
current = current.next;
}
return current;
}
// checks if index is out of bound and if it is
throws IndexOutOfBoundsException
private void checkIndex(int index) {
if (index < 0 ||
index >= size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: "
+ size);
}
// post: creates a comma-separated, bracketed
version of the list
@Override
public String toString() {
if (front == null)
{
return "[]";
} else {
String result = "[" + front.data;
Node current = front.next;
while (current != null) {
result += ", " + current.data;
current = current.next;
}
result += "]";
return result;
}
}
// Node inner class containing the data, and
next and previous references
private static class Node {
Node previous;
int data;
Node next;
// constructor to create
a Node with data and null previous and next references
public Node(int data)
{
this.previous = this.next = null;
this.data = data;
}
// constructor to add
a new Node with data before another "next" node
public Node(int data,
Node next) {
this.previous = null;
this.data = data;
this.next = next;
if (next != null)
next.previous = this;
}
// constructor to
initialize a new Node with data between its "previous" and "next"
node
public Node(Node
previous, int data, Node next) {
this.previous = previous;
this.data = data;
this.next = next;
if (previous != null)
previous.next = this;
if (next != null)
next.previous = this;
}
}
}
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package printerjob;
public class DoublyLinkedList {
private Node front; // first element in the
list
private Node last; // last element in the
list
private int size; // number of elements in the
list
// post: constructs an empty list
public DoublyLinkedList() {
front = last =
null;
size=10;//default size
is 10
}
// post: constructs a list with data in first
element and "list" after that
public DoublyLinkedList(int data,
DoublyLinkedList list) {
front = new Node(data);
front.next = list.front;
last = list.last;
}
// post: returns the integer at the given index
in the list
public int get(int index) {
checkIndex(index);
return
nodeAt(index).data;
}
// post: returns the position of the first
occurrence of the given value (-1 if not found)
public int indexOf(int value) {
int index = 0;
Node current =
front;
while (current != null)
{
if (current.data == value) {
return index;
}
index++;
current = current.next;
}
return -1;
}
// post: appends the given value to the end
of the list
public void add(int value) {
if (last != null) {
last.next = new Node(value);
last = last.next;
}
else {
front
= last = new Node(value);
}
}
//inserted node with index and value
public void add(int index,int value){
Node
tempfront=front;
int count=0;
while(tempfront!=null){
if(count==index){
Node N=new Node(value);
N.data=value;
Node t=tempfront.next;
tempfront.next=N;
N.previous=tempfront;
N.next=t;
t.previous=N;
}
count++;
tempfront=tempfront.next;
}
}
//alter node after
public void addAlternative(Node prev_Node, int
new_data){
if (prev_Node == null)
{
System.out.println("The
given previous node cannot be NULL ");
return;
}
Node new_node = new Node(new_data);
new_node.next = prev_Node.next;
prev_Node.next = new_node;
new_node.previous = prev_Node;
if (new_node.next != null)
new_node.next.previous =
new_node;
}
// add random element
public void addAll(int index){
Node
tempfront=front;
int count=0;
while(tempfront!=null){
if(count==index){
Node N=new Node((int)Math.random());
Node t=tempfront.next;
tempfront.next=N;
N.previous=tempfront;
N.next=t;
t.previous=N;
}
count++;
tempfront=tempfront.next;
}
}
// check equal of two linkedlist
public void equals(Node front1,Node
front2){
int fl=0;
while (front1 !=
null && front2 != null) {
if(front1.data != front2.data) {
fl=1;
break;
}
front1 = front1.next;
front2 = front2.next;
}
if(front1 != null
|| front2 != null){
fl=1;
}
if(fl==0)
System.out.println("Equals");
else
System.out.println("Not Equals");
}
//print doubly list in reverse order
public void Reverse(Node last){
System.out.println("Traversal in reverse
direction");
while (last != null)
{
System.out.print(last.data + " ");
last = last.previous;
}
}
// post: removes value at the given index
public void remove(int index) {
checkIndex(index);
if (index == 0) {
front = front.next;
} else {
Node current = nodeAt(index - 1);
current.next = current.next.next;
}
}
// post: returns a reference to the node at
the given index
private Node nodeAt(int index) {
checkIndex(index);
Node current =
front;
for (int i = 0; i <
index; i++) {
current = current.next;
}
return current;
}
// checks if index is out of bound and if it is
throws IndexOutOfBoundsException
private void checkIndex(int index) {
if (index < 0 ||
index >= size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: "
+ size);
}
// post: creates a comma-separated, bracketed
version of the list
@Override
public String toString() {
if (front == null)
{
return "[]";
} else {
String result = "[" + front.data;
Node current = front.next;
while (current != null) {
result += ", " + current.data;
current = current.next;
}
result += "]";
return result;
}
}
// Node inner class containing the data, and
next and previous references
private static class Node {
Node previous;
int data;
Node next;
// constructor to create
a Node with data and null previous and next references
public Node(int data)
{
this.previous = this.next = null;
this.data = data;
}
// constructor to add
a new Node with data before another "next" node
public Node(int data,
Node next) {
this.previous = null;
this.data = data;
this.next = next;
if (next != null)
next.previous = this;
}
// constructor to
initialize a new Node with data between its "previous" and "next"
node
public Node(Node
previous, int data, Node next) {
this.previous = previous;
this.data = data;
this.next = next;
if (previous != null)
previous.next = this;
if (next != null)
next.previous = this;
}
}
}
Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...
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...
Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...
Hi! Can someone can convert this java code to c++. ASAP thanks I will thumbs up Here's the code: package lists; import bookdata.*; public class DoublyLinkedList { int size; //Variable que define el tamano de la lista. Node head, tail; //Nodos que definen el Head y Tail en la lista. //Constructor public DoublyLinkedList(){ this.head = null; this.tail = null; this.size = 0; } //Insert a new book in alphabetic order. public void insert(Book nb){ //Creamos uno nuevo nodo con el...
JAVA
Submit: HashSet.java
with the following methods added:
HashSet.java
// Implements a set of objects using a hash table.
// The hash table uses separate chaining to resolve
collisions.
// Original from buildingjavaprograms.com supplements
public class HashSet<E> {
private static final double MAX_LOAD_FACTOR = 0.75;
private HashEntry<E>[] elementData;
private int size;
// Constructs an empty set.
@SuppressWarnings("unchecked")
public HashSet() {
elementData = new HashEntry[10];
size = 0;
}
// ADD METHODS HERE for exercise solutions:
// Adds the...
Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a Circular Doubly Linked List and am having serious trouble creating a method retainAll. Here's the code, and my attempt. Initialization: public class CDoublyLinkedList { private class Node { private Object data; //Assume data implemented Comparable private Node next, prev; private Node(Object data, Node pref, Node next) { this.data = data; ...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...
solve this Q in java languege Write a method that return DoublyLinkedList as reversed string For example: If the elements of a list is 1, 2, 3, 4, 5, 6 the reverse string should be 6, 5, 4, 3, 2, 1 implement reverse method you have two steps: 1- you should start traversing from the last element of DoublyLinkedList (the previous of the trailer) 2- you should add the element inside each node to string don't forget the space in...
Language: Java Topic: Doubly Linked Lists Write a method that removes and returns the last copy of the given data from the list. Do not return the same data that was passed in. Return the data that was stored in the list. Must be O(1) if data is in the tail and O(n) for all other cases. * @param data the data to be removed from the list * @return the data that was removed * @throws java.lang.IllegalArgumentException if...