Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 02-08-07   #1 (permalink)
WaterCooler
 
killnine's Avatar
 
intel ati

Join Date: Aug 2005
Posts: 2,672

Rep: 137 killnine is acknowledged by manykillnine is acknowledged by many
Unique Rep: 122
FAQs Submitted: 1
Trader Rating: 8
Default Java: Linked List Help

Hey guys,
I know this isn't much of a programming forum, but I am working on a small java app that takes two doubly-linked lists and concatenates them. Therefore, you have list L and M, and M is smashed onto the end of the list L.

However, I cant seem to find a working way of doing this. See below:

Code:
		DNode list_L_1 = new DNode("Value 1",null,null);
		DNode list_L_2 = new DNode("Value 2",null,null);
		DNode list_L_3 = new DNode("Value 3",null,null);
		DNode list_L_4 = new DNode("Value 4",null,null);
		
		DNode list_M_1 = new DNode("Value Add 1",null,null);
		DNode list_M_2 = new DNode("Value Add 2",null,null);
		DNode list_M_3 = new DNode("Value Add 3",null,null);
		DNode list_M_4 = new DNode("Value Add 4",null,null);
		
		/* Declaring List to Call Later to Test Methods*/
		
		DList list_L = new DList();
		list_L.addFirst(list_L_1);
		list_L.addAfter(list_L_1,list_L_2);
		list_L.addAfter(list_L_2,list_L_3);
		list_L.addAfter(list_L_3,list_L_4);
		System.out.println(list_L);
		
		DList list_M = new DList();
		list_M.addFirst(list_M_1);
		list_M.addAfter(list_M_1,list_M_2);
		list_M.addAfter(list_M_2,list_M_3);
		list_M.addAfter(list_M_3,list_M_4);
		System.out.println(list_M);		
				
		/* Below I Test My Many Amazing Methods */
		testConcatenate(list_L,list_M);
	}
	
	public static void testConcatenate(DList L, DList M)
	{
		L.concatenate(M);
		System.out.println(L);
	}
So here I create some lists, called list_L and list_M. Then I call the following method to concatenate them (called "concatenate()"):

Code:
  public void concatenate(DList M)
  {
	  /*If both lists null, return null */
	  if ((this.size == 0) && (M.size == 0))
	  {
		  System.out.println("List Error: Both Lists Null, Aborting...");
	  }

	  else 
	  {
		  DList Mcopy = M;
		  		  		
		  int length = M.size;
		  for (length = M.size; length > 1; length --)
		  {
			  DNode t = M.getFirst();
			  this.addAfter(trailer.getPrev(),t);
			  M.remove(M.getFirst());
			  
		  }
		  M = Mcopy;	//Return shizzle back to normalz, yo.
		  System.out.println("Concatenation Complete");
	  }	  
	  
  }
However, all it does is print out the original list_L. When I step through this thing, it adds the node onto the end, but after "M.remove(M.getFirst());", it ALSO REMOVES IT FROM where it put it on List_L. Crazy.... help?

System: Teh System
CPU
Q6600@ 3.2Ghz (9x355)
Motherboard
Asus P5W-DH
Memory
Crucial Ballistix (4x1024)
Graphics Card
VisionTek 4870
Hard Drive
Seagate 7200rpm (160)
Sound Card
X-fi Fatal1ty Pro
Power Supply
Silverstone Strider 750W
Case
P180B
CPU cooling
OCZ Vendetta 2
GPU cooling
HR-03 GT
OS
XP Professional
Monitor
Dell 2001FP 20.1"
killnine is offline   Reply With Quote
Old 02-08-07   #2 (permalink)
Audiophile
 
welfinator's Avatar
 
intel nvidia

Join Date: Jan 2007
Location: New Jersey
Posts: 243

Rep: 14 welfinator Unknown
Unique Rep: 13
Trader Rating: 0
Default

i'm not that good with double linked lists but don't you have to set a pointer from the object to the next and to the previous item?
__________________
Think for yourself...

http://www.last.fm/user/germanguru69/
^stuff i listen to

http://hs.facebook.com/profile.php?id=1342020499
^add me on facebook

