Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA: Please help me change this DoubleLinkedList into a Circular Double LinkedList. import java.util.*; public class DoubleLinkedList { private static class Node { private E

JAVA:

Please help me change this DoubleLinkedList into a Circular Double LinkedList.

import java.util.*;

public class DoubleLinkedList {

private static class Node {

private E data; private Node next = null; private Node prev = null;

public Node(E data, Node next,Node prev) { this.data = data; this.next = next; this.prev = prev; }

public Node(E data) { this(data, null,null); } }

private Node head = null;

private int size = 0;

private void addFirst(E item) { head = new Node(item, head,null); size++; } private void addAfter(Node node, E item) { if(node == null) { return; } Node curr= node; Node newNode= new Node(item, null,null); Node next= curr.next; curr.next= newNode; newNode.prev= curr; newNode.next = next; if(next!=null) { next.prev = newNode; } size++; }

private E removeFirst() { Node temp = head; if (head != null) { head = head.next; if(head != null){ head.prev = null; } } if (temp != null) { size--; return temp.data; } else { return null; } }

private E removeAfter(Node node) { Node temp = node.next; if (temp != null) { node.next = temp.next; if(node.next != null){ node.next.prev = node; } size--; return temp.data; } else { return null; } } private Node getNode(int index) { Node node = head; for (int i = 0; i < index && node != null; i++) { node = node.next; } return node; }

public E get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node node = getNode(index); return node.data; }

public E set(int index, E newValue) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node node = getNode(index); E result = node.data; node.data = newValue; return result; }

public void add(int index, E item) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if (index == 0) { addFirst(item); } else { Node node = getNode(index - 1); addAfter(node, item); } }

/** * Append the specified item to the end of the list * @param item The item to be appended * @returns true (as specified by the Collection interface) */ public boolean add(E item) { add(size, item); return true; }

// Insert solution to programming exercise 1, section 5, chapter 2 here

/** * Query the size of the list * @return The number of objects in the list */ int size() { return size; }

/** * Obtain a string representation of the list * @return A String representation of the list */ @Override public String toString() { StringBuilder sb = new StringBuilder("["); Node p = head; if (p != null) { while (p.next != null) { sb.append(p.data.toString()); sb.append(", "); p = p.next; } sb.append(p.data.toString()); } sb.append("]"); return sb.toString(); }

public static void main(String [] args) { DoubleLinkedList list = new DoubleLinkedList(); list.add("Tom"); list.add("Mike"); list.add("Dave"); //list.add(0, "Bill");

//list.addAfter(list.getNode(2), "Peter"); //list.removeFirst(); list.removeAfter(list.getNode(3)); //System.out.println("The list is: " + list.size() + " long!"); //System.out.println(list); System.out.println(list); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems Uropean Conference Adbis 2020 Lyon France August 25 27 2020 Proceedings Lncs 12245

Authors: Jerome Darmont ,Boris Novikov ,Robert Wrembel

1st Edition

3030548317, 978-3030548315

More Books

Students also viewed these Databases questions