In: Computer Science
Can you fix this code please. the removing methods id no doing anything. this is java code
import java.util.NoSuchElementException;
public class DoublyLinkedList<E> {
public int size;
public Node head;
public Node tail;
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public int getSize() {
return 0;
}
@Override
public void addAtFront(E element) {
Node<E> temp = new Node<E>(element, head, null);
if(head != null) {
head.prev =
temp;
}
head = temp;
if (tail == null) {
tail =
temp;
}
size++;
}
@Override
public void addAtBack(E element) {
Node temp = new
Node(element);
Node prev ;
if(isEmpty()) {
head =
temp;
}
else {
tail.next =
temp;
temp.prev =
tail;
}
tail = temp;
size++;
}
@Override
public void addAtLocation(E newNode, int location)
{
Node node = new Node(newNode);
node.data = newNode;
Node previous = head;
int counter = 1;
while(counter < location -1)
{
previous =
previous.next;
counter++;
}
Node current = previous.next;
node.next = current;
previous.next = node;
node.prev = previous;
if(current != null) {
current.prev =
node;
}
size++;
}
@Override
public void removeFromFront() {
if(isEmpty()) {
throw new
NoSuchElementException();
}
Node temp = head;
if(head == tail) {
tail =
null;
}
else
{
head.next.prev =
null;
}
head = head.next;
temp.next = null;
}
@Override
public void removeFromBack() {
if (tail == null) {
System.out.println("is emty");
}
else
{
Node temp = tail.prev;
tail.next = null;
tail = temp;
}
}
@Override
public void removeFromLocation(int location) {
Node previous = head;
int counter = 1;
System.out.print("\nthe deleted
element in any location is: ");
while(counter < location -1)
{
previous =
previous.next;
counter++;
}
Node current =
previous.next;
previous.next =
current.next;
System.out.println(current.data);
current.next =
null;
}
@Override
public void printForward() {
if (tail == null) {
System.out.println("is emty");
}
else {
Node temp = head;
System.out.print("\nthis is
traverse Forward: ");
while (temp != null){
System.out.print(" " + temp.element);
temp =
temp.next;
}
}
}
@Override
public void printBackward() {
if (tail == null) {
System.out.println("is emty");
}
else
{
Node node = tail;
System.out.print("\nthis is
traverse Backward: ");
while (node != null) {
System.out.print(" " + node.element);
node =
node.prev;
}
}
}
public void show1() {
Node temp = head;
if(tail == null) {
System.out.println("it is
empty");
}
else
System.out.print("\nThis is the
Doubly LinkList is: ");
do {
System.out.print(" " + temp.element);
temp =
temp.next;
}
while (temp != null);
}
}
this is node class
public class Node<E> {
E element;
Node next;
E data;
Node prev;
public Node previous;
public Node(E element, Node next, Node prev) {
this.element = element;
this.next = next;
this.prev = prev;
}
public Node(E element) {
this.element = element;
}
}
/***********************Node.java*********************/
/**
* The Class Node.
*
* @param <E> the element type
*/
public class Node<E> {
/** The element. */
E element;
/** The next. */
Node<E> next;
/** The data. */
E data;
/** The prev. */
Node<E> prev;
/** The previous. */
public Node<E> previous;
/**
* Instantiates a new node.
*
* @param element the element
* @param next the next
* @param prev the prev
*/
public Node(E element, Node<E> next,
Node<E> prev) {
this.element = element;
this.next = next;
this.prev = prev;
}
/**
* Instantiates a new node.
*
* @param element the element
*/
public Node(E element) {
this.element = element;
}
}
/********************************DoublyLinkedList.java**********************/
import java.util.NoSuchElementException;
/**
* The Class DoublyLinkedList.
*
* @param <E> the element type
*/
public class DoublyLinkedList<E> {
/** The size. */
public int size;
/** The head. */
public Node<E> head;
/** The tail. */
public Node<E> tail;
/**
* Checks if is empty.
*
* @return true, if is empty
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Gets the size.
*
* @return the size
*/
public int getSize() {
return 0;
}
/**
* Adds the at front.
*
* @param element the element
*/
public void addAtFront(E element) {
Node<E> temp = new Node<E>(element, head, null);
if (head != null) {
head.prev = temp;
}
head = temp;
if (tail == null) {
tail =
temp;
}
size++;
}
/**
* Adds the at back.
*
* @param element the element
*/
public void addAtBack(E element) {
Node<E> temp = new Node<E>(element);
Node<E> prev;
if (isEmpty()) {
head =
temp;
}
else {
tail.next = temp;
temp.prev =
tail;
}
tail = temp;
size++;
}
/**
* Adds the at location.
*
* @param newNode the new node
* @param location the location
*/
public void addAtLocation(E newNode, int location)
{
Node<E> node = new Node<E>(newNode);
node.data = newNode;
Node<E> previous = head;
int counter = 1;
while (counter < location - 1) {
previous = previous.next;
counter++;
}
Node<E> current = previous.next;
node.next = current;
previous.next = node;
node.prev = previous;
if (current != null) {
current.prev
= node;
}
size++;
}
/**
* Removes the from front.
*/
public void removeFromFront() {
if (isEmpty()) {
throw new
NoSuchElementException();
}
Node<E> temp = head;
if (head == tail) {
tail =
null;
} else {
head.next.prev =
null;
}
head = head.next;
temp.next = null;
}
/**
* Removes the from back.
*/
public void removeFromBack() {
if (tail == null) {
System.out.println("is emty");
}
else
{
Node<E> temp = tail.prev;
tail.next =
null;
tail = temp;
}
}
/**
* Removes the from location.
*
* @param location the location
*/
public void removeFromLocation(int location) {
Node<E> previous =
head;
int counter = 1;
System.out.print("\nthe deleted element in any location is: ");
while (counter < location - 1) {
previous = previous.next;
counter++;
}
Node<E> current = previous.next;
previous.next =
current.next;
System.out.println(current.data);
current.next = null;
}
/**
* Prints the forward.
*/
public void printForward() {
if (tail == null) {
System.out.println("is emty");
}
else {
Node<E> temp = head;
System.out.print("\nthis is traverse Forward: ");
while (temp != null) {
System.out.print(" " + temp.element);
temp = temp.next;
}
}
}
/**
* Prints the backward.
*/
public void printBackward() {
if (tail == null) {
System.out.println("is emty");
} else {
Node<E> node = tail;
System.out.print("\nthis is traverse Backward: ");
while (node != null) {
System.out.print(" " + node.element);
node = node.prev;
}
}
}
/**
* Show 1.
*/
public void show1() {
Node<E> temp = head;
if (tail == null) {
System.out.println("it is empty");
}
else
System.out.print("\nThis is the Doubly LinkList is: ");
do {
System.out.print(" " + temp.element);
temp = temp.next;
} while (temp != null);
}
/**
* The main method.
*
* @param a the arguments
*/
public static void main(String a[]){
DoublyLinkedList<Integer> doublyLinkedList = new
DoublyLinkedList<Integer>();
doublyLinkedList.addAtFront(10);
doublyLinkedList.addAtFront(34);
doublyLinkedList.addAtBack(57);
doublyLinkedList.addAtBack(353);
doublyLinkedList.printForward();
doublyLinkedList.removeFromFront();
doublyLinkedList.removeFromBack();
doublyLinkedList.printBackward();
}
}
/*********************output*******************/
this is traverse Forward: 34 10 57 353
this is traverse Backward: 57 10
Please let me know if you have any doubt or modify the answer, Thanks :)