LinkList in java.

Discussion in 'Web Design & Coding' started by mooo, May 14, 2005.

  1. mooo

    mooo thecyberninja

    Messages:
    886
    Location:
    earth
    I am trying to work on java for my next semester class that starts in a week. The first lab starts out with linklist. It ask to make a linklist with all odd numbers from 1-100. I have that so far. Where i am lost is the skipindex. I have looked at java.sun.com documentation and have found nothing about skipindex.

    2) Set skipIndex to 1 or zero depending how you refer to the fist element in your list.
    3) Using skipIndex find the next number to be used as the skipFactor.
    4) Begin with the first node, set every element at skipFactor to zero.
    5) Remove the nodes containing zeros from list

    I am not asking for the answer just some sort of information on skipindex. I know how to remove duplicate numbers also. Just this skipindex thing is confusing me.


    Here is my code so far. I know i have made it sloppy, but its just temp till I can get the hang of this linklist stuff.

    Code:
       import java.util.*;
    
        public class LinkedListTest
       {  
           public static void main(
                      String[] args)
          {  List a = new LinkedList();
          
             int i = 1;
          
             while (i < 100)
             {
                a.add(i);
                i++;
                i++;
             }
             
          	
          	
             ListIterator aIter = a.listIterator();
             //aIter = a.iterator();
             
             while (aIter.hasNext())
             {  aIter.next(); 
                aIter.next();
             // skip one element
                if (aIter.hasNext()) 
                {  aIter.next(); 
                // skip next element
                   aIter.remove(); 
                // remove that element
                }
             }
          	
          	
             System.out.println(a);
          }
       }
    
     
  2. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    I think its something where you are supposed to write your own skip index method and then use that to either advance the index or remove the next list entry or something like that anyway
     
  3. mooo

    mooo thecyberninja

    Messages:
    886
    Location:
    earth
    hmm, know any websites where i can look information on this? My books don't say much info on skipindex.
     
  4. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA

    you are supposed to write the function yourself.

    I do homework on a $120 an hour basis. If you want to have this done, contact me at homework [at] x-istence [dot] com
     
  5. mooo

    mooo thecyberninja

    Messages:
    886
    Location:
    earth
    haha na i like to do it myself, won't learn if someone else did it :) Ijust like to look at examples of how certain things are implimented. Will wait for class on this one i guess.
     
  6. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    something along the lines of

    class MyLinkedList {
    public LinkedList myList;
    private int index;
    private void skipIndex() { index++; }
    }

    something like that, who knows
     
  7. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    I can picture you saying this to your teachers when they assign homework :p
     
  8. mooo

    mooo thecyberninja

    Messages:
    886
    Location:
    earth
    finished :) just have to make it look pretttty.


    Code:
    
    /*********************************************
    
    LuckyNumbers.java
    
    **********************************************/
    
    import java.io.*;
    
    class ListItem {
    	public int value;
    	ListItem next;
    }
    
    public class LuckyNumbers2 {
    	/**
    	 * Main program
    	 * 	inputs, stores and outputs a list of strings
    	 * @exception java.io.IOException
    	 * 	if any i/o error occurs
    	 */
    	public static void main(String[] args) throws java.io.IOException {
    
    		
    		ListItem head = null;
    		ListItem tail = null;
    
    		for (int i = 1; i < 10000; i=i+2)
     		{ 
    			if (head == null) {							// List empty.
    				head = new ListItem ();  						// Create listnode.
    				head.value = i;   								// Assign value.
    				tail = head;
    				tail.next = null;
    			} else {											// List non-empty.
    				tail.next = new ListItem ();					// Create listnode at rear.
    				tail.next.value = i;   							// Assign value. 
    				tail.next.next = null;
    				tail = tail.next;  								// Adjust rear pointer. 
    			}
    		} 
    		
    		System.out.println("List Built, Final Node = " + tail.value);
    		
    		int skipFactor = 3;
    		
    		ListItem current = head;
    		ListItem temp = null;
    		
    		//Figure out exactly how many loops are needed, quick and dirty hardcoded 500
    		for (int skipIndex = 1; skipIndex < 500; skipIndex++)
    		{
    			
    			//Set every skipFactor-th number to 0
    			current = head;
    			int loopCounter = 1;
    			while (current != null)
    			{
    				if (loopCounter == skipFactor)
    				{
    					current.value = 0;
    					loopCounter = 0;
    				}
    				loopCounter ++;
    				current = current.next;
    			}			
    					
    			//Delete all the 0's out of the list			
    			current = head;
    			ListItem last = null;
    			while (current != null)
    			{
    				if (current.value == 0)
    				{
    					if (last == null)
    						{ head = current.next; }
    					else
    						{ last.next = current.next; }
    																	//Need to delete unused nodes to avoid mem leak?
    					//temp = current;
    					//del temp;
    				}
    				last = current;
    				current = current.next;
    			}
    			
    		
    			//Print List - For debugging only /*
    			//String outputLine = "";
    			//current = head;
    			//for (int i = 0; i < 1000; i++)
    			//{
    			//	outputLine = outputLine + current.value + ", ";
    			//	current = current.next;
    			//}
    			//System.out.println(outputLine);				
    			
    			
    			//Set the next skipFactor
    			current = head;
    			for (int i = 0;i < skipIndex+1;i++)
    			{
    				current = current.next;
    			}
    			skipFactor = current.value;
    		}
    		
    		FileOutputStream out; // declare a file output object 
    		PrintStream p; // declare a print stream object 
    		try { // Create a new file output stream 
    				// connected to "myfile.txt" 
    				out = new FileOutputStream("myfile.txt"); // Connect print stream to the output stream 
    				p = new PrintStream( out );
    						
    				//Print List
    				String outputLine = "";
    				current = head;
    				for (int i = 0; i < 1000; i++)
    				{
    					outputLine = outputLine + current.value + ", ";
    					p.println (current.value);
    					current = current.next;
    				}
    				System.out.println(outputLine);				
    				 
    				p.close();
    		}
    		catch (Exception e)
    		{ 
    			System.err.println ("Error writing to file");
    		}
    	
    	}
    }
    
    
    
    
     
  9. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    does it compile?

    you might need to split the two classes up into separate files unless you make ListItem an inner class of LuckyNumbers
     
  10. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA

    Ive tried. :(
     
  11. mooo

    mooo thecyberninja

    Messages:
    886
    Location:
    earth
    yea it works fine for me :)