Home > Net >  Is there a way to delete certain elements in a linked list above a certain number?
Is there a way to delete certain elements in a linked list above a certain number?

Time:12-21

I have a linked list and I am trying to make a function that removes charms above a certain price. Lets say I have 4 charms and two of them cost $1.25 how would I remove them?

I got my function to traverse the Linked list and remove the charms above the price but I get an error saying it cannot set the node because it is null

class Main {
  public static void main(String[] args) {
        
        CharmList Bracelet = new CharmList();
        
        Bracelet.add(new Charm("YOLO", 1.25));
        Bracelet.add(new Charm("Donald Duck", 1.25));
        Bracelet.add(new Charm("Hello Kitty", 3.25));
        Bracelet.add(new Charm("Drums", 4.25));

        System.out.print(Bracelet);
        System.out.println(Bracelet.total());
    System.out.println(Bracelet.findPrice("YOLO"));

    Bracelet.remove(1.25);
    System.out.println(Bracelet);
    
                
  }
} 
void remove(double price) {
        try {
            CharmNode back = null;
            CharmNode current = list;
            boolean found = false;
            
            while (current != null && !found) {
                if(current.getCharm().getPrice() == price) {
                    found = true;
                } else {
                    back = current;
                    current = current.getNextNode();
                }
                
                if (found) {
                    back.setNode(current.getNextNode());
                    current.setNode(null);
                }
            }
        } catch(NullPointerException e) {
            e.printStackTrace();
        }
        
    } 

CodePudding user response:

Your issue is that the first node is the one you are trying to remove, which means back is null hence the NullPointerException. You need to account for this with a special condition. So if the node to be removed is the first node then the next node needs to become the new first node:

void remove(double price) {
    CharmNode prev = null;
    CharmNode current = list;

    while (current != null) {
        // found first instance
        if (current.getCharm().getPrice() == price) {
            // current is the first
            if (prev == null) {
                Bracelet.setFirst(current.getNextNode())
                current.setNode(null)
            } else {
                back.setNode(current.getNextNode());
                current.setNode(null);
            }
            break;
        }
        back = current;
        current = current.getNextNode();
    }
}

CodePudding user response:

The first CharmNode price matches 1.25, so it calls back.setNode() while back is null.

CodePudding user response:

Whether it is a linked list or an array list you can do it like this. No need to reinvent the wheel.

record Charm(String getName, double getPrice) {}
 List<Charm> bracelet = new ArrayList<>(List.of( 
 new Charm("YOLO", 1.25),
 new Charm("Donald Duck", 1.25),
 new Charm("Hello Kitty", 3.25),
 new Charm("Drums", 4.25)));
 
 System.out.println("Before removing");
 bracelet.forEach(System.out::println);
 // remove entries where the price is > 1.25
 bracelet.removeIf(charm->charm.getPrice() > 1.25);
 System.out.println("\nAfter removing");
 bracelet.forEach(System.out::println);

prints

Before removing
Charm[getName=YOLO, getPrice=1.25]
Charm[getName=Donald Duck, getPrice=1.25]
Charm[getName=Hello Kitty, getPrice=3.25]
Charm[getName=Drums, getPrice=4.25]

After removing
Charm[getName=YOLO, getPrice=1.25]
Charm[getName=Donald Duck, getPrice=1.25]
  • Related