user3076911
user3076911

Reputation: 23

Java - Delete Node From Linked List

Okay so this is just a simple program that will receive input from a user and add it to a linked list, and also give them the options to view the list and delete a node. It compiles fine and can add nodes and display the list but it will not delete a node. It works when I hand code it without the keyboard input, even with the same variable name so that's where the problem is.

    public class LinkedList {

       public class Link {

      public String content;
      public Link next;

      public Link(String content) {
         this.content = content;
      }

      public void display(){
         System.out.println(content);
      }
}

public static Link head;

LinkedList(){
    head = null;
}

public boolean isEmpty() {

    return(head == null);
}

public void insertFirstLink(String content) {
    Link newLink = new Link(content);

    newLink.next = head;

    head = newLink;
}

public void display() {
    Link theLink = head;

    while(theLink != null) {
        theLink.display();
        theLink = theLink.next;
    }
}

public Link removeLink(String content) {
    Link curr = head;
    Link prev = head;

    while(curr.content != content) {

        if (curr.next == null) {
        return null;
    }

        else {
            prev = curr;
            curr = curr.next;
        }
}

    if(curr == head) {

        head = head.next;


    }
    else {
        prev.next = curr.next;
    }
    return curr;
    }
    }


    public class Testlist {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
        int choice = 0;
        String content;
        System.out.println("Enter 1 to add to list");
        System.out.println("Enter 2 to display list");
        System.out.println("Enter 3 to delete node");
        System.out.println("Enter 4 to quit");
        choice = keyboard.nextInt();
        LinkedList newlist = new LinkedList();
        while(choice != 4) {

            if (choice == 1) {

                content = keyboard.next();
                newlist.insertFirstLink(content);
                newlist.display();


            }

            if (choice == 2) {

                newlist.display();
            }

            if (choice == 3) {


                content = keyboard.next();  // this is where is goes wrong
                newlist.removeLink(content);
                newlist.display();
            }


            System.out.println("Enter 1 to add to list");
            System.out.println("Enter 2 to display list");
            System.out.println("Enter 3 to delete node");
            System.out.println("Enter 4 to quit");
            choice = keyboard.nextInt();

        }
        } 

    }

Upvotes: 2

Views: 131

Answers (4)

polis
polis

Reputation: 805

Also use 'equals' in this line: 'if(curr == head) {'.

Upvotes: 0

vidit bhatia
vidit bhatia

Reputation: 60

For comparing string you should use equals or equalsignorecase()

For example String1="xyz"; and String2="xyz" these two strings are different if you comare them using == or != as the objects are compared instead of the actual content . the correct implementation for your program would be

package stackoverflow.practice;

public class LinkedList {

public class Link {

public String content;
public Link next;

public Link(String content) {



this.content = content;
}

public void display(){
System.out.println(content);
}


}

public static Link head;

LinkedList(){
head = null;
}

public boolean isEmpty() {

return(head == null);
}

public void insertFirstLink(String content) {
Link newLink = new Link(content);

newLink.next = head;

head = newLink;
}

public void display() {
Link theLink = head;

while(theLink != null) {
    theLink.display();
    theLink = theLink.next;
}
}

public Link removeLink(String content) {
Link curr = head;
Link prev = head;

while(!curr.content.equalsIgnoreCase(content)) {

    if (curr.next == null) {
    return null;
}

    else {
        prev = curr;
        curr = curr.next;
    }
}

if(curr == head) {

    head = head.next;


}
else {
    prev.next = curr.next;
}
return curr;
}
}

Upvotes: 0

yatesrates
yatesrates

Reputation: 11

Some one may have to double check, but i'm fairly sure this is because the nextInt() method grabs the first integer and thats all. which leaves the 'enter/carriage return' in the input stream. so when the next() method is run it grabs that enter. Definatly put in some debug lines to see what content is.

Upvotes: 0

Steve P.
Steve P.

Reputation: 14709

You're using !=, which compares by reference for objects, not by value. You want to use .equals(), ie:

while(!curr.content.equals(content))

Upvotes: 2

Related Questions