Sfoglia il codice sorgente

add Hash Table Lab

Lara Luu 10 anni fa
parent
commit
f13a58ed6d

+ 6 - 0
Lab12/.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.7"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
Lab12/.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>Lab12</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
Lab12/.settings/org.eclipse.jdt.core.prefs

@@ -0,0 +1,12 @@
+#Mon Nov 02 11:08:00 PST 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

+ 63 - 0
Lab12/src/HashTableSC.java

@@ -0,0 +1,63 @@
+import java.util.LinkedList;
+
+
+public class HashTableSC<T>
+{
+	private LinkedList<T>[] elements;
+	
+	@SuppressWarnings("unchecked")
+	public HashTableSC(int size)
+	{
+		elements = (LinkedList<T>[]) new LinkedList[size];
+		for (int i = 0; i < size; i++)
+		{
+			elements[i] =  new LinkedList<T>();
+		}
+	}
+	
+	private int hash(T input)
+	{
+		return input.hashCode() % elements.length;
+	}
+	
+	public void insert(T elm)
+	{
+		elements[hash(elm)].addFirst(elm);
+	}
+	
+	public void delete(T elm)
+	{
+		elements[hash(elm)].remove(elm);
+	}
+	
+	public boolean find(T elm)
+	{
+		return elements[hash(elm)].contains(elm);
+	}
+	
+	public boolean isEmpty()
+	{
+		for (LinkedList<T> list : elements)
+		{
+			if (list.size() > 0)
+			{
+				return false;
+			}
+		}
+		return true;
+	}
+	
+	public void print()
+	{
+		int i = 0;
+		for (LinkedList<T> list : elements)
+		{
+			System.out.print(i++ + ": ");
+			for (T elm : list)
+			{
+				System.out.print(elm+" ");
+			}
+			System.out.println();
+		}
+	}
+}

+ 89 - 0
Lab12/src/HashTest.java

@@ -0,0 +1,89 @@
+import java.util.Scanner;
+
+
+public class HashTest
+{
+	public static void main(String... args)
+	{
+		Scanner in = new Scanner(System.in);
+		System.out.println("Give me a size for the hash table!");
+		HashTableSC<Integer> table = new HashTableSC<Integer>(in.nextInt());
+		in.nextLine();
+		System.out.println("Options:");
+		System.out.println("\"a\" -\tAdd to table");
+		System.out.println("\"f\" -\tFind in table");
+		System.out.println("\"d\" -\tDelete from table");
+		System.out.println("\"e\" -\tIs table empty");
+		System.out.println("\"p\" -\tPrint the table");
+		System.out.println("\"q\" -\tQuit");
+		boolean running = true;
+		while (running)
+		{
+			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 insert.");
+					if (in.hasNextInt())
+					{
+						int newVal = in.nextInt();
+						table.insert(newVal);
+						System.out.println(newVal+" added");
+					}
+					else
+					{
+						System.out.println("Not an integer.  No action taken.");
+					}
+					in.nextLine();
+					break;
+				case 'f':
+					System.out.println("Enter an integer to find.");
+					if (in.hasNextInt())
+					{
+						int val = in.nextInt();
+						System.out.println(val+(table.find(val)? " found":" not found"));
+					}
+					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 val = in.nextInt();
+						table.delete(val);
+						System.out.println(val+" deleted");
+					}
+					else
+					{
+						System.out.println("Not an integer.  No action taken.");
+					}
+					in.nextLine();
+					break;
+				case 'e':
+					System.out.println(table.isEmpty()? "Empty":"Not Empty");
+					break;
+				case 'p':
+					table.print();
+					break;
+				case 'q':
+					running = false;
+					System.out.println("Quitting");
+					break;
+				default:
+					System.out.println("Invalid Option.");
+					//printHelp();
+			}
+		}
+	}
+}