How can I modify the following Linked List code to become a Circular Doubly Link
ID: 3728656 • Letter: H
Question
How can I modify the following Linked List code to become a Circular Doubly Linked List?
public class LinkedList
{
public Node head;
public LinkedList()
{
head = null;
}
public void insert(int value)
{
Node newNode = new Node();
newNode.data = value;
newNode.next = head;
head = newNode;
}
public void append(int value)
{
Node newNode = new Node();
newNode.data = value;
newNode.next = null;
if (head == null)
head = newNode;
else
{
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = newNode;
}
}
public Node search(int value)
{
Node temp = head;
while (temp != null)
{
if (temp.data == value)
return temp;
temp = temp.next;
}
return null;
}
public boolean update(int oldValue, int newValue)
{
Node temp = search(oldValue);
if (temp == null)
return false;
temp.data = newValue;
return true;
}
public boolean delete(int value)
{
Node current = head;
// Situation #1
if (current == null)
return false;
// Situation #2
if (current.data == value)
{
head = current.next;
current = null;
return true;
}
// Final situations
current = head.next;
Node previous = head;
while (current != null)
{
if (current.data == value)
{
previous.next = current.next;
current = null;
return true;
}
current = current.next;
previous = previous.next;
}
return false;
}
public String toString()
{
String output = "";
Node temp = head;
while(temp != null)
{
output += String.format("%s", temp);
temp = temp.next;
}
output += String.format("null ");
return output;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class Node
{
public int data;
public Node next;
public Node()
{
data = -1;
next = null;
}
public String toString()
{
return String.format("%d ", data);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class LinkedListSupplement
{
public static void main(String[] args)
{
Node Node();
oneElement.data = 25;
oneElement.next = null;
Node anotherElement = new Node();
anotherElement.data = 35;
anotherElement.next = oneElement;
System.out.println(anotherElement);
System.out.println(oneElement);
LinkedList l = new LinkedList();
l.insert(25);
l.insert(5);
l.insert(15);
l.insert(35);
l.append(90);
System.out.println(l);
System.out.println(l.search(15));
System.out.println(l.search(20));
System.out.println(l.update(15,20));
System.out.println(l);
System.out.println(l.update(18,28));
System.out.println(l);
System.out.println(l.delete(15));
System.out.println(l);
System.out.println(l.delete(35));
System.out.println(l);
System.out.println(l.delete(5));
System.out.println(l);
}
} // End of code
Explanation / Answer
Updated code:
/*
* 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 Y2018.March;
/**
*
* @author sambh
*/
class LinkedList
{
public Node head;
public LinkedList()
{
head = null;
}
public void insert(int value)
{
Node newNode = new Node();
newNode.data = value;
if(head != null) {
newNode.prev = head.prev;
newNode.next = head;
head.prev = newNode;
newNode.prev.next = newNode;
}
else {
newNode.prev = newNode;
newNode.next = newNode;
}
head = newNode;
}
public void append(int value)
{
if (head == null){
insert(value);
return;
}
Node newNode = new Node();
newNode.data = value;
newNode.next = head;
newNode.prev = head.prev; // remember: head.prev is the last node!!
head.prev.next = newNode;
head.prev = newNode;
}
public Node search(int value)
{
if (head == null)
return null;
Node temp = head;
do {
if (temp.data == value)
return temp;
temp = temp.next;
} while (temp != head);
return null;
}
public boolean update(int oldValue, int newValue)
{
Node temp = search(oldValue);
if (temp == null)
return false;
temp.data = newValue;
return true;
}
public boolean delete(int value)
{
// Situation #1
if (head == null)
return false;
// Situation #2
if (head.data == value)
{
if (head.next != head) {
head.next.prev = head.prev;
head.prev.next = head.next;
head = head.next;
}
else
head = null;
return true;
}
// Final situations
Node victim;
if ((victim = search(value)) == null )
return false;
Node prev = victim.prev;
victim.prev.next = victim.next;
victim.next.prev = victim.prev;
return true;
}
@Override
public String toString()
{
String output = "";
Node temp = head;
while(temp != null)
{
output += String.format("%s", temp);
temp = temp.next;
}
output += String.format("null ");
return output;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Node
{
public int data;
public Node next;
public Node prev;
public Node()
{
data = -1;
next = null;
prev = null;
}
@Override
public String toString()
{
return String.format("%d ", data);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class LinkedListSupplement
{
public static void main(String[] args)
{
Node Node();
oneElement.data = 25;
oneElement.next = null;
Node anotherElement = new Node();
anotherElement.data = 35;
anotherElement.next = oneElement;
System.out.println(anotherElement);
System.out.println(oneElement);
LinkedList l = new LinkedList();
l.insert(25);
l.insert(5);
l.insert(15);
l.insert(35);
l.append(90);
System.out.println(l);
System.out.println(l.search(15));
System.out.println(l.search(20));
System.out.println(l.update(15,20));
System.out.println(l);
System.out.println(l.update(18,28));
System.out.println(l);
System.out.println(l.delete(15));
System.out.println(l);
System.out.println(l.delete(35));
System.out.println(l);
System.out.println(l.delete(5));
System.out.println(l);
}
} // End of code
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.