6 Replies - 14837 Views - Last Post: 09 October 2006 - 09:29 AM Rate Topic: -----

#1 youwish16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 06

merging 2 linked lists into 1

Posted 04 October 2006 - 11:56 AM

ok i gott merge 2 linked lists into 1 linked list. so i am trying to compare the first node of each list and put the smaller one in the 3rd list first then the bigger of the 2 in second and so on. this is what i have so far

import java.lang.*;
import java.util.*;
import java.io.*;
public class IntegerNode
{
  private int item;
  private IntegerNode next;
  
  public void setItem(int newItem)
  {
	item = newItem;
  }
  
  public int getItem()
  {
	return item;
  }
  
  public void setNext(IntegerNode nextNode)
  {
	next = nextNode;
  }
  
  public IntegerNode getNext()
  {
	return next;
  }
  public static void main(String[] args) throws IOException
  {
	IntegerNode n1 = new IntegerNode();
	IntegerNode n2 = new IntegerNode();
	IntegerNode n3 = new IntegerNode();
	IntegerNode nn1 = new IntegerNode();
	IntegerNode nn2 = new IntegerNode();
	IntegerNode nn3 = new IntegerNode();
	IntegerNode nnn1 = new IntegerNode();
	IntegerNode nnn2 = new IntegerNode();
	IntegerNode nnn3 = new IntegerNode();
	int x;
	String str;
	
	
	 FileReader freader = new FileReader("list.txt");
	 BufferedReader inputFile = new BufferedReader(freader);
	 
	 str = inputFile.readLine();
	 x = Integer.parseInt(str);	
	 n1.setItem(x);
	 
	 str = inputFile.readLine();
	 x = Integer.parseInt(str);
	 n2.setItem(x);
	 
	 str = inputFile.readLine();
	 x = Integer.parseInt(str);
	 n3.setItem(x);
	 
	 n1.setNext(n2);
	 n2.setNext(n3);
	 
	 str = inputFile.readLine();
	 x = Integer.parseInt(str);	
	 nn1.setItem(x);
	 
	 str = inputFile.readLine();
	 x = Integer.parseInt(str);
	 nn2.setItem(x);
	 
	 str = inputFile.readLine();
	 x = Integer.parseInt(str);
	 nn3.setItem(x);
	 
	 nn1.setNext(nn2);
	 nn2.setNext(nn3);
	 
	 if(n1.equals(nn1))
	 {
	   nnn1.setItem(nn1);
	   nnn1.setNext(n1);
	 }
	 else
	 {
	  nnn1.setItem(n1);
	  nnn1.setNext(nn1);
	 }
  }
}

it says i cant use setItem on integer nodes, what do i do now?

Is This A Good Question/Topic? 0
  • +

Replies To: merging 2 linked lists into 1

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: merging 2 linked lists into 1

Posted 04 October 2006 - 12:13 PM

You need to make 2 classes for your code. One will be your IntegerNode class. Make sure you create a constructor inside this class, so you can instatiate an object of it and be able to access its methods.

The other will contain your main method with all its code.
Was This Post Helpful? 0
  • +
  • -

#3 youwish16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 06

Re: merging 2 linked lists into 1

Posted 04 October 2006 - 12:47 PM

View Postjayman9, on 4 Oct, 2006 - 12:13 PM, said:

You need to make 2 classes for your code. One will be your IntegerNode class. Make sure you create a constructor inside this class, so you can instatiate an object of it and be able to access its methods.

The other will contain your main method with all its code.


isnt that what i did? i know thats what i tried to do lol
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: merging 2 linked lists into 1

Posted 04 October 2006 - 01:46 PM

Actually you have one class with several methods. I did not create a constructor in the IntegerNode class, I will leave that for you. But this is what two seperate classes looks like.

1st class:
import java.lang.*;
import java.util.*;
import java.io.*;
public class IntegerNode
{
  private int item;
  private IntegerNode next;
  
  public void setItem(int newItem)
  {
	item = newItem;
  }
  
  public int getItem()
  {
	return item;
  }
  
  public void setNext(IntegerNode nextNode)
  {
	next = nextNode;
  }
  
  public IntegerNode getNext()
  {
	return next;
  }
}


2nd class with main:
import java.lang.*;
import java.util.*;
import java.io.*;
public class IntegerTest
{
  public static void main(String[] args) throws IOException
  {
	IntegerNode n1 = new IntegerNode();
	IntegerNode n2 = new IntegerNode();
	IntegerNode n3 = new IntegerNode();
	IntegerNode nn1 = new IntegerNode();
	IntegerNode nn2 = new IntegerNode();
	IntegerNode nn3 = new IntegerNode();
	IntegerNode nnn1 = new IntegerNode();
	IntegerNode nnn2 = new IntegerNode();
	IntegerNode nnn3 = new IntegerNode();
	int x;
	String str;
	
	
	 FileReader freader = new FileReader("list.txt");
	 BufferedReader inputFile = new BufferedReader(freader);
	
	 str = inputFile.readLine();
	 x = Integer.parseInt(str);	
	 n1.setItem(x);
	
	 str = inputFile.readLine();
	 x = Integer.parseInt(str);
	 n2.setItem(x);
	
	 str = inputFile.readLine();
	 x = Integer.parseInt(str);
	 n3.setItem(x);
	
	 n1.setNext(n2);
	 n2.setNext(n3);
	
	 str = inputFile.readLine();
	 x = Integer.parseInt(str);	
	 nn1.setItem(x);
	
	 str = inputFile.readLine();
	 x = Integer.parseInt(str);
	 nn2.setItem(x);
	
	 str = inputFile.readLine();
	 x = Integer.parseInt(str);
	 nn3.setItem(x);
	
	 nn1.setNext(nn2);
	 nn2.setNext(nn3);
	
	 if(n1.equals(nn1))
	 {
	   nnn1.setItem(nn1);
	   nnn1.setNext(n1);
	 }
	 else
	 {
	  nnn1.setItem(n1);
	  nnn1.setNext(nn1);
	 }
  }
}

Was This Post Helpful? 0
  • +
  • -

#5 youwish16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 06

Re: merging 2 linked lists into 1

Posted 04 October 2006 - 08:06 PM

oh i see, you made it 2 seperate files that need each other to run. so this way it will let me use setItem with nodes? sorry about all the questions, im new to programming. thanks, if i have anymore questions, ill ask
Was This Post Helpful? 0
  • +
  • -

#6 youwish16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 06

Re: merging 2 linked lists into 1

Posted 08 October 2006 - 12:52 PM

ok i did everything you said to but it still wont let me use setItem with integernodes like a said before? how do i take a value of a linked list and add it to another linked list?
Was This Post Helpful? 0
  • +
  • -

#7 youwish16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 06

Re: merging 2 linked lists into 1

Posted 09 October 2006 - 09:29 AM

someone please point me in the correct direction, i really need help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1