user3840332
user3840332

Reputation:

How to remove duplicated nodes from a linked list?

I've designed my own singly linked list data structure in java.Now I'm about to define a function with a specific behavior.I named this functon "PurgeList".This function is supposed to delete every duplicated nodes(nodes with a same content)from the linked list and at least I expect the list to keep only one node with that content in itself.For example if the current contents saved in the nodes are sequentially :

1 , 2 , 3 , 4 , 1 , 4 , 5

after the function with the mentioned behavior executes , the list must shape into :

1 , 2 , 3 , 4 , 5

Example Codes :

1- Class Node

public class Node {
Object Element;
Node Link;

public Node() {
    this(null,null);
}

public Node(Object Element, Node Link) {
    this.Element = Element;
    this.Link = Link;
}
}

2- Class List

import java.util.Scanner;
public class List {
int Size;
Node FirstNode;
Scanner UserInfo = new Scanner(System.in);
Scanner UserInput = new Scanner(System.in);
Node LastNode;
public List() {
    FirstNode = null;
    Size = 1;
}
public void PurgeList() {
    Node temp1 = FirstNode;
    while (temp1 != null) {
        Node temp2 = temp1;
        while (temp2.Link != null)
            if (temp1.Element.equals(temp2.Link.Element))
                temp2 = temp2.Link;
            else
                temp2=temp2.Link;
        temp1=temp1.Link;
    }
}
public boolean IsEmpty() {
    return FirstNode == null;
}
public int SizeOf() {
    return Size;
}
public void InsertArbitrary() {
    System.out.print("Where To Put Node :  ");
    int Location = UserInput.nextInt();
    if (Location > Size) {
        System.out.println("Invalid Input.Try again");
        return;
    } else if (Location < 0) {
        System.out.println("Invalid Input.Try again");
        return;
    } else if (Location == 1) {
        System.out
                .printf("Enter something to save in Node %d : ", Location);
        Object Element = UserInfo.nextLine();
        FirstNode = new Node(Element, FirstNode);
    } else if (Location > 0 && Location <= Size) {
        System.out
                .printf("Enter something to save in Node %d : ", Location);
        Object Element = UserInfo.nextLine();
        Node CurrentNode = FirstNode;
        for (int i = 1; i <= Location - 2; i++) {
            CurrentNode = CurrentNode.Link;
        }
        Node NewNode = new Node(Element, CurrentNode.Link);
        CurrentNode.Link = NewNode;
    } else {
        System.out.println("Invalid Number . Try again.");
        return;
    }
    Size++;
}

public void ShowOff() {
    Node Temp;
    Temp = FirstNode;
    int number = 1;

    while (Temp != null) {
        System.out.println(Temp.Element);
        Temp = Temp.Link;
        number++;
    }
}
protected boolean ListIsEmpty() {
    return FirstNode == null;
 }
}

I copied my other functions I implemented for more details.I've also traced my program but failed to find my logical mistakes.Please help me fix out this matter.Thanks in advance.

Upvotes: 1

Views: 115

Answers (2)

Md. Nasir Uddin Bhuiyan
Md. Nasir Uddin Bhuiyan

Reputation: 1596

You need to use Set Collection for avoiding duplicate values. Here is an example The Set interface

Upvotes: 0

jas
jas

Reputation: 10865

You're not removing the node when you find a duplicate.

Instead of

    while (temp2.Link != null)
        if (temp1.Element.equals(temp2.Link.Element))
            temp2 = temp2.Link;
        else
            temp2=temp2.Link;

try

       while (temp2.Link != null) {
            if (temp1.Element.equals(temp2.Link.Element))
                temp2.Link = temp2.Link.Link;
            else
                temp2=temp2.Link;
        }

You should try to think of different cases to test it well. I tried some with this:

public static void main(String[] args) {
    List list = new List();
    for (int i=0; i<5; ++i) {
        list.InsertArbitrary();
    }
    list.ShowOff();
    list.PurgeList();
    System.out.println("------------------");
    list.ShowOff();
}

$ java List
Where To Put Node :  1
Enter something to save in Node 1 : 1
Where To Put Node :  2
Enter something to save in Node 2 : 2
Where To Put Node :  3
Enter something to save in Node 3 : 3
Where To Put Node :  4
Enter something to save in Node 4 : 1
Where To Put Node :  5
Enter something to save in Node 5 : 5
1
2
3
1
5
------------------
1
2
3
5

$ java List
Where To Put Node :  1
Enter something to save in Node 1 : 1
Where To Put Node :  2
Enter something to save in Node 2 : 1
Where To Put Node :  3
Enter something to save in Node 3 : 1
Where To Put Node :  4
Enter something to save in Node 4 : 1
Where To Put Node :  5
Enter something to save in Node 5 : 1
1
1
1
1
1
------------------
1

Upvotes: 0

Related Questions