浏览代码

finish lab 9, lab 11 init

Lara Luu 10 年之前
父节点
当前提交
0f78cdc990
共有 6 个文件被更改,包括 216 次插入12 次删除
  1. 6 0
      Lab11/.classpath
  2. 17 0
      Lab11/.project
  3. 12 0
      Lab11/.settings/org.eclipse.jdt.core.prefs
  4. 64 0
      Lab11/src/LList.java
  5. 97 0
      Lab9/src/MyListDriver.java
  6. 20 12
      Lab9/src/MySortedList.java

+ 6 - 0
Lab11/.classpath

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
Lab11/.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>Lab11</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

+ 12 - 0
Lab11/.settings/org.eclipse.jdt.core.prefs

@@ -0,0 +1,12 @@
+#Wed Oct 21 11:11:01 PDT 2015
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6

+ 64 - 0
Lab11/src/LList.java

@@ -0,0 +1,64 @@
+import java.util.Iterator;
+
+public class LList<T> implements Iterable<T> {
+	
+	private Node head;
+	
+	private class Node
+	{
+		public T elm;
+		public Node next;
+	}
+	
+	private class Iter implements Iterator<T>
+	{
+		private Node next;
+		
+		{
+			next = head;
+		}
+		
+		@Override
+		public boolean hasNext() {
+			return next != null;
+		}
+
+		@Override
+		public T next() {
+			try
+			{
+				return next.elm;
+			}
+			finally
+			{
+				next = next.next;
+			}
+		}
+
+		@Override
+		public void remove() {
+			throw new UnsupportedOperationException();
+		}
+	}
+	
+	{
+		head = null;
+	}
+	
+	public Iterator<T> iterator()
+	{
+		return new Iter();
+	}
+	
+	public void add(T item)
+	{
+		//Handle empty list
+		Node cur = head;
+		while (cur.next != null)
+		{
+			cur = cur.next;
+		}
+		cur.next = new Node();
+		cur.next.elm = item;
+	}
+}

+ 97 - 0
Lab9/src/MyListDriver.java

@@ -0,0 +1,97 @@
+import java.util.Scanner;
+
+public class MyListDriver 
+{
+	
+	public static void main(String[] args)
+	{
+		MySortedList myList = new MySortedList();
+		Scanner in = new Scanner(System.in);
+		printHelp();
+		boolean running = true;
+		while (running)
+		{   System.out.println("Enter a menu choice: ");
+			String line = in.nextLine();
+			if (line.length() != 1)
+			{
+				System.out.println("Invalid Option.");
+				//printHelp();
+				continue;
+			}
+			char command = line.charAt(0);
+			switch (command)
+			{
+				case 'a':
+					System.out.println("Enter an integer to add.");
+					if (in.hasNextInt())
+					{
+						int newVal = in.nextInt();
+						myList.add(newVal);
+						System.out.println(newVal+" added");
+					}
+					else
+					{
+						System.out.println("Not an integer.  No action taken.");
+					}
+					in.nextLine();
+					break;
+				case 'd':
+					System.out.println("Enter an integer to delete.");
+					if (in.hasNextInt())
+					{
+						int newVal = in.nextInt();
+						myList.delete(newVal);
+						System.out.println(newVal+" delete");
+					}
+					else
+					{
+						System.out.println("Not an integer.  No action taken.");
+					}
+					in.nextLine();
+					break;
+				case 'x':
+					if (myList.isEmpty())
+					{
+						System.err.println("List is empty. No max found.");
+						continue;
+					}
+					System.out.println("The max value is: " + myList.max());
+					break;
+				case 'm':
+					if (myList.isEmpty())
+					{
+						System.err.println("List is empty. No min found.");
+						continue;
+					}
+					System.out.println("The min value is: " + myList.min());
+					break;
+				case 'e':
+					System.out.println(myList.isEmpty()? "Empty":"Not Empty");
+					break;
+				case 'p':
+					myList.print();
+					break;
+				case 'q':
+					running = false;
+					System.out.println("Quitting");
+					break;
+				default:
+					System.out.println("Invalid Option.");
+					//printHelp();
+			}
+		}
+	}
+	
+	public static void printHelp()
+	{
+		System.out.println("Options:");
+		System.out.println("\"a\" -\tAdd to List");
+		System.out.println("\"d\" -\tRemove from List");
+		System.out.println("\"x\" -\tChecks for max");
+		System.out.println("\"m\" -\tChecks for min");
+		System.out.println("\"e\" -\tCheck if List is empty");
+		System.out.println("\"p\" -\tPrint");
+		System.out.println("\"q\" -\tQuit");
+	}
+		
+}

+ 20 - 12
Lab9/src/MySortedList.java

@@ -1,5 +1,3 @@
-import org.omg.CORBA.Current;
-
 
 public class MySortedList
 {
@@ -42,7 +40,7 @@ public class MySortedList
 			Node current = new Node(newElm);
 			Node tmpHead = head;
 			
-			while ( tmpHead.next.element < current.element)
+			while (tmpHead.next != null && tmpHead.next.element < current.element)
 			{
 				tmpHead = tmpHead.next;
 			}
@@ -55,15 +53,25 @@ public class MySortedList
 	
 	public void delete(int elm)
 	{
-		if (head == null)
-		{
-			//DO NOTHING
-		}
-		else
+		if (head != null)
 		{
-			//Set tmp variable head, iterate w/while until tmp.next == null
-			//Stop when tmp.elem = elm
-			//
+			if (head.element == elm)
+			{
+				head = head.next;
+			}
+			else
+			{
+				Node tmp =  head;
+				while(tmp.next != null && tmp.next.element <= elm)
+				{
+					if (tmp.next.element == elm)
+					{
+						tmp.next = tmp.next.next;
+						return;
+					}
+					tmp = tmp.next;
+				}
+			}	
 		}
 	}
 	
@@ -87,7 +95,7 @@ public class MySortedList
 	public void print()
 	{
 		Node tmpHead = head;
-		while (tmpHead.next != null)
+		while (tmpHead != null)
 		{
 			System.out.print(tmpHead.element + " " );
 			tmpHead = tmpHead.next;