Tom Flucke 10 vuotta sitten
vanhempi
commit
b79a3bb9ae

+ 6 - 0
Lab13/.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
Lab13/.project

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

@@ -0,0 +1,12 @@
+#Wed Nov 04 11:08:23 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

+ 135 - 0
Lab13/src/MyHashTable.java

@@ -0,0 +1,135 @@
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.NoSuchElementException;
+
+
+public class MyHashTable<T>
+{
+	private LinkedList<T>[] elements;
+	
+	@SuppressWarnings("unchecked")
+	public MyHashTable(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();
+		}
+	}
+
+	public void makeEmpty()
+	{
+		for (int i = 0; i < elements.length; i++)
+		{
+			elements[i].clear();
+		}
+	}
+
+	public int size()
+	{
+		int sum = 0;
+		for (LinkedList<T> list : elements)
+		{
+			sum += list.size();
+		}
+		return sum;
+	}
+	
+	private class Iter implements Iterator<T>
+	{
+		private int i, j;
+		
+		public Iter()
+		{
+			j = 0;
+			i = 0;
+			while (i < elements.length && elements[i].isEmpty())
+			{
+				i++;
+			}
+		}
+		
+		@Override
+		public boolean hasNext()
+		{
+			return i < elements.length;
+		}
+
+		@Override
+		public T next()
+		{
+			if (!hasNext())
+			{
+				throw new NoSuchElementException();
+			}
+			T result = elements[i].get(j++);
+			if (j >= elements[i].size())
+			{
+				j = 0;
+				do
+				{
+					i++;
+				}
+				while (i < elements.length && elements[i].isEmpty());
+			}
+			return result;
+		}
+
+		@Override
+		public void remove()
+		{
+			throw new UnsupportedOperationException();
+		}
+	}
+	
+	public Iterator<T> iterator()
+	{
+		return new Iter();
+	}
+}

+ 108 - 0
Lab13/src/MyHashTest.java

@@ -0,0 +1,108 @@
+import java.util.Iterator;
+import java.util.Scanner;
+
+
+public class MyHashTest
+{
+	public static void main(String... args)
+	{
+		Scanner in = new Scanner(System.in);
+		System.out.println("Give me a size for the hash table!");
+		MyHashTable<Integer> table = new MyHashTable<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("\"k\" -\tMake table empty");
+		System.out.println("\"s\" -\tPrint table size");
+		System.out.println("\"o\" -\tOutput entire collection");
+		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 'k':
+					table.makeEmpty();
+					System.out.println("Table is now empty.");
+					break;
+				case 's':
+					System.out.println(table.size()+" elements in collection");
+					break;
+				case 'o':
+					Iterator<Integer> iter = table.iterator();
+					while (iter.hasNext())
+					{
+						System.out.print(iter.next()+" ");
+					}
+					System.out.println();
+					break;
+				case 'q':
+					running = false;
+					System.out.println("Quitting");
+					break;
+				default:
+					System.out.println("Invalid Option.");
+					//printHelp();
+			}
+		}
+	}
+}

+ 7 - 0
Lab17/.classpath

@@ -0,0 +1,7 @@
+<?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 combineaccessrules="false" kind="src" path="/Lab3"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
Lab17/.project

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

@@ -0,0 +1,12 @@
+#Mon Nov 30 11:14:41 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

+ 99 - 0
Lab17/src/DIGraphAM.java

