Results 1 to 18 of 18
  1. #1
    Get_tanked is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default copying a linked list

    I need to write code that will copy the contents of a linked list to another linked list. Unfortunately I'm having trouble doing this, I've never used a linked list before so its probably something very simple that I am missing. When I run my main program which I didn't post here I either get a null pointer exception or it doesn't print anything out I would really appreciate it if someone could help me out.

    thanks

    Java Code:
    public class Intcoll4 
    {
    	private ListNode c;
    	private int how_many;
    	
    	public Intcoll4()//constructure
    	{
    		c = null;
    		how_many = 0;
    	}
    	
    	 
    	public void copy(Intcoll4 obj)//this is the method I'm having trouble with
    	{
    		if (this != obj)
    		{
    			this.how_many = obj.how_many;
    			
    			ListNode q = new ListNode();
    			q = obj.c;
    			System.out.println(q.info);
    			this.c = q;
    			   
    		}
    	}
    	
    	private class ListNode//Class for my list nodes
    	{
    		private int info;
    		private ListNode link;
    		
    		public ListNode()
    		{
    			info = 0;
    			link = null;
    		}
    	}
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Your code doesn't copy anything. All it does is get 2 variables to refer to the same object.

    Java Code:
    private class ListNode {
        private ListNode link;
    This is dangerous.

  3. #3
    Get_tanked is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    what i'm trying to do do is save whats in obj's link list into q and then have the this obj point to q so whats in this will be the same as whats in obj. Any ideas how i could do that? Also, I know its dangerous making it private but unfortunately thats the way I have to do it.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Get_tanked View Post
    what i'm trying to do do is save whats in obj's link list into q and then have the this obj point to q so whats in this will be the same as whats in obj. Any ideas how i could do that?
    You will want to read about cloning and deep cloning.

    Also, I know its dangerous making it private but unfortunately thats the way I have to do it.
    I wasn't talking about accessability (private/public). I was talking about creating an instance of a class inside that same class. What happens? You create a Foo object so it calls the constructor which creates a Foo object which calls the constructor which creates a Foo object which calls the constructor etc etc etc.

  5. #5
    Get_tanked is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    I'm still confused do i have to copy both the head and the tail of the linked list into q? If so how would i know what the tail is?

  6. #6
    Get_tanked is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    I am still having trouble with this could someone help me out. I need to make q point to objs linked list and then make the this obj point to q.

  7. #7
    Get_tanked is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    I hate to be nagging like this but ive hit a brick wall no matter what i do i keep getting a null pointer exception and i have no clue what I'm supposed to do. A little more help other than telling me to read about cloning would be GREATLY appreciated. This thing is driving me crazy ive been working on this one method for about 12+ hours, sorry if i seem a little persistent but my frustration level is at about a 9 right now lol.

    Thank you so much

  8. #8
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Java Code:
    q = obj.c;
    How do you acces the c field?

  9. #9
    Get_tanked is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    my thought was that obj.c points to the first node in the list, and since the first node points to the next one and so on, by setting q = obj.c, q would also point to the first node which point to the rest of them.

  10. #10
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Java Code:
    public class Intcoll4 
    {
    	[COLOR="red"][U]private[/U][/COLOR] ListNode c;
    	private int how_many;
    	
    	public Intcoll4()//constructure
    	{
    		c = null;
    		how_many = 0;
    	}
    	
    	 
    	public void copy(Intcoll4 obj)//this is the method I'm having trouble with
    	{
    		if (this != obj)
    		{
    			this.how_many = obj.how_many;
    			
    			ListNode q = new ListNode();
    			q = [U][COLOR="Red"]obj.c[/COLOR][/U];
    			System.out.println(q.info);
    			this.c = q;
    			   
    		}
    	}
    	
    	private class ListNode//Class for my list nodes
    	{
    		private int info;
    		private ListNode link;
    		
    		public ListNode()
    		{
    			info = 0;
    			link = null;
    		}
    	}
    }
    See the problem?

  11. #11
    Get_tanked is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    is the problem that it is private? if so how would i access it with it being private, because the member c has to be private.

    also, thanks a lot for helping me out :D

  12. #12
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Java Code:
    public class Intcoll4 
    {
    	private ListNode c;
    	private int how_many;
    	
    	public Intcoll4()//constructure
    	{
    		c = null;
    		how_many = 0;
    	}
    	
    	 
    	public void copy(Intcoll4 obj)//this is the method I'm having trouble with
    	{
    		if (this != obj)
    		{
    			this.how_many = obj.how_many;
    			
    			ListNode q = new ListNode();
    			q = obj.c;
    			System.out.println(q.info);
    			this.c = q;
    			   
    		}
    	}
    	[COLOR="Red"]public ListNode getC()
    	{
    	return ....;//<--something goes on the dots
    	}[/COLOR]
    	private class ListNode//Class for my list nodes
    	{
    		private int info;
    		private ListNode link;
    		
    		public ListNode()
    		{
    			info = 0;
    			link = null;
    		}
    	}
    }

  13. #13
    Get_tanked is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Still kinda confused but after I get obj.c would my code work?

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by imorio View Post
    See the problem?
    No I don't. Objects can access the private instance variables of other objects as long as they are of the same class.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Get_tanked View Post
    Still kinda confused but after I get obj.c would my code work?
    I still don't understand what your problem is. As I mentioned before your code does not copy. All it does is get 2 Intcoll4 objects point at the same ListNode object.

  16. #16
    Get_tanked is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    my problem is that I want q to point to obj's list nodes, so that q would essentially be a copy of obj. I thought that by setting q equal to objs head (which i thought was obj.c) it would then point to all of objs other nodes.

    After I have a copy of obj in q I want to make the this.obj point to q's head, so that this.obj is a copy of q. essentially I'm trying to make a copy of a copy.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Once again you are not making a copy. If you want to copy then you need to do as I suggested earlier and read about cloning.

    Here is some code that highlights what you are trying to do and the problem it causes.
    Java Code:
    class Foo {
        Bar babar;
    
        Foo(Bar b) {
            babar = b;
        }
        public void change(String c) {
            babar.data = c;
        }
    
        public void doStuff(Foo other) {
            babar = other.babar;
        }
    
        public String getData() {
            return babar.data;
        }
    
        public static void main(String[] args) {
            Bar b1 = new Bar("Hello");
            Bar b2 = new Bar("Bye");
            Foo f1 = new Foo(b1);
            Foo f2 = new Foo(b2);
            System.out.println(f1.getData());
            System.out.println(f2.getData());
            f1.doStuff(f2);
            f1.change("Oh No!");
            System.out.println(f1.getData());
            System.out.println(f2.getData());
            f2.change("What is wrong?");
            System.out.println(f1.getData());
            System.out.println(f2.getData());
        }
    }
    
    class Bar {
        String data;
        
        Bar(String s) {
            data = s;
        }
    }
    No matter which Foo object gets changed they both see the change. Because the code does not make a copy but rather just had both Foo objects point at the same Bar object. You are trying to do the very same thing.

  18. #18
    Get_tanked is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  2. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  3. Need help in linked list
    By Hotzero in forum New To Java
    Replies: 17
    Last Post: 06-05-2010, 08:13 AM
  4. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM
  5. deep copying arraylist to add to a list
    By alvations in forum New To Java
    Replies: 13
    Last Post: 10-08-2008, 03:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •