need some java help

nburvi1

Solid State Member
Messages
6
well i am just lost about how to use linked lists what i need to do is this

Given a sequence of numbers, write a class named my_Special_Reverse to store them in a linked list and then print them in reverse order on the screen except the three numbers in the middle. For example, given “103 23 4 13 21 18 19” as input, your program should print out “19 18 4 13 21 23 103”. The input will have an odd number of integers.

and i am just lost about how to do it?
 
Code:
import java.util.*;

public class my_Special_Reverse
{
	public static void main(String args[])
	{
		LinkedList list = new LinkedList();
		for(int i = 0; i < args.length; i++)
		{
			list.add(args[i]);
		}
		printList(arrangeList(list));
	}
	private static LinkedList arrangeList(LinkedList l)
	{
		LinkedList list = new LinkedList();
		for(int i = 0; i < l.size(); i++)
		{
			list.add(l.get(i));
		}
		if(list.size() > 3)
		{
			int length = list.size();
			int x = ((length - 3) / 2);
			LinkedList list2 = new LinkedList();
			LinkedList list3 = new LinkedList();
			for(int i = (x - 1); i >= 0; i--)
			{
				list2.add(list.get(i));
			}
			for(int i = (length - x); i < length; i++)
			{
				list2.add(list.get(i));
			}
			for(int i = (list2.size() - 1); i >= 0; i--)
			{
				list3.add(list2.get(i));
			}
			for(int i = 0; i < x; i++)
			{
				list.set(i, list3.get(i));
			}
			int y = x;
			for(int i = (length - 1); i >= (length - x); i-- )
			{
				list.set(i, list3.get(y));
				y++;
			}
		}
		return list;
	}
	private static void printList(LinkedList l)
	{
		String text = "";
		for(int i = 0; i < l.size(); i++)
		{
			text += l.get(i) + " ";
		}
		System.out.println(text);
	}
}

This can be run from the command prompt, using the numbers you want to sort as arguments.
 
nice, thank you again for your help, my code was fairly similar the syntax was just off.
 
Back
Top Bottom