@@ -0,0 +1,99 @@
+
+public class DIGraphAM
+{
+	private int[][] adjTable;
+	
+	public DIGraphAM(int N)
+	{
+		adjTable = new int[N][N];
+	}
+	
+	public void addEdge(int from, int to)
+	{
+		adjTable[from][to] = 1;
+	}
+	
+	public void deleteEdge(int from, int to)
+	{
+		adjTable[from][to] = 0;
+	}
+	
+	public int edgeCount()
+	{
+		int count = 0;
+		for (int[] links : adjTable)
+		{
+			for (int i : links)
+			{
+				count += i;
+			}
+		}
+		return count;
+	}
+	
+	public int vertexCount()
+	{
+		return adjTable.length;
+	}
+	
+	public void print()
+	{
+		for (int from = 0; from < adjTable.length; from++)
+		{
+			System.out.print(from+" is connected to:");
+			for (int to = 0; to < adjTable.length; to++)
+			{
+				if (adjTable[from][to] == 1)
+				{
+					System.out.print(" "+to);
+				}
+			}
+			System.out.println();
+		}
+	}
+	
+	private int[] indegrees()
+	{
+		int[] result = new int[adjTable.length];
+		for (int from = 0; from < adjTable.length; from++)
+		{
+			for (int to = 0; to < adjTable.length; to++)
+			{
+				result[to] += adjTable[from][to];
+			}
+		}
+		return result;
+	}
+	
+	public int[] topSort() throws Exception
+	{
+		int[] result = new int[adjTable.length];
+		int i = 0;
+		LQueue<Integer> noNeighbors = new LQueue<Integer>();
+		int[] neighborCount = indegrees();
+		for (int j = 0; j < neighborCount.length; j++)
+		{
+			if (neighborCount[j] == 0)
+			{
+				noNeighbors.enqueue(j);
+			}
+		}
+		while (!noNeighbors.isEmpty())
+		{
+			int v = noNeighbors.dequeue();
+			result[i++] = v;
+			for (int from = 0; from < adjTable.length; from++)
+			{
+				if (adjTable[v][from] == 1 && --neighborCount[from] == 0)
+				{
+					noNeighbors.enqueue(from);
+				}
+			}
+		}
+		if (i != result.length)
+		{
+			throw new Exception();
+		}
+		return result;
+	}
+}

+ 108 - 0
Lab17/src/DIGraphTest.java

@@ -0,0 +1,108 @@
+import java.util.Arrays;
+import java.util.Scanner;
+
+
+public class DIGraphTest
+{
+	public static void main(String... args)
+	{
+		Scanner in = new Scanner(System.in);
+		System.out.print("Vertex Count:");
+		DIGraphAM graph = new DIGraphAM(in.nextInt());
+		in.nextLine();
+		System.out.println("Options:");
+		System.out.println("\"a\" -\tAdd Edge");
+		System.out.println("\"d\" -\tRemove Edge");
+		System.out.println("\"e\" -\tCount Edges");
+		System.out.println("\"V\" -\tCount Verticies");
+		System.out.println("\"p\" -\tPrint");
+		System.out.println("\"t\" -\tTopological Sort");
+		System.out.println("\"q\" -\tQuit");
+		boolean running = true;
+		while (running)
+		{
+			System.out.print("Command: ");
+			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 two integers:");
+					if (in.hasNextInt())
+					{
+						int from = in.nextInt();
+						if (in.hasNextInt())
+						{
+							int to = in.nextInt();
+							graph.addEdge(from, to);
+							System.out.println(from+" linked to "+to);
+						}
+						else
+						{
+							System.out.println("Second value not an integer.  No action taken.");
+						}
+					}
+					else
+					{
+						System.out.println("First value not an integer.  No action taken.");
+					}
+					in.nextLine();
+					break;
+				case 'd':
+					System.out.println("Enter two integers:");
+					if (in.hasNextInt())
+					{
+						int from = in.nextInt();
+						if (in.hasNextInt())
+						{
+							int to = in.nextInt();
+							graph.deleteEdge(from, to);
+							System.out.println(from+" unlinked to "+to);
+						}
+						else
+						{
+							System.out.println("Second value not an integer.  No action taken.");
+						}
+					}
+					else
+					{
+						System.out.println("First value not an integer.  No action taken.");
+					}
+					in.nextLine();
+					break;
+				case 'e':
+					System.out.println("Edge Count: "+graph.edgeCount());
+					break;
+				case 'v':
+					System.out.println("Vertex Count: "+graph.vertexCount());
+					break;
+				case 'p':
+					graph.print();
+					break;
+				case 't':
+					try
+					{
+						System.out.println(Arrays.toString(graph.topSort()));
+					}
+					catch (Exception e)
+					{
+						System.out.println("Cyclic Graph");
+					}
+					break;
+				case 'q':
+					running = false;
+					System.out.println("Quitting");
+					break;
+				default:
+					System.out.println("Invalid Option.");
+			}
+		}
+		System.out.println();
+	}
+}