2

folks, I'm having trouble with removing a specified Link from a LinkedList. For example, if I have:

"A" --> "B" --> "C"

and would like to remove a Link "B", so the result would be:

"A" --> "C"

I'm having troubles with how to get the previous Link "A" and reference to "C". Can anyone help me out with that? The method is:

 public void delete(String data){
        if(isEmpty()){
            System.out.println("The list is empty!");
            System.exit(0);
        }
        else{
            Link current = firstLink;
            Link previous = firstLink;
            while(current != null){
                if(current.getData().equals(data)){
                    previous = current.getNext();   
                }
                else{
                    previous = current;
                    current = current.getNext();
                }
            }
        }
    }

Class Link

package LinkedList;

public class Link{

    private String data;
    private Link next;

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

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

    public String getData(){
        return this.data;
    }

    public Link getNext(){
        return this.next;
    }
}

class LinkedList{

    private Link firstLink;

    public LinkedList(){
        this.firstLink = null;
    }

    public boolean isEmpty(){
        return (this.firstLink == null);
    }

    public void insert(String data){
        Link newLink = new Link(data);
        Link newLinkNext = newLink.getNext();
        newLinkNext = firstLink;
        firstLink = newLink;
    }

    public Link deleteFirst(){
        if(isEmpty()){
            return null;
        }
        else {
            Link deletedOne = this.firstLink;
            Link nextLink = firstLink.getNext();
            firstLink = nextLink;
            return deletedOne;
        }
    }

    public void delete(String data){
    if(isEmpty()){
        System.out.println("The list is empty!");
        System.exit(0);
    }
    else{
        Link current = firstLink;
        Link previous = firstLink;
        while(current != null){
            if(current.getData().equals(data)){
                previous = current.getNext();   
            }
            else{
                previous = current;
                current = current.getNext();
            }
        }
    }
}
5
  • I don't even see any attempt to join A to C. You need to know the node before B so you can make the node before B point to the node after B... Commented Jul 27, 2015 at 23:25
  • @John3136, that's my question how should I get the Node before "B"? Commented Jul 27, 2015 at 23:27
  • You have a while loop - why not remember the previous value each time through the loop? Commented Jul 27, 2015 at 23:31
  • @John3136, I've updated the code. Havent tested yet but will be doing in a sec Commented Jul 27, 2015 at 23:31
  • Should be previous.next = current.getNext() and not previous = current.getNext(). Should probably put a check if current == firstLink and deal with that appropriately. Commented Jul 28, 2015 at 1:31

4 Answers 4

1

Pseudo code:

prev = null;
current = first;
while not at the end of the list
{
    if (current.data == the object I want) {
        if (prev == null) {
            first = current.next
            break
        }
        prev.next = current.next
        break;
    }
    prev = current
    current = current.next
}
Sign up to request clarification or add additional context in comments.

4 Comments

Question, @John3136, why do you have there prev.next = current.next ?
It sets the previous node to point to the next one (so now "current" isn't part of your list). It's in the if so you only do it when current is the node you want to remove.
But still it does not work, @John3136. I've tried the code you gave here
Then you need to post more details - that pseudo code works - run through it with a pen and paper to confirm - that might help you spot your issue.
0

here is a sample tried to make it similar to your code

public class LinkedList {

private Node firstNode = null;

public LinkedList() {
}

public Node getFirstNode() {
    return firstNode;
}

public void setFirstNode(Node firstNode) {
    this.firstNode = firstNode;
}

public void addNode(Node node) {
    if(firstNode == null){
        firstNode = node;
    }else{
        Node walker = firstNode;
        while(walker.getNext() != null)
            walker = walker.getNext();
        walker.setNext(node);
    }
}

public void delete(int value) {
    if (firstNode != null) {
        Node walker = firstNode;
        if (walker.getValue() == value) {
            if(walker.getNext()!=null){
                firstNode = walker.getNext();
            }else{
                setFirstNode(null);
            }
        } else {
            Node previous = walker;
            while (walker.getNext() != null) {
                previous = walker;
                walker = walker.getNext();
                if (walker.getValue() == value) {
                    previous.setNext(walker.getNext());
                    break;
                }
            }
        }

    } else {
        System.out.println("Nothing to delete");
    }
}

public void listValues (){
    if(firstNode != null){
        Node walker = firstNode;
        while(walker.getNext() != null)
        {
            System.out.println(walker.getValue());
            walker = walker.getNext();

        }
    }
}

}

public class Node {

private Node next = null;
private int value;

public Node(Node node, int value) {
    this.next = node;
    this.value = value;
}

public Node getNext() {
    return next;
}

public void setNext(Node node) {
    this.next = node;
}

public int getValue() {
    return value;
}

public void setValue(int value) {
    this.value = value;
}

}

public class Simulate {

public static void main(String[] args) {
    LinkedList list = new LinkedList();
    list.addNode(new Node(null, 1));
    list.addNode(new Node(null, 2));
    list.addNode(new Node(null, 3));
    list.addNode(new Node(null, 4));
    list.addNode(new Node(null, 123));
    list.addNode(new Node(null, 5));
    list.addNode(new Node(null, 6));
    list.listValues();


    list.delete(1);
    System.out.println("-----");
    list.listValues();

    list.delete(123);
    System.out.println("-----");
    list.listValues();


    list.addNode(new Node(null, 123));
    list.addNode(new Node(null, 5));

    list.delete(2);
    System.out.println("-----");
    list.listValues();

    list.delete(26);
    System.out.println("-----");
    list.listValues();
}

}

and output: 1 2 3 4 123 5

2 3 4 123 5

2 3 4 5

3 4 5 6 123

3 4 5 6 123

Regards

Comments

0
class LLNode
    {
        int data;
        LLNode next;
        public LLNode(int data)
        {
            this.data=data;
        }
    }

public class DeleteNodeFromLinkedList 
{
    /* Link list node */


    /* Given a reference (pointer to pointer) to the head
    of a list and an int, push a new node on the front
    of the list. */

    public static void printList(LLNode head)
    {
       LLNode curr = head;
       while(curr != null)
       {
          System.out.print(curr.data+" ");
          curr = curr.next;
       }
    }

    static void deleteNode(LLNode DelNode)
    {
       LLNode temp = DelNode.next;
       DelNode.data = temp.data;
       DelNode.next = temp.next;

    }

    /* Drier program to test above function*/
    public static void main(String []args)
    {
        /* Start with the empty list */
        LLNode head = new LLNode(1);
        head.next = new LLNode(4);
        head.next.next = new LLNode(4);
        head.next.next.next = new LLNode(1);
        head.next.next.next.next = new LLNode(12);
        head.next.next.next.next.next = new LLNode(1);


        System.out.println(" Before deleting ");
        printList(head);

        /* I m deleting the head.next
            You can check for more cases */
       deleteNode(head.next);

       System.out.println(" \n After deleting ");
       printList(head);

    }
}

Comments

0

you cannot operate in lists (add, remove... items) while you iterate on them. You have to use an Iterator

 for(Iterator<EmpDedup> iter = list.iterator(); iter.hasNext();) {
     EmpDedup data = iter.next();
     if (data.getRecord() == rec1) {
         iter.remove();
     }
  }

see http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html

Comments

Your Answer

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