Tom Flucke 10 gadi atpakaļ
vecāks
revīzija
55c17e7d41

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

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

@@ -0,0 +1,12 @@
+#Mon Sep 28 11:05:08 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

BIN
Lab3/bin/LQueue$MyException.class


BIN
Lab3/bin/LQueue$Node.class


BIN
Lab3/bin/LQueue.class


BIN
Lab3/bin/LQueueDriver.class


+ 70 - 0
Lab3/src/LQueue.java

@@ -0,0 +1,70 @@
+
+public class LQueue<T>
+{
+	public static class MyException extends RuntimeException
+	{
+		private static final long serialVersionUID = -244806549310243756L;
+		public MyException()
+		{
+			super();
+		}
+		public MyException(String message)
+		{
+			super(message);
+		}
+	}
+	
+	private Node front, end;
+	
+	public LQueue()
+	{
+		front = end = null;
+	}
+	
+	public void enqueue(T item)
+	{
+		Node newEnd = new Node(item);
+		if (isEmpty())
+		{
+			front = newEnd;
+		}
+		else
+		{
+			end.next = newEnd;	
+		}
+		end = newEnd;
+	}
+	
+	public T dequeue()
+	{
+		if (isEmpty())
+		{
+			throw new MyException();
+		}
+		try
+		{
+			return front.item;
+		}
+		finally
+		{
+			front = front.next;
+		}
+	}
+	
+	public boolean isEmpty()
+	{
+		return front == null;
+	}
+	
+	private class Node
+	{
+		public T item;
+		public Node next;
+		
+		public Node(T item)
+		{
+			this.item = item;
+			this.next = null;
+		}
+	}
+}

+ 72 - 0
Lab3/src/LQueueDriver.java

@@ -0,0 +1,72 @@
+import java.util.Scanner;
+
+public class LQueueDriver {
+	public static void main(String[] args) {
+		Scanner in = new Scanner(System.in);
+		LQueue<Integer> queue = new LQueue<Integer>();
+		printHelp();
+		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.");
+					if (in.hasNextInt())
+					{
+						int newVal = in.nextInt();
+						queue.enqueue(newVal);
+						System.out.println(newVal+" enqueued");
+					}
+					else
+					{
+						System.out.println("Not an integer.  No action taken.");
+					}
+					in.nextLine();
+					break;
+				case 'd':
+					try
+					{
+						System.out.println(queue.dequeue() +" dequeued");
+					}
+					catch (LQueue.MyException me)
+					{
+						System.out.println("Invalid operation.  Queue is empty.");
+					}
+					break;
+				case 'e':
+					System.out.println(queue.isEmpty()? "Empty":"Not Empty");
+					break;
+				case 'q':
+					running = false;
+					System.out.println("Quitting");
+					break;
+				default:
+					System.out.println("Invalid Option.");
+					printHelp();
+			}
+		}
+		while (!queue.isEmpty())
+		{
+			System.out.print(queue.dequeue()+" ");
+		}
+	}
+	
+	public static void printHelp()
+	{
+		System.out.println("Options:");
+		System.out.println("\"a\" -\tAdd to Queue");
+		System.out.println("\"d\" -\tRemove from Queue");
+		System.out.println("\"e\" -\tCheck if Queue is empty");
+		System.out.println("\"q\" -\tQuit");
+	}
+
+}