Lara Luu hace 10 años
padre
commit
586ef1a268
Se han modificado 2 ficheros con 66 adiciones y 6 borrados
  1. 13 6
      Lab11/src/LList.java
  2. 53 0
      Lab11/src/SeperateAndMerge.java

+ 13 - 6
Lab11/src/LList.java

@@ -52,13 +52,20 @@ public class LList<T> implements Iterable<T> {
 	
 	public void add(T item)
 	{
-		//Handle empty list
-		Node cur = head;
-		while (cur.next != null)
+		if (head == null)
 		{
-			cur = cur.next;
+			head = new Node();
+			head.elm = item;
+		}
+		else
+		{
+			Node cur = head;
+			while (cur.next != null)
+			{
+				cur = cur.next;
+			}
+			cur.next = new Node();
+			cur.next.elm = item;
 		}
-		cur.next = new Node();
-		cur.next.elm = item;
 	}
 }

+ 53 - 0
Lab11/src/SeperateAndMerge.java

@@ -0,0 +1,53 @@
+import java.util.Iterator;
+import java.util.Scanner;
+
+
+public class SeperateAndMerge {
+
+	/**
+	 * @param args
+	 */
+	public static void main(String[] args) {
+		LList<Integer> listInt = new LList<Integer>();
+		LList<Float> listFloat = new LList<Float>();
+		Scanner in = new Scanner(System.in);
+		while (in.hasNext())
+		{
+			if (in.hasNextInt())
+			{
+				listInt.add(in.nextInt());
+			}
+			else if (in.hasNextFloat())
+			{
+				listFloat.add(in.nextFloat());
+			}
+			else
+			{
+				in.next();
+			}
+		}
+		in.close();
+		System.out.print("Inputted values:");
+		Iterator<Integer> intIter = listInt.iterator();
+		Iterator<Float> fltIter = listFloat.iterator();
+		while (intIter.hasNext() && fltIter.hasNext())
+		{
+			System.out.print(" ");
+			System.out.print(intIter.next());
+			System.out.print(" ");
+			System.out.print(fltIter.next());
+		}
+		while (intIter.hasNext())
+		{
+			System.out.print(" ");
+			System.out.print(intIter.next());
+		}
+		while (fltIter.hasNext())
+		{
+			System.out.print(" ");
+			System.out.print(fltIter.next());
+		}
+		System.out.println();
+	}
+
+}