Linked List Delete Method - java

Could anyone explain why the following delete method does not work? It appears to create an infinite loop at the value I am trying to remove. It should loop through a linked list, delete all instances of the value passed to the method, and return the total number of nodes deleted (return num;).
public int delete(T value)
{
int num = 0;
ListNode<T> trav = head;
ListNode<T> next = head.getNext();
while(trav != null) {
if(trav.getValue().compareTo(value) == 0) {
trav = next;
num++;
}
if(next.getValue().compareTo(value) == 0) {
trav = next.getNext();
num++;
}
trav = trav.getNext();
}
return num;
}

You never change the value of next.
if(trav.getValue().compareTo(value) == 0) {
trav = next;
num++;
}
if(next.getValue().compareTo(value) == 0) {
trav = next.getNext();
num++;
}
Since next never changes, you're comparing the same value in the second if every time.

Related

How to properly count nodes in the stack?

I created this loop to count elements in the stack that are within the interval [-10;10], but when I return the answer, I can see, that it doesn't count the last node, is there something wrong here?
public int intervalNumbers() {
int counter = 0;
Node node;
for (node = top;
node.link != null;
node = node.link) {
if (node.data >= -10 && node.data <= 10) {
counter++;
}
}
return counter;
}
It is sort of off by one case.
for (node = top;
node.link != null;
node = node.link)
Because of the condition node.link != null, the body of the loop is not executed for the last node (as the last node's link would be null)
Change it to node != null

Java Linked List maxElement recursive

I have been working on an assignment for awhile now one of the methods I have to define is a recursive solution to finding the max element in the list. I feel like my solution is close, but I keep getting the last element inserted back, instead of the max element. Can someone point out what I need to do to solve this?
* I have been instructed not to use pre-built classes or methods. *
int maxElement () {
if (head == null) {
return -1;
}
else {
Node current = head;
return maxElement (current);
}
}
private int maxElement (Node current) {
Node max;
// If our next node does not exist, our current node has to be max
if (current.getNext() == null) {
return current.getKey();
}
//
else {
if (current.getNext().getKey() > current.getKey()) {
// Assign the larger key as our new max
max = current.getNext();
return maxElement (max);
}
else {
}
}
return maxElement (max.getNext());
}
Here is a fairly elegant solution:
public Node max(Node node1, Node node2) {
if (node1 == null || node2 == null)
return node1;
else
return max(node1.getKey() > node2.getKey() ? node1 : node2, node2.getNext());
}
It is called using max(head, head.getNext()).
If you specifically need to avoid passing the current maximum into the method then:
private static Node currentMax;
private Node maxElement(Node node) {
if (currentMax == null || node == null)
return currentMax;
else if (node.getKey() > currentMax.getKey())
currentMax = node;
return maxElement(node.getNext());
}
This is called with currentMax = head; then maxElement(head).
You have to keep track of the current max in your recursion:
public Node max() {
return maxAux(root, null);
}
private Node maxAux(Node current, Node currentMax) {
if(current == null) {
return currentMax;
}
if(currentMax == null || current.getKey() > currentMax.getKey()) {
return maxAux(current.getNext(), current); // current is the new max
} else {
return maxAux(current.getNext(), currentMax);
}
}
private Node head, max;
int maxElement () {
if (head == null) {
return -1;
}
else {
Node current = head;
return maxElement (current);
}
}
private int maxElement (Node current) {
// If our next node does not exist, our current node has to be max
if (current.getNext() == null) {
current = this.max;
return this.max.getKey();
}
//
else {
if (current.getNext().getKey() > current.getKey()) {
// Assign the larger key as our new max
this.max = current.getNext();
return maxElement (current.getNext());
}
else {
}
}
return maxElement (max.getNext());
}

NullPointerException with while loop using compareTo

For our homework, I have to take in Chair objects and add them to the DoublyLinkedList that we made; it has to be sorted by alphabetical order, if style is the same alphabetically, we sort by color
When I try to go through the loop, I keep getting a NullPointerException.
public void add(Chair element){
if(isEmpty() || first.object.style.compareTo(element.style) > 0 || (first.object.style.compareTo(element.style) == 0 && first.object.color.compareTo(element.color) >= 0){
addFirst(element);
}else if(first.object.style.compareTo(element.style) <= 0){
Node temp = first;
Node insert = new Node(); insert.object = element;
while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs
if(temp.hasNext())
temp = temp.next;
while(temp.object.style.compareTo(element.style) == 0 && temp.object.color.compareTo(element.color) <= 0)
if(temp.hasNext())
temp = temp.next;
insert.prev = temp.prev;
insert.next = temp;
temp.prev.next = insert;
temp.prev = insert;
}
}
This is the code where I put the information into the DoublyLinkedList
try{
FileReader fr = new FileReader(filename);
Scanner sc = new Scanner(fr);
String[] temp;
while(sc.hasNext()){
temp = sc.nextLine().split(" ");
if(temp[0].equals("Bed")){}
else if(temp[0].equals("Table")){
// tables.add(new Table(Integer.parseInt(temp[1]), Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), temp[4]));
}else if(temp[0].equals("Desk")){}
else if(temp[0].equals("Chair")){
chairs.add(new Chair(temp[1], temp[2]));
}else if(temp[0].equals("Bookshelves")){}
else{
color = temp[0];
}
}
while(!chairs.isEmpty())
System.out.println(chairs.removeFirst().info());
System.out.println();
//while(!tables.isEmpty())
// System.out.println(tables.removeFirst().info());
}catch(Exception e){e.printStackTrace();}
This is the DoublyLinkedList class that I've made:
class CDoublyLinkedList{
Node first, last;
public CDoublyLinkedList(){
first = new Node(); last = new Node();
first.prev = last.next = null;
first.object = last.object = null;
first.next = last;
last.prev = first;
}
public boolean isEmpty(){
return first.object == null;
}
public void addFirst(Chair element){
Node insert = new Node();
insert.object = element;
insert.prev = null;
insert.next = first;
first.prev = insert;
first = insert;
}
public void add(Chair element){
if(isEmpty() || first.object.style.compareTo(element.style) > 0 || (first.object.style.compareTo(element.style) == 0 && first.object.color.compareTo(element.color) >= 0){
addFirst(element);
}else if(first.object.style.compareTo(element.style) <= 0){
Node temp = first;
Node insert = new Node(); insert.object = element;
while(first.object.style.compareTo(element.style) <= 0)
if(temp.hasNext())
temp = temp.next;
while(first.object.style.compareTo(element.style) == 0 && first.object.color.compareTo(element.color) <= 0)
if(temp.hasNext())
temp = temp.next;
insert.prev = temp.prev;
insert.next = temp;
temp.prev.next = insert;
temp.prev = insert;
}
}
public Chair removeFirst(){
Chair tobedeleted = first.object;
Node temp = first.next;
first = temp;
first.prev = null;
return tobedeleted;
}
private class Node{
Node next, prev;
Chair object;
public boolean hasNext(){
return next != null;
}
}
}
The Chair class:
class Chair extends Furniture{
public String style, color;
public Chair(String s, String c){
style = s; color = c;
}
public String toString(){
return color;
}
public String getType(){
return "Chair";
}
public String info(){
return (color+", "+style);
}
}
Can someone please explain to me why I keep getting this error? Thank you!
EDIT:
while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs
chairs.add(new Chair(temp[1], temp[2]));
java.lang.NullPointerException
at CDoublyLinkedList.add(Furnish2SS.java:119)
at Furnish2SS.main(Furnish2SS.java:23)
java.lang.NullPointerException
at CDoublyLinkedList.add(Furnish2SS.java:119)
at Furnish2SS.main(Furnish2SS.java:23)
EDIT2: SOLVED!
I changed my while loop to:
while(temp.object != null && element != null && (temp.object.compareTo(element) == 0 || temp.object.compareTo(element) == -1))
The reason I got the error was because I wasn't checking for null every iteration.
You say this is the line of code causing the exception:
while(temp.object.style.compareTo(element.style) <= 0)
You probably should set a debugger breakpoint on that line and use a debugger to determine which of the values is null. But it's hard for me to explain here full instructions on how to setup and use a debugger (that doesn't mean you shouldn't learn! You should. There are lots of tutorials. Google it.) So instead of writing a tutorial on debuggers, I'll just post code that will tell you which variable is null:
if (temp == null) {
System.out.println("temp is null");
} else if (temp.object == null) {
System.out.println("temp.object is null");
} else if (temp.object.style == null) {
System.out.println("temp.object.style is null");
}
if (element == null) {
System.out.println("element is null");
} else if (element.style == null) {
System.out.println("element.style is null");
}
while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs
{
if(temp.hasNext())
temp = temp.next;
if (temp == null) {
System.out.println("loop: temp is null");
} else if (temp.object == null) {
System.out.println("loop: temp.object is null");
} else if (temp.object.style == null) {
System.out.println("loop: temp.object.style is null");
}
if (element == null) {
System.out.println("loop: element is null");
} else if (element.style == null) {
System.out.println("loop: element.style is null");
}
}
If you use the above code statements to replace these three lines of your code:
while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs
if(temp.hasNext())
temp = temp.next;
you will know which variable is null based on which statement is printed. Hopefully you can take it from there. (The usual way to fix a NullPointerException is to take the steps necessary to ensure the offending null variable actually has a valid, non-null value by the time the program reaches the line of the NullPointerException).
Take a look at addFirst(Chair element). That method is really screwed up. It creates a new Node which contains the correct Chair. It then sets its prev to null. Then it sets next to first. And this is what's causing all of your troubles. Because first points to an empty Node. You end up with this:
first points to your new Node. That one points to a Node which holds no Chair. That one again points to last.
e:
Your whole code looks like you had at least two different approaches on implementing your list and threw them toghether. There are some more errors but since this is homework I guess it's not that bad if you try fixing it first.
If you can't figure out how to correct that, ask here.
PS: Sorry for all of the editing and (un)deleting my answeres (if you noticed). I'm a bit tired and kept causing new errors by fixing old ones until I finally figured out what was the true cause of all this.

circular single linkedList

I've done some exercises in Java and now I'm stuck at such a problem - my list works incorrectly. I am sure that remove works incorrectly and maybe you can help me (with advice or code) to implement a circular singly linked list in a correct way. I am not sure whether other functions work properly, but I've tried to do my best.
Here is my code:
import java.util.*;
public class Node {
private Object value;
private Object nextValue;
private Node next;
public Node(int data) {
this.value = data;
this.next = null;
}
public Object getValue() {
return this.value;
}
public Node nextItem() {
return this.next;
}
public void setNextItem(Node nextItem) {
this.next = (Node) nextItem;
this.next.setValue(nextItem.getValue());
}
public void setValue(Object arg0) {
this.value = arg0;
}
}
-------------------------------------------------------------------
import java.util.*;
public class CircularList {
private Object[] array;
private int arrSize;
private int index;
private Node head;
private Node tail;
public CircularList() {
head = null;
tail = null;
}
public boolean add(Node item) {
if (item == null) {
throw new NullPointerException("the item is null!!!");
}
if (head == null) {
head = item;
head.setNextItem(head);
arrSize++;
return true;
}
Node cur = head;
while(cur.nextItem() != head) {
if(cur.getValue() == item.getValue()) {
throw new IllegalArgumentException("the element already " +
"exists!");
}
cur = cur.nextItem();
}
head.setNextItem(item);
item.setNextItem(head);
arrSize++;
return true;
}
public Node getFirst() {
return head;
}
public void insertAfter(Node item, Node nextItem) {
if ((item == null) || (nextItem == null)) {
throw new NullPointerException("the item is nul!!!");
} else if (this.contains(nextItem) == true) {
throw new IllegalArgumentException("the item already exists!");
}
Node cur = head;
while(cur.nextItem() != head) {
if(cur.getValue() == item.getValue()) {
nextItem.setNextItem(item.nextItem());
item.setNextItem(nextItem);
} else {
cur = cur.nextItem();
}
}
}
public boolean remove(Node item) {
if(item == head) {
Node cur = head;
for(int i = 0; i < arrSize-1; i++) {
cur = cur.nextItem();
}
head = head.nextItem();
for(int i = 0; i < arrSize; i++) {
cur = cur.nextItem();
}
arrSize--;
return true;
}
Node cur = head;
int counter = 0;
while(cur.nextItem() != head) {
if(cur == item) {
item = null;
cur = cur.nextItem();
while(cur.nextItem() != head) {
cur.setNextItem(cur.nextItem().nextItem());
}
return true;
}
cur = cur.nextItem();
}
return false;
}
public int size() {
return arrSize;
}
public boolean contains(Object o) {
if ((o == null) && (arrSize == 0)) {
return false;
}
Node cur = head;
while(cur.nextItem() != head) {
if(cur.getValue() == o) {
return true;
}
cur = cur.nextItem();
}
return false;
}
}
Many of these algorithms could be simpler.
Example:
public boolean remove(Node item) {
Node current = head;
for(int i = 0; i < size; i++) {
if (current.getNext() == item) {
current.next = current.getNext().getNext();
size --;
return true;
}
current = current.getNext()
}
return false;
}
There are a variety of issues here beyond the list. You seem to be comparing your nodes with ==. This code will output 'no match'.
Node n1 = new Node(5);
Node n2 = new Node(5);
if (n1 == n2)
System.out.println("objects match");
else
System.out.println("no match");
In add(), it looks like you can only ever have two items in the list, so
head.setNextItem(item);
item.setNextItem(head);
should be this:
cur.setNextItem(item);
item.setNextItem(head);
There's a lot going on in your code, here's some advice for some of it:
In your Node class: Java naming conventions: the same way that setters should be prefixed with "set," getters should be prefixed with "get:" nextItem() should really be getNextItem().
Also in your Node class: as far as I know, the "next value" field of a node of a linked list is usually a reference to the next Node in the list, and should therefore be of type Node, not just any Object. It should work the way you have it, but using explicit typing is a lot safer. (Please correct me if using "Object" is indeed a common way to construct the next node of a linked list.)
In the first case of remove(), when removing the head: you're looping through the list to reach the last value, presumably to reset its "next value" to the new head, but you're not actually doing it. You want something like this:
if (item == head) {
head = head.nextItem();
for(int i = 0; i < arrSize-1; i++){
cur = cur.nextItem();
}
}
cur.setNextItem(head);
I'm not sure what you hope to accomplish with the second loop.
In the second case of remove(): I'm not sure what you're trying to do with the second while loop - reset all the links in the whole list? The whole point of a linked list is to make that unnecessary. Deleting a node in a linked list does not actually get rid of the object (so you don't have to set item to null). Rather, you simply "go around" the unwanted object and "ignore" it, effectively removing it from the list, as in:
Original list:
[ Value: A; Next: B ] --> [ Value: B; Next: C ] --> [ Value C; Next: D ] ...
After deleting node B:
[ Value: A; Next: C ] --> [Value C; Next: D ] ...
[ Value: B; Next: C ] still exists in memory, but nothing is pointing to it, so it will be removed in the next garbage collection cycle.
To implelement: As you walk the list, keep a reference to the previous node that you visited. Then, once you find the item you're looking for (using correct comparison, as Thomas noted), you can simply set prev.setNextItem(cur.nextItem()); (caveat: untested code):
Node prev = head;
Node cur;
while ((cur = prev.nextItem()) != head) {
if (cur.equals(item)) {
prev.setNextItem(cur.getNextItem());
return true;
}
}
I hope these tips help you along the correct path.

Java: NPE in Circular Linked List :(

import javax.swing.JOptionPane;
public class RotateArrayCircularLL
{
private Node head=null;
public void init()
{
int choice = 0;
while (choice != -1){
choice = Integer.parseInt(JOptionPane.showInputDialog("Enter -1 to stop loop, 1 to continue"));
if(choice == -1)
break;
inputNum();
}
printList();
}
public void inputNum()
{
Node n;
Node temp;
int k;
k = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter a number:"));
n = new Node(k);
if (head == null) {
head = n;
} else {
temp = head;
while (temp.getNext() != null)
temp = temp.getNext();
temp.setNext(n);
}
}
public void printList()
{
Node temp = head;
int count = Integer.parseInt(JOptionPane.showInputDialog("Enter the value to shift to the right"));
for (int i = 1; i <= count; i++) // Rotates the head
temp = temp.getNext();
for (Node c = temp; c != null && c.getNext() != head; c= c.getNext()){ // Prints the new LL
System.out.print(c.getInfo());
}
}
}
I get an NPE during the second for loop. I understand that it is giving me a NPE because I reach the end of the list, but how can I stop it from doing this?
It appears from the behavior you are seeing that one of the nodes in your linked list is returning null instead of the next element of the list. At a guess, I'd suggest that the last node of your list is probably not pointing to the first node of your list. Hence as Hovercraft Full Of Eels suggests, you don't really have a properly circular linked list. If you can post the code showing how temp is populated, it may be possible to give a more concrete solution to your issue. Otherwise, you need to treat the case where getNext() returns null as a special case and ensure that you instead get the first element from the initial list.

Categories