LinkList in java.

mooo

thecyberninja
Joined
6 Jan 2004
Messages
886
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);
      }
   }
 
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
 
hmm, know any websites where i can look information on this? My books don't say much info on skipindex.
 
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
 
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.
 
something along the lines of

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

something like that, who knows
 
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
 
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");
		}
	
	}
}
 
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 profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back