Results 1 to 4 of 4

Thread: Linklist

  1. #1
    buckey is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default Linklist

    i'm doing a simple linklist of arrays.

    anyway i have my node class which is a basic class
    next, prev, int[] arr

    has set and gets for all variables.

    then i have my super class with my head and tail

    i'm doing a simple insert

    Node temp;
    temp = new Node(null,nums);
    if (tail != null)
    {
    temp.set_data(nums);
    tail.set_next(temp);
    temp.set_prev(tail);
    tail = temp;
    }
    else
    {
    head = tail = temp;
    }

    However when i go to print out the list its the total size of the list but only prints out the last element

    so suppose its a list of 3 elements

    first element would be 1, 2, 3
    2nd would be 4, 5, 6
    3rd would be 7, 8, 9

    well it prints out 3 elements of 7, 8, 9 instead of all 3 elements that were inserted

    any ideas on what it could.

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    your code looks correct. show us more code...

    also, use code tags to make your code more readable (there's a button in the toolbar for that).

  3. #3
    buckey is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    Here is what i've written so far

    Java Code:
    public class number{
    
    		private Node head;
    	private Node tail;
    	
    	public number()
    	{
    		int[] num = new int[6];
    		Node temp = new Node(null,num);
    		this.head = this.tail = temp;
    		
    	}
    	
    	public void print(PrintStream outfile)throws IOException
    	{
    		Node current;
    		int counter = 0;
    		for (current = head.next ; current != null; current = current.return_next())
    		{
    			++counter;
    			int[] numr = new int[6];
    			numr = current.return_data();
    			for (int i = 0; i <=5; i = i+1)
    			{	outfile.println(numr[i]);}
    			outfile.println("COUNTER" + counter);
    		}
    	counter = 0;
    	}
    	
    	public void insert(int[] nums)
    	{
    		Node temp;
    		temp = new Node(null,nums);
    		if (tail != null)
    		{
    			temp.set_data(nums);
    			tail.set_next(temp);
    			temp.set_prev(tail);
    			tail = temp;
    		}
    		else
    		{
    			head = tail = temp;
    		}
    			
    	}
    
    	
    
    	
    	public static void main(String[] args) throws IOException
    	{
    		number a = new number(); 
    		String filenumber;
    		
    		int[] numbr= new int[6];
    		int countr = 0;
    		FileInputStream stuff = new FileInputStream("C:/Documents and Settings/bbuckey/Desktop/numbers2.txt");
    		InputStreamReader stuffs = new InputStreamReader(stuff);
    		BufferedReader reads = new BufferedReader(stuffs);
    		FileOutputStream outext = new FileOutputStream("C:/Documents and Settings/bbuckey/Desktop/out.txt");
    		PrintStream outfile= new PrintStream(outext);
    		while(true)
    		{
    			countr++;
    			filenumber = reads.readLine();
    			if(filenumber == null)break;
    			filenumber = filenumber.trim();
    			//System.out.println(filenumber);
    			int count;
    			count = 0;
    			for (int i = 0; i <= filenumber.length(); i = i+4)
    			{
    
    				numbr[count] = Integer.parseInt(filenumber.substring(i, i+2));
    				count = count + 1;
    			}
    			for (int i = 0; i <=5; i = i+1)
    				System.out.println(numbr[i]);
    				System.out.println(countr);
    			a.insert(numbr);
    			//a.printa(outfile);
    		}
    
    		a.print(outfile);
    		outfile.close();
    		reads.close();
    	}
    
    }
    
    
    /************************/
    /******* Node Class ********/
    
    
    public class Node {
    		
    		public Node next;
    		public Node prev;
    		public int[] numbers;
    		public Node(Node ne,int[] numb)
    		{
    			this.prev = ne;
    			this.next = ne;
    			this.numbers = numb;
    			//counter++;
    		}
    		public void set_next( Node current)
    		{
    			next = current;
    		}
    		
    		public void set_data(int[] arr){
    			numbers = arr;
    		}
    		
    		public void set_prev(Node current)
    		{
    			prev = current;
    		}
    		
    		public int[] return_data()
    		{
    			return numbers;
    		}
    		
    		public Node return_next()
    		{
    			return next;
    		}
    		
    		public Node return_prev()
    		{
    			return prev;
    		}
    		
    	}
    Last edited by buckey; 03-31-2010 at 02:40 AM.

  4. #4
    buckey is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    nevermind i figured it out it was a problem with how i declared my array outside the while loop in my main. Just for future reference why would that be an issue.

Similar Threads

  1. Sorted LinkList problem
    By koolaqua16 in forum Advanced Java
    Replies: 1
    Last Post: 08-08-2009, 06:49 AM

Posting Permissions

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