System: Dell XPS M1530
CPU
Intel® Core™ 2 Duo Processor T8300 (2.4GHz/800Mhz
Memory
4GB Shared Dual Channel DDR2 SDRAM at 667MHz (2 Di
Graphics Card
256MB NVIDIA® GeForce® 8600M GT
Hard Drive
Size: 320GB 5400rpm SATA Hard Drive
Sound Card
High Definition Audio 2.0
Power Supply
85 WHr 9-cell Lithium Ion Primary Battery
Case
Tuxedo Black
OS
Windows Vista
Monitor
Full Hi Definition, glossy widescreen 15.4 inch LC
welfinator is offline   Reply With Quote
Old 02-09-07   #3 (permalink)
WaterCooler
 
killnine's Avatar
 
intel ati

Join Date: Aug 2005
Posts: 2,672

Rep: 137 killnine is acknowledged by manykillnine is acknowledged by many
Unique Rep: 122
FAQs Submitted: 1
Trader Rating: 8
Default

Yeah, that is correct. However, I have build the methods addAfter, addBefore, and addFirst to do this automatically. Actually, they were methods provided by my professor, so I know they work.

System: Teh System
CPU
Q6600@ 3.2Ghz (9x355)
Motherboard
Asus P5W-DH
Memory
Crucial Ballistix (4x1024)
Graphics Card
VisionTek 4870
Hard Drive
Seagate 7200rpm (160)
Sound Card
X-fi Fatal1ty Pro
Power Supply
Silverstone Strider 750W
Case
P180B
CPU cooling
OCZ Vendetta 2
GPU cooling
HR-03 GT
OS
XP Professional
Monitor
Dell 2001FP 20.1"
killnine is offline   Reply With Quote
Old 02-09-07   #4 (permalink)
110100001101001111000
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,832

Rep: 283 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 215
FAQs Submitted: 6
Folding Team Rank: 237
Hardware Reviews: 9
Trader Rating: 1
Default

Questions first.
1) What does your Dnode class look like? Are there references for next node AND previous nodes?

2) For your node constructors, do you automatically initiliaze the next reference to null?

3) Are you trying to make a new linked list that's a concatenation of the two, or are you just trying to tack one on the end of an existing one?

4) Are you using a dummy node for your head, or does your first node contain actuall data?

This is how I'd do it. Make sure your constructor makes this.next = null. That way, you know if you ever reach a node where this.next==null, you've reached the end of your list. This way you can simply do a search and make the next reference from your last node, equal to the first node from the other list.

public void concatenate(DNode M){

DNode lastNode = this.next; // create a node to track your position

while(lastNode.next != null){
lastNode = lastNode.next;
}

//when you get out of this loop, lastNode will be pointing to your last node

lastNode.next = M;
// this adds the link from your last node in L to point to the first one in M

}


You'll have to mess around with this because I didn't have a chance to compile or check for syntax, but you get the idea of what I'm trying to do. Basically do these 2 steps:

1) find your last node in list L
2) point the next value from the last node in L to M

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business 32-Bit
Monitor
HP F2105 21" & Samsung 712N
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Old 02-09-07   #5 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,975
Blog Entries: 1

Rep: 564 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

For the declaration i would say
Code:
DList list_L = new DList(), list_M = new DList();
for(int i = 0; i < 4; i++)
{
   list_L.addLast(new DNode("Value Add " + (i + 1), NULL, NULL)
   list_M.addLast(new DNode("Value Add " + (i + 1), NULL, NULL)
}
as for the method

Code:
public void concatenate(DList M)
{
  if(!this && !M)
     System.out.println("List Error: Both Lists Null, Aborting...");
  else
      for(int i = 0; i < M.length; i++)
          addLast(M.get(i));
}
**EDIT**
@ Cyberd
the dnode code doesn't matter since linkedlists use and return objects

**ANOTHER EDIT**
Does Dlist have the same methods as the linkedList class? If not I'll need to see the Dlist code to help you

**yet another edit**
Since this is java (OO) when you remove from list M you are also removing from Mcopy
Saying Mcopy = M does not create a copy of M - it just links Mcopy and M to the same data
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw

Last edited by rabidgnome229 : 02-09-07 at 09:34 PM.
rabidgnome229 is offline Overclocked Account   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 01:23 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.24539 seconds with 8 queries