Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: What's wrong?!

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong?!

    Why doesn't my printLinked() work after the invert() ??
    And why doesn't my printLinked "System.out.println();" work? It doesn't give me a blank line...

    And extra credits to anyone who can give me a hint on what I need to make my delrep() deleate all duplicates not only the first one I refer to, a loop to do ref = ref.link and continue until ref.link = null.

    import java.util.Scanner;
     
    public class LinkedList
    {
    	public static void main(String []args)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		ListNode head = new ListNode();
    		head.link = null;
    		ListNode tail = head;
    		System.out.print("Enter integers, seperated with blanks, to put into a linked list (type zero (0) to end): ");
    		int in;
    		in = keyboard.nextInt();
    		while (in != 0)
    		{
    			ListNode x = new ListNode(); 
    			x.link = null;
    			x.data = in;
    			tail.link = x;
    			tail = x;
    			in = keyboard.nextInt();
    		}
    		printLinked(head.link);
    		delrep(head.link);
    		printLinked(head.link);
    		invert(head);
    		printLinked(head.link);	
    	}
    	/** The printLinked() method prints out, in order, the integers stored in the linked list.
    	 */
    	public static void printLinked(ListNode head)
    	{
    		while (head != null)
    		{
    			System.out.print(head.data + " ");
    			head = head.link;
    		}
    		System.out.println();
    	}
    	public static void delrep(ListNode head)
    	{
    		ListNode seek = head.link;
    		ListNode ref = head;
    		ListNode c = ref;
    		while (seek != null)
    		{
    			if (seek.data == ref.data)
    			{
    				seek = seek.link;
    				c.link = c.link.link;
    			}
    			else
    			{
    				seek = seek.link;
    				c = c.link;
    			}		
    		}
    	}
    	public static void invert (ListNode head)
    	{
    		ListNode lead = head.link.link;
    		ListNode trail = head.link;
    		ListNode secondtrail = head;
    		head.link = null;
    		trail.link = null;
    		while (lead.link != null)
    		{
    			secondtrail = trail;
    			trail = lead;
    			lead = lead.link;
    			trail.link = secondtrail;
    		}
    		head.link = lead;
     
    	}
     
    }
    Last edited by helloworld922; February 20th, 2010 at 05:19 PM.


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?!

    And why doesn't my printLinked "System.out.println();" work? It doesn't give me a blank line... // sorry about that, it actually does work

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?!

    Well if I am following your code correctly the reason why your delrep() only deletes the first occurence is because there is nothing telling it to continue deleting after the first occurence.

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?!

    Yes I know that, I want to need how to write to make it deleate all duplicates

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What's wrong?!

    I'm guessing you have a singly linked list? You need to make sure that you are modifying your links in the correct order, or else all references to parts or even all of the list can be lost.

    A simple O(n^2) algorithm for deleting all duplicates:

    1. Take the first value of you list and call that your "holding" value.
    2. Iterate through the entire list. If any of the other values match your holding value, remove it.
    3. Take the item after holding as your new holding value. Repeat step 2 until you reach the end of the list.

  6. #6
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?!

    Yes okay but IO don't really know how to make that happen.. While loop and if statements? Im stuck and can't figure out how to write the loop for this reference node to go through the entire list.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What's wrong?!

    Well, you just need some way to distinguish the end of the list. This can either be that the link of the last node returns null, or you can put a special end-of-list node to the end of your list. Iterating through a linked list is as simple as creating a node to point to the head, then simply update that node to be node.link

    // iterate through the list, printing out the data
    ListNode iterator = head;
    while (head.link != null)
    {
         System.out.println(iterator.data);
         iterator = iterator.link;
    }

    If you haven't done so already, implement a equals() method to whatever data it is you're putting into your nodes. Then modify the above code to have an if statement that will check if iterator's data is equal to a held node's data.

    Once you have this figured out, changing the held node is as simple as duplicating the above loop code, except move the held node's reference rather than the iterator's, and put everything for the iterator inside of this new bigger loop.

Similar Threads

  1. don't know what's wrong
    By james in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 15th, 2010, 07:37 PM
  2. Where am I going wrong? :)
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 18th, 2009, 12:03 AM
  3. whats wrong with this one....
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 6th, 2009, 10:08 AM
  4. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM
  5. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM

Tags for this Thread