assignment
This commit is contained in:
parent
f0b5074ac7
commit
9e95d7d514
110
assignments/AssigmentS5/LinkedStack.java
Normal file
110
assignments/AssigmentS5/LinkedStack.java
Normal file
@ -0,0 +1,110 @@
|
||||
//Note: We are using the class LinkedStack instead of
|
||||
// the class OurStack that the book uses.
|
||||
/**
|
||||
A class that checks whether the parentheses, brackets, and braces
|
||||
in a string occur in left/right pairs.
|
||||
@author Frank M. Carrano, Timothy M. Henry
|
||||
@version 5.0 */
|
||||
public class LinkedStack<T> implements StackInterface<T>
|
||||
{
|
||||
private Node firstNode; // Reference to first node
|
||||
private int numberOfEntries;
|
||||
|
||||
public LinkedStack()
|
||||
{
|
||||
firstNode = null;
|
||||
numberOfEntries = 0;
|
||||
} // end default constructor
|
||||
|
||||
/** Adds a new entry to the top of this stack.
|
||||
@param newEntry An object to be added to the stack. */
|
||||
public void push(T newEntry)
|
||||
{
|
||||
// Add to beginning of chain:
|
||||
Node newNode = new Node(newEntry);
|
||||
newNode.setNextNode(firstNode); // Make new node reference rest of chain
|
||||
// (firstNode is null if chain is empty)
|
||||
firstNode = newNode; // New node is at beginning of chain
|
||||
numberOfEntries++;
|
||||
}
|
||||
|
||||
/** Removes and returns this stack's top entry.
|
||||
@return The object at the top of the stack.
|
||||
@throws EmptyStackException if the stack is empty before the operation. */
|
||||
public T pop()
|
||||
{
|
||||
T result = null;
|
||||
if (firstNode != null)
|
||||
{
|
||||
Node removingNode = firstNode;
|
||||
result = removingNode.getData();
|
||||
firstNode = firstNode.getNextNode();
|
||||
numberOfEntries--;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Retrieves this stack's top entry.
|
||||
@return The object at the top of the stack.
|
||||
@throws EmptyStackException if the stack is empty. */
|
||||
public T peek()
|
||||
{
|
||||
T result = null;
|
||||
if (firstNode != null)
|
||||
result = firstNode.getData();
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Detects whether this stack is empty.
|
||||
@return True if the stack is empty. */
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return numberOfEntries == 0;
|
||||
}
|
||||
|
||||
/** Removes all entries from this stack. */
|
||||
public void clear()
|
||||
{
|
||||
firstNode = null;
|
||||
}
|
||||
|
||||
// @author Frank M. Carrano, Timothy M. Henry
|
||||
// @version 5.0 */
|
||||
private class Node
|
||||
{
|
||||
private T data; // Entry in bag
|
||||
private Node next; // Link to next node
|
||||
|
||||
private Node(T dataPortion)
|
||||
{
|
||||
this(dataPortion, null);
|
||||
} // end constructor
|
||||
|
||||
private Node(T dataPortion, Node nextNode)
|
||||
{
|
||||
data = dataPortion;
|
||||
next = nextNode;
|
||||
} // end constructor
|
||||
|
||||
private T getData()
|
||||
{
|
||||
return data;
|
||||
} // end getData
|
||||
|
||||
private void setData(T newData)
|
||||
{
|
||||
data = newData;
|
||||
} // end setData
|
||||
|
||||
private Node getNextNode()
|
||||
{
|
||||
return next;
|
||||
} // end getNextNode
|
||||
|
||||
private void setNextNode(Node nextNode)
|
||||
{
|
||||
next = nextNode;
|
||||
} // end setNextNode
|
||||
} // end Node
|
||||
|
||||
} // end LinkedStack
|
170
assignments/AssigmentS5/Postfix.java
Normal file
170
assignments/AssigmentS5/Postfix.java
Normal file
@ -0,0 +1,170 @@
|
||||
public class Postfix {
|
||||
public static String convertToPostfix(String infix) {
|
||||
StackInterface<Character> operatorStack = new LinkedStack<>();
|
||||
char[] postfixArray = new char[infix.length()]; // Max possible size
|
||||
int postfixIndex = 0;
|
||||
int index = 0;
|
||||
char nextCharacter = ' ';
|
||||
char topOperator = ' ';
|
||||
|
||||
while (index < infix.length()) {
|
||||
nextCharacter = infix.charAt(index);
|
||||
switch (nextCharacter) {
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
|
||||
postfixArray[postfixIndex++] = nextCharacter;
|
||||
break;
|
||||
case '^':
|
||||
operatorStack.push(nextCharacter);
|
||||
break;
|
||||
case '+': case '-': case '*': case '/':
|
||||
while (!operatorStack.isEmpty() &&
|
||||
!isBigger(nextCharacter, operatorStack.peek())) {
|
||||
postfixArray[postfixIndex++] = operatorStack.pop();
|
||||
}
|
||||
operatorStack.push(nextCharacter);
|
||||
break;
|
||||
case '(':
|
||||
operatorStack.push(nextCharacter);
|
||||
break;
|
||||
case ')':
|
||||
topOperator = operatorStack.pop();
|
||||
while (topOperator != '(') {
|
||||
postfixArray[postfixIndex++] = topOperator;
|
||||
topOperator = operatorStack.pop();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
while (!operatorStack.isEmpty()) {
|
||||
postfixArray[postfixIndex++] = operatorStack.pop();
|
||||
}
|
||||
|
||||
// Create string only from the used portion of the array
|
||||
return new String(postfixArray, 0, postfixIndex);
|
||||
}
|
||||
|
||||
// Rest of the class remains the same (isBigger, precedenceLevel, evaluatePostfix, etc.)
|
||||
private static boolean isBigger(char ch1, char ch2) {
|
||||
return precedenceLevel(ch1) > precedenceLevel(ch2);
|
||||
}
|
||||
|
||||
private static int precedenceLevel(char op) {
|
||||
switch (op) {
|
||||
case '(': return 0;
|
||||
case '+': case '-': return 1;
|
||||
case '*': case '/': return 2;
|
||||
case '^': return 3;
|
||||
default: throw new IllegalArgumentException("Operator unknown: " + op);
|
||||
}
|
||||
}
|
||||
|
||||
public static int evaluatePostfix(String postfix) {
|
||||
StackInterface<Integer> valueStack = new LinkedStack<>();
|
||||
int index = 0;
|
||||
char nextCharacter = ' ';
|
||||
Integer operandTwo, operandOne;
|
||||
int result;
|
||||
while (index < postfix.length()) {
|
||||
nextCharacter = postfix.charAt(index);
|
||||
switch (nextCharacter) {
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
|
||||
valueStack.push(getValue(nextCharacter));
|
||||
break;
|
||||
case '+': case '-': case '*': case '/': case '^':
|
||||
operandTwo = valueStack.pop();
|
||||
operandOne = valueStack.pop();
|
||||
result = calculate(operandOne,operandTwo,nextCharacter);
|
||||
valueStack.push(result);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return valueStack.peek();
|
||||
}
|
||||
|
||||
private static Integer getValue(char op) {
|
||||
Integer[] values = {2, 3, 4, 5, 6}; // a, b, c, d, e
|
||||
return values[op-97];
|
||||
}
|
||||
|
||||
private static Integer calculate(Integer opd1, Integer opd2, char opr) {
|
||||
Integer intObj = 0;
|
||||
switch (opr) {
|
||||
case '+': intObj = opd1 + opd2; break;
|
||||
case '-': intObj = opd1 - opd2; break;
|
||||
case '*': intObj = opd1 * opd2; break;
|
||||
case '/':
|
||||
if (opd2.equals(0))
|
||||
throw new ArithmeticException("Division by zero");
|
||||
else
|
||||
intObj = opd1 / opd2;
|
||||
break;
|
||||
case '^': intObj = (int) Math.pow(opd1, opd2); break;
|
||||
default: break;
|
||||
}
|
||||
return intObj;
|
||||
}
|
||||
|
||||
public static int evaluateInfix(String infix) {
|
||||
StackInterface<Character> operatorStack = new LinkedStack<>();
|
||||
StackInterface<Integer> valueStack = new LinkedStack<>();
|
||||
int index = 0;
|
||||
char nextCharacter = ' ';
|
||||
char topOperator = ' ';
|
||||
Integer operandTwo, operandOne;
|
||||
int result;
|
||||
while (index < infix.length()) {
|
||||
nextCharacter = infix.charAt(index);
|
||||
switch (nextCharacter) {
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
|
||||
valueStack.push(getValue(nextCharacter));
|
||||
break;
|
||||
case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
|
||||
valueStack.push(Character.getNumericValue(nextCharacter));
|
||||
break;
|
||||
case '^':
|
||||
operatorStack.push(nextCharacter);
|
||||
break;
|
||||
case '+': case '-': case '*': case '/':
|
||||
while (!operatorStack.isEmpty() &&
|
||||
!isBigger(nextCharacter, operatorStack.peek())) {
|
||||
topOperator = operatorStack.pop();
|
||||
operandTwo = valueStack.pop();
|
||||
operandOne = valueStack.pop();
|
||||
result = calculate(operandOne,operandTwo,topOperator);
|
||||
valueStack.push(result);
|
||||
}
|
||||
operatorStack.push(nextCharacter);
|
||||
break;
|
||||
case '(':
|
||||
operatorStack.push(nextCharacter);
|
||||
break;
|
||||
case ')':
|
||||
topOperator = operatorStack.pop();
|
||||
while (topOperator != '(') {
|
||||
operandTwo = valueStack.pop();
|
||||
operandOne = valueStack.pop();
|
||||
result = calculate(operandOne,operandTwo,topOperator);
|
||||
valueStack.push(result);
|
||||
topOperator = operatorStack.pop();
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
while (!operatorStack.isEmpty()) {
|
||||
topOperator = operatorStack.pop();
|
||||
operandTwo = valueStack.pop();
|
||||
operandOne = valueStack.pop();
|
||||
result = calculate(operandOne,operandTwo,topOperator);
|
||||
valueStack.push(result);
|
||||
}
|
||||
return valueStack.peek();
|
||||
}
|
||||
}
|
45
assignments/AssigmentS5/PostfixTest.java
Normal file
45
assignments/AssigmentS5/PostfixTest.java
Normal file
@ -0,0 +1,45 @@
|
||||
// @author Frank M. Carrano
|
||||
// @author Timothy M. Henry
|
||||
// @version 5.0
|
||||
public class PostfixTest
|
||||
{
|
||||
public static void main(String arg[])
|
||||
{
|
||||
|
||||
// Test convertToPostfix
|
||||
System.out.println("Testing convertToPostfix");
|
||||
String[] expressions = {"(a+b)/(c-d)","a/(b-c)*d","a-(b/(c-d)*e+f)^g",
|
||||
"(a-b*c)/(d*e^f*g+h)"};
|
||||
String[] qNums = {"a", "b", "c", "d"};
|
||||
for (int i = 0; i < expressions.length; i++)
|
||||
{
|
||||
String postfix = Postfix.convertToPostfix(expressions[i]);
|
||||
System.out.println(qNums[i]+". " + expressions[i] + " is converted to " + postfix);
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
// Test evaluatePostfix
|
||||
System.out.println("Testing evaluatePostfix");
|
||||
String[] postfixes = {"a e + b d - /", "a b c * d * -","a b c - / d *",
|
||||
"e b c a ^ * + d -"};
|
||||
for (int i = 0; i < postfixes.length; i++)
|
||||
{
|
||||
int evaluation = Postfix.evaluatePostfix(postfixes[i]);
|
||||
System.out.println(qNums[i]+". " + postfixes[i] + " is evaluated as " + evaluation);
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
// Test evaluateInfix
|
||||
System.out.println("Testing evaluateInfix");
|
||||
String[] infixes = {"a+b*c-9", "(a+ e)/ (b- d)","a+(b+c*d)-e/2",
|
||||
"e-b*c^a+d"};
|
||||
for (int i = 0; i < postfixes.length; i++)
|
||||
{
|
||||
int evaluation = Postfix.evaluateInfix(infixes[i]);
|
||||
System.out.println(qNums[i]+". " + infixes[i] + " is evaluated as " + evaluation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
30
assignments/AssigmentS5/StackInterface.java
Normal file
30
assignments/AssigmentS5/StackInterface.java
Normal file
@ -0,0 +1,30 @@
|
||||
/**
|
||||
An interface for the ADT stack.
|
||||
@author Frank M. Carrano
|
||||
@author Timothy M. Henry
|
||||
@version 5.0
|
||||
*/
|
||||
public interface StackInterface<T>
|
||||
{
|
||||
/** Adds a new entry to the top of this stack.
|
||||
@param newEntry An object to be added to the stack. */
|
||||
public void push(T newEntry);
|
||||
|
||||
/** Removes and returns this stack's top entry.
|
||||
@return The object at the top of the stack.
|
||||
@throws EmptyStackException if the stack is empty before the operation. */
|
||||
public T pop();
|
||||
|
||||
/** Retrieves this stack's top entry.
|
||||
@return The object at the top of the stack.
|
||||
@throws EmptyStackException if the stack is empty. */
|
||||
public T peek();
|
||||
|
||||
/** Detects whether this stack is empty.
|
||||
@return True if the stack is empty. */
|
||||
public boolean isEmpty();
|
||||
|
||||
/** Removes all entries from this stack. */
|
||||
public void clear();
|
||||
|
||||
} // end StackInterface
|
30
assignments/AssigmentS6/DequeInterface.java
Normal file
30
assignments/AssigmentS6/DequeInterface.java
Normal file
@ -0,0 +1,30 @@
|
||||
/** An interface for the ADT deque.
|
||||
@author Frank M. Carrano
|
||||
*/
|
||||
public interface DequeInterface<T>
|
||||
{
|
||||
/** Adds a new entry to the front/back of this deque.
|
||||
@param newEntry An object to be added. */
|
||||
public void addToFront(T newEntry);
|
||||
public void addToBack(T newEntry);
|
||||
|
||||
/** Removes and returns the front/back entry of this deque.
|
||||
@return The object at the front/back of the deque.
|
||||
@throws EmptyQueueException if the deque is empty before the
|
||||
operation. */
|
||||
public T removeFront();
|
||||
public T removeBack();
|
||||
|
||||
/** Retrieves the front/back entry of this deque.
|
||||
@return The object at the front/back of the deque.
|
||||
@throws EmptyQueueException if the deque is empty. */
|
||||
public T getFront();
|
||||
public T getBack();
|
||||
|
||||
/** Detects whether this deque is empty.
|
||||
@return True if the deque is empty, or false otherwise. */
|
||||
public boolean isEmpty();
|
||||
|
||||
/* Removes all entries from this deque. */
|
||||
public void clear();
|
||||
} // end DequeInterface
|
17
assignments/AssigmentS6/EmptyQueueException.java
Normal file
17
assignments/AssigmentS6/EmptyQueueException.java
Normal file
@ -0,0 +1,17 @@
|
||||
/**
|
||||
A class of runtime exceptions thrown when an attempt is made to access
|
||||
or remove the front of a queue
|
||||
@author Hyunryung Kim
|
||||
*/
|
||||
public class EmptyQueueException extends RuntimeException
|
||||
{
|
||||
public EmptyQueueException()
|
||||
{
|
||||
this(null);
|
||||
} // end default constructor
|
||||
|
||||
public EmptyQueueException(String message)
|
||||
{
|
||||
super(message);
|
||||
} // end constructor
|
||||
} // end EmptyQueueException
|
156
assignments/AssigmentS6/LinkedDeque.java
Normal file
156
assignments/AssigmentS6/LinkedDeque.java
Normal file
@ -0,0 +1,156 @@
|
||||
/**
|
||||
A class that implements a deque of objects by using
|
||||
a chain of doubly linked nodes.
|
||||
@author Frank M. Carrano
|
||||
*/
|
||||
public final class LinkedDeque<T> implements DequeInterface<T>
|
||||
{
|
||||
private DLNode firstNode; // References node at front of queue
|
||||
private DLNode lastNode; // References node at back of queue
|
||||
|
||||
public LinkedDeque()
|
||||
{
|
||||
firstNode = null;
|
||||
lastNode = null;
|
||||
} // end default constructor
|
||||
|
||||
/** Adds a new entry to the front/back of this deque.
|
||||
@param newEntry An object to be added. */
|
||||
public void addToFront(T newEntry)
|
||||
{
|
||||
DLNode newNode = new DLNode(null, newEntry, firstNode);
|
||||
|
||||
if (isEmpty())
|
||||
lastNode = newNode;
|
||||
else
|
||||
firstNode.setPreviousNode(newNode);
|
||||
firstNode = newNode;
|
||||
} // end addToFront
|
||||
|
||||
public void addToBack(T newEntry)
|
||||
{
|
||||
DLNode newNode = new DLNode(lastNode, newEntry, null);
|
||||
|
||||
if (isEmpty())
|
||||
firstNode = newNode;
|
||||
else
|
||||
lastNode.setNextNode(newNode);
|
||||
// end if
|
||||
lastNode = newNode;
|
||||
} // end addToBack
|
||||
|
||||
/** Removes and returns the front/back entry of this deque.
|
||||
@return The object at the front/back of the deque.
|
||||
@throws EmptyQueueException if the deque is empty before the
|
||||
operation. */
|
||||
public T removeFront()
|
||||
{
|
||||
T front = getFront(); // Might throw an EmptyQueueException
|
||||
assert firstNode != null;
|
||||
|
||||
firstNode = firstNode.getNextNode();
|
||||
|
||||
if (firstNode == null)
|
||||
lastNode = null;
|
||||
else
|
||||
firstNode.setPreviousNode(null);
|
||||
return front;
|
||||
} // end removeFront
|
||||
|
||||
public T removeBack()
|
||||
{
|
||||
T back = getBack(); // Might throw an EmptyQueueException
|
||||
assert lastNode != null;
|
||||
lastNode = lastNode.getPreviousNode();
|
||||
|
||||
if (lastNode == null)
|
||||
firstNode = null;
|
||||
else
|
||||
lastNode.setNextNode(null);
|
||||
return back;
|
||||
} // end removeBack
|
||||
|
||||
/** Retrieves the front/back entry of this deque.
|
||||
@return The object at the front/back of the deque.
|
||||
@throws EmptyQueueException if the deque is empty. */
|
||||
public T getFront()
|
||||
{
|
||||
if (isEmpty())
|
||||
throw new EmptyQueueException();
|
||||
else
|
||||
return firstNode.getData();
|
||||
} // end getFront
|
||||
|
||||
public T getBack()
|
||||
{
|
||||
if (isEmpty())
|
||||
throw new EmptyQueueException();
|
||||
else
|
||||
return lastNode.getData();
|
||||
} // end getBack
|
||||
|
||||
/** Detects whether this deque is empty.
|
||||
@return True if the deque is empty, or false otherwise. */
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return (firstNode == null && lastNode == null);
|
||||
} // end isEmpty
|
||||
|
||||
/* Removes all entries from this deque. */
|
||||
public void clear()
|
||||
{
|
||||
firstNode = null;
|
||||
lastNode = null;
|
||||
} // end clear
|
||||
|
||||
// @author Frank M. Carrano, Timothy M. Henry
|
||||
// @version 5.0 */
|
||||
private class DLNode
|
||||
{
|
||||
private T data; // Entry in bag
|
||||
private DLNode previous; // Link to previous node
|
||||
private DLNode next; // Link to next node
|
||||
|
||||
private DLNode(T dataPortion)
|
||||
{
|
||||
this(null, dataPortion, null);
|
||||
} // end constructor
|
||||
|
||||
private DLNode(DLNode previousDLNode, T dataPortion, DLNode nextDLNode)
|
||||
{
|
||||
data = dataPortion;
|
||||
previous = previousDLNode;
|
||||
next = nextDLNode;
|
||||
} // end constructor
|
||||
|
||||
private T getData()
|
||||
{
|
||||
return data;
|
||||
} // end getData
|
||||
|
||||
private void setData(T newData)
|
||||
{
|
||||
data = newData;
|
||||
} // end setData
|
||||
|
||||
private DLNode getNextNode()
|
||||
{
|
||||
return next;
|
||||
} // end getNextDLNode
|
||||
|
||||
private void setNextNode(DLNode nextDLNode)
|
||||
{
|
||||
next = nextDLNode;
|
||||
} // end setNextDLNode
|
||||
|
||||
private DLNode getPreviousNode()
|
||||
{
|
||||
return previous;
|
||||
} // end getPreviousDLNode
|
||||
|
||||
private void setPreviousNode(DLNode previousDLNode)
|
||||
{
|
||||
previous = previousDLNode;
|
||||
} // end setPreviousDLNode
|
||||
} // end DLNode
|
||||
} // end LinkedQueue
|
54
assignments/AssigmentS6/LinkedDequeDemo.java
Normal file
54
assignments/AssigmentS6/LinkedDequeDemo.java
Normal file
@ -0,0 +1,54 @@
|
||||
public class LinkedDequeDemo {
|
||||
public static void main(String[] args) {
|
||||
// Create a new LinkedDeque
|
||||
LinkedDeque<String> deque = new LinkedDeque<>();
|
||||
|
||||
System.out.println("=== Testing LinkedDeque Operations ===");
|
||||
System.out.println("Initial deque is empty: " + deque.isEmpty());
|
||||
|
||||
// Add elements to front and back
|
||||
System.out.println("\nAdding elements to front and back:");
|
||||
deque.addToFront("Front 1");
|
||||
deque.addToBack("Back 1");
|
||||
deque.addToFront("Front 2");
|
||||
deque.addToBack("Back 2");
|
||||
|
||||
// Display front and back
|
||||
System.out.println("Front element: " + deque.getFront());
|
||||
System.out.println("Back element: " + deque.getBack());
|
||||
|
||||
// Remove elements
|
||||
System.out.println("\nRemoving elements:");
|
||||
System.out.println("Removed from front: " + deque.removeFront());
|
||||
System.out.println("Removed from back: " + deque.removeBack());
|
||||
|
||||
// Display again
|
||||
System.out.println("\nAfter removal:");
|
||||
System.out.println("Front element: " + deque.getFront());
|
||||
System.out.println("Back element: " + deque.getBack());
|
||||
|
||||
// Test isEmpty
|
||||
System.out.println("\nDeque is empty: " + deque.isEmpty());
|
||||
|
||||
// Clear the deque
|
||||
System.out.println("\nClearing the deque...");
|
||||
deque.clear();
|
||||
System.out.println("Deque is empty after clear: " + deque.isEmpty());
|
||||
|
||||
// Test exception handling
|
||||
System.out.println("\nTesting exception handling:");
|
||||
try {
|
||||
System.out.println("Attempting to get front of empty deque...");
|
||||
System.out.println(deque.getFront());
|
||||
} catch (EmptyQueueException e) {
|
||||
System.out.println("Caught EmptyQueueException: " + e.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
System.out.println("Attempting to remove from back of empty deque...");
|
||||
System.out.println(deque.removeBack());
|
||||
} catch (EmptyQueueException e) {
|
||||
System.out.println("Caught EmptyQueueException: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user