Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 86,252 Java Programmers. There are 2,130 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Duplicates in Linked Lists

 
Reply to this topicStart new topic

Duplicates in Linked Lists

Rating  5
Den2000
post 8 May, 2008 - 06:59 PM
Post #1


New D.I.C Head

*
Joined: 8 May, 2008
Posts: 7



Ok, so i know I'm not supposed to ask about my assignments, but i'm not asking you to do it. Just help with what my instructor nor the book did not discuss. The assignment asks to create a linked list and not allow duplicates. The list part i got, but lnked lists allow duplicates and i haven't seen anything in the API to show where to weed out them out. I have an idea of a way to do it, but that could get messy and a little cumbersome. Any thought or ideas would be a big help.

Thanks, Dennis

Here's what i have so far: (note: this is very incomplete)

CODE
import java.util.*;

public class NamesList
{
    private static final String names[] = {"Arron", "Arron", "Jack", "Jack", "Jill", "Jill", "Brian", "Beth", "Donny", "Samantha", "Alex", "Thomas"};
    
    public NamesList()
    {
        List< String > list = new LinkedList< String >();
        
        for(String first : names)
            list.add(first);
        
        printList(list);
            
    }
    
    public void printList(List< String > newList)
    {
        System.out.println("\nList:  ");
        
        for(String first : newList)
            System.out.printf("%s", first);
        
        System.out.println();
    }
    
    public void SearchList(List< String > newList)
    {
        
    }
    
    public static void main(String[] args)
    {
        new NamesList();        
    }

}
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


pbl
post 8 May, 2008 - 07:34 PM
Post #2


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

First make your list as a LinkedList not a List
an make it an instance variable not just part of the contructor

CODE

import java.util.*;

public class NamesList
{
    private static final String names[] = {"Arron", "Arron", "Jack", "Jack", "Jill", "Jill", "Brian", "Beth", "Donny", "Samantha", "Alex", "Thomas"};

    LinkedList< String > list = new LinkedList< String >();
    
    public NamesList()
    {
        
        for(String first : names)
            list.add(first);
        
// now you do nota have to pass the LinkedList as argument to printList
        printList();
            
    }
    
    public void printList()
    {
        System.out.println("\nList:  ");
        
        for(String first : newList)
            System.out.printf("%s", first);
        
        System.out.println();
    }
}


Now you can write your own add() method that checks if the elemet is already there

CODE

// method to add a new String to the list return false if duplicate
boolean add(String toInsert) {
    for(String member : list) {
        if(member.equals(toInsert))     // String already in list
          return false;
    }
    // so not already in the linkedList
    list.add(toInsert);
    return true;
}


Edited to correct a typo printList() not printList(1

This post has been edited by pbl: 8 May, 2008 - 08:22 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Den2000
post 8 May, 2008 - 08:25 PM
Post #3


New D.I.C Head

*
Joined: 8 May, 2008
Posts: 7

Thank you! That is a huge help, and is much better than what i was thinking.

Dennis
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 8 May, 2008 - 09:33 PM
Post #4


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

QUOTE(Den2000 @ 8 May, 2008 - 08:25 PM) *

Thank you! That is a huge help, and is much better than what i was thinking.

Dennis

You are welcome. If you liked my answer rate it using the Rating combo box at the top of the post

This post has been edited by pbl: 8 May, 2008 - 09:33 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

potator
post 9 May, 2008 - 05:50 AM
Post #5


D.I.C Head

Group Icon
Joined: 2 Dec, 2007
Posts: 56

If the data type implements Comparable, you could also create a loop that checks each list value against the one that you're about to insert:
CODE

LinkedList<String> list;
boolean add = true;
String insertValue = "Hello.";
for(int x = 0; x < list.size(); x++)
     if(list.get(x).equals(insertValue))
          add = false;
if(add)
     list.add(insertValue);
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 9 May, 2008 - 06:38 AM
Post #6


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

QUOTE(potator @ 9 May, 2008 - 05:50 AM) *

If the data type implements Comparable, you could also create a loop that checks each list value against the one that you're about to insert:
CODE

LinkedList<String> list;
boolean add = true;
String insertValue = "Hello.";
for(int x = 0; x < list.size(); x++)
     if(list.get(x).equals(insertValue))
          add = false;
if(add)
     list.add(insertValue);


What is the difference between this solution and the one that I have proposed beside the fact that this one pass through all the linked list even if there is a match at the first element ?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 09:05AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month