• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

LinkList in java.

mooo

thecyberninja
#1
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
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
 

X-Istence

*
Political User
#4
mooo said:
hmm, know any websites where i can look information on this? My books don't say much info on skipindex.

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
 

mooo

thecyberninja
#5
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
something along the lines of

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

something like that, who knows
 
#7
X-Istence said:
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
I can picture you saying this to your teachers when they assign homework :p
 

mooo

thecyberninja
#8
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
does it compile?

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

Members online

No members online now.

Latest posts

Latest profile posts

Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,961
Messages
673,239
Members
89,014
Latest member
sanoravies