I'm starting to learn Java (started with C++), and still getting to get used to the syntax and whatnot. For practice, I made a doubly-linked list program. Is there any way this could be improved at all, or is this okay code?
// Doubly Linked-List Practice
package doublylinkedlist;
public class DoublyLinkedList {
node head, tail;
class node // Creates node class
{
double num;
node next;
node prev;
public node()
{
num = 0;
next = null;
prev = null;
}
public node (double p)
{
num = p;
next = null;
prev = null;
}
}
public void append(double x) // Appends data to list
{
node p = new node(x);
if (head == null) // Creates head node if empty list
{
head = p;
tail = p;
return;
}
p.prev = tail; // Skips to end of list and appends
tail.next = p;
tail = p;
return;
}
public void showF() // Displays list forward
{
for (node q = head; q != null; q = q.next)
System.out.print(q.num + " ");
System.out.println();
}
public void showR() // Displays list in reverse
{
for (node q = tail; q != null; q = q.prev)
System.out.print(q.num + " ");
System.out.println();
}
public double findAvg()
{
double mean = 0, total = 0;
int i = 0;
for (node p = head; p != null; p=p.next)
{
total += p.num;
++i;
}
return(mean = total/i);
}
public void delete(double x) // Deletes a single node
{
node p = new node(x);
node temp = head;
node pre, nex;
if (head.num == p.num) // If head node needs to be removed
{
head = head.next;
return;
}
while (temp != null) // If a node in between is to be deleted
{
if (p.num == temp.num)
{
System.out.println("Node found! Deleting " + x + "...");
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
return;
}
else temp = temp.next;
}
if (tail.num == p.num) // If tail is to be deleted
{
tail = tail.prev;
return;
}
}
public void deleteMore(double x) // Removes all nodes of certain key
{
node temp = head;
if(head == null) // If list is empty
{
System.out.println("Empty list!");
return;
}
while (head != null && head.num > x) // If head node needs to be deleted
{
head = head.next;
head.prev = null;
temp = head;
}
while(temp !=null) // Every remaining occurrence to be removed
{
if(temp.num > x)
{
if (temp.num == tail.num) // If tail node needs to be removed
{
tail = tail.prev;
tail.next = null;
temp = tail;
} else
{
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
}
}
temp = temp.next;
}
}
public static void main(String[] args) {
DoublyLinkedList myList = new DoublyLinkedList();
double[] arr = {600.0, 100.0, 10.0, 15.0, 20.0, 200.0, 30.0, 40.0, 300.0, 350.0, 400.0, 500.0};
for (int i = 0; i < arr.length; ++i)
myList.append(arr[i]);
System.out.println("Here's your list foreward...");
myList.showF();
System.out.println("Average = " + myList.findAvg());
System.out.println("Removing all excess averages...");
myList.deleteMore(myList.findAvg());
System.out.println("Here's your revised list");
myList.showF();
System.out.println("And here's your list in reverse");
myList.showR();
System.out.println("Removing 30, just because I can");
myList.delete(30.0);
myList.showF();
}
}
delete(x)
. If you delete thehead
node, the next node’sprev
still points to it. If there was only 1 node,tail
will still point to it. If you delete the last node,temp.next.prev
is aNullPointerException
. You need more testing ofdeleteMore(x)
, it has bugs too! (Try adding a 500.0 in the middle of the list!) \$\endgroup\$