2

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();

        }
        } 

    }
1
  • Post full stacktrace. Commented Dec 7, 2013 at 6:38

4 Answers 4

2

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

while(!curr.content.equals(content))
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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;
}
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.