School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,158 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,068 people online right now. Registration is fast and FREE... Join Now!



Duplicates in Linked Lists

Duplicates in Linked Lists Rate Topic: ***** 1 Votes

#1 Den2000  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 9
  • Joined: 08-May 08


Dream Kudos: 0

Post icon  Posted 08 May 2008 - 05:59 PM

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)

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

}
 

Was This Post Helpful? 0
  • +
  • -


#2 pbl  Icon User is offline

  • Java Lover
  • Icon
  • Group: Mentors
  • Posts: 11,168
  • Joined: 06-March 08


Dream Kudos: 475

Posted 08 May 2008 - 06:34 PM

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

 
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

// 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: 08 May 2008 - 07:22 PM

Was This Post Helpful? 0
  • +
  • -

#3 Den2000  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 9
  • Joined: 08-May 08


Dream Kudos: 0

Posted 08 May 2008 - 07:25 PM

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

Dennis
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • Java Lover
  • Icon
  • Group: Mentors
  • Posts: 11,168
  • Joined: 06-March 08


Dream Kudos: 475

Posted 08 May 2008 - 08:33 PM

View PostDen2000, on 8 May, 2008 - 08:25 PM, said:

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: 08 May 2008 - 08:33 PM

Was This Post Helpful? 0
  • +
  • -

#5 potator  Icon User is offline

  • D.I.C Head
  • Icon
  • Group: Contributors
  • Posts: 78
  • Joined: 02-December 07


Dream Kudos: 175

Posted 09 May 2008 - 04: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:
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);


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • Java Lover
  • Icon
  • Group: Mentors
  • Posts: 11,168
  • Joined: 06-March 08


Dream Kudos: 475

Posted 09 May 2008 - 05:38 AM

View Postpotator, on 9 May, 2008 - 05:50 AM, said:

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:
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 ?
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month