This commit is contained in:
louiscklaw
2025-01-31 19:15:17 +08:00
parent 09adae8c8e
commit 6c60a73f30
1546 changed files with 286918 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
public class ArrayStack implements Stack {
public static final int CAPACITY =1000;
private int capacity;
private Object[] array;
private int top =-1;
public ArrayStack() {
this(CAPACITY);
}
public ArrayStack(int cap){
capacity =cap;
array = new Object[capacity];
}
@Override
public int size() {
return top+1;
}
@Override
public boolean isEmpty() {
return (top<0);
}
@Override
public void push(Object item) throws StackFullException {
if(size()==capacity)
throw new StackFullException();
array[++top]=item;
}
@Override
public Object pop() throws StackEmptyException {
if(isEmpty())
throw new StackEmptyException();
Object item =array[top];
array[top--]=null;
return item;
}
@Override
public Object top() throws StackEmptyException {
if(isEmpty())
throw new StackEmptyException ();
return array[top];
}
}

View File

@@ -0,0 +1,6 @@
class EmptyListException extends Exception {
public EmptyListException() {
}
}

View File

@@ -0,0 +1,101 @@
import java.util.Scanner;
public class EvaPostfix {
public static void main(String[] args) {
Stack s = new LinkedStack();
Scanner keyboard = new Scanner(System.in);
System.out.print("Input the infix expression? ");
String exp = keyboard.nextLine();
try {
if (parenthesisMatching(exp)) {
for (int i = 0; i < exp.length(); i++) {
char item = exp.charAt(i);
if (item == '(' || item == '{' || item == '[') {
s.push(item);
} else if (isOperater(item)) {
if (s.isEmpty()) {
s.push(item);
} else if ((checkVal(item) == checkVal((char) s.top())) || (checkVal(item) < checkVal((char) s.top()))) {
System.out.print(s.pop());
if(s.isEmpty()){
s.push(item);
}else if(checkVal((char) s.top()) == checkVal(item)) {
while (checkVal(item) == checkVal((char) s.top())) {
System.out.print(s.pop());
if (s.isEmpty()) {
s.push(item);
break;
}
}
} else {
s.push(item);
}
} else if (checkVal(item) > checkVal((char) s.top())) {
s.push(item);
}
} else if (item == ')' || item == ']' || item == '}') {
while (((char) s.top() != '(')&&((char) s.top() != '{') && ((char) s.top() != '[')) {
System.out.print(s.pop());
}
s.pop();
} else {
System.out.print(item);
}
}
while (!s.isEmpty()) {
System.out.print(s.pop());
}
} else {
System.out.println("Error");
}
} catch (StackEmptyException e) {
System.out.println("Parenthesis not matched");
} catch (StackFullException e) {
System.out.println("Parenthesis not matched");
}
}
public static boolean parenthesisMatching(String exp) throws StackFullException, StackEmptyException {
Stack s = new LinkedStack();
char item;
for (int i = 0; i < exp.length(); i++) {
item = exp.charAt(i);
if (item == '{' || item == '(' || item == '[') {
s.push(item);
} else if (item == '}' || item == ')' || item == ']') {
char x = (char) s.pop();
if ((x != '(') && (x != '[') && (x != '{')) {
return false;
}
}
}
if (s.isEmpty()) {
return true;
} else {
return false;
}
}
public static boolean isOperater(char exp) {
switch (exp) {
case '+':
case '-':
case '*':
case '/':
return true;
default:
return false;
}
}
public static int checkVal(char exp) {
if (exp == '*' || exp == '/') {
return 5;
} else if (exp == '+' || exp == '-') {
return 3;
} else {//'('
return -1;
}
}
}

View File

@@ -0,0 +1,132 @@
public class LinkedList {
private ListNode head;
private ListNode tail;
private int count = 0;
public boolean isEmpty() {
return (head == null);
}
public void addToHead(Object item) {
if (isEmpty()) {
head = tail = new ListNode(item);
} else {
head = new ListNode(item, head);
}
count++;
}
public void addToTail(Object item) {
if (isEmpty()) {
head = tail = new ListNode(item);
} else {
tail.next = new ListNode(item);
tail = tail.next;
}
count++;
}
public Object removeFromHead() throws EmptyListException {
Object item = null;
if (isEmpty()) {
throw new EmptyListException();
}
item = head.data;
if (head == tail) {
head = tail = null;
} else {
head = head.next;
}
count--;
return item;
}
public Object removeFromtail() throws EmptyListException {
if (isEmpty()) {
throw new EmptyListException();
}
Object item = tail.data;
if (head == tail) {
head = tail = null;
return item;
}
ListNode current = head;
while (current.next != tail) {
current = current.next;
}
tail = current;
tail.next = null;
count--;
return item;
}
public String toString() {
String s = "[ ";
ListNode current = head;
while (current != null) {
s = s + current.data + " ";
current = current.next;
}
return s + " ]";
}
public int getCount() {
return count;
}
public Object getItemAt(int n) {
if (n < 0 || n >= count) {
throw new IndexOutOfBoundsException();
}
int currentPos = 0;
ListNode current = head;
while (currentPos <= (n - 1)) {
current = current.next;
currentPos++;
}
return current.data;
}
public void addItemAt(Object item, int n) {
if (isEmpty() || n == 0) {
addToHead(item);
return;
}
if (n >= count) {
addToTail(item);
return;
}
int currentPos = 0;
ListNode current = head;
while (currentPos < (n - 1)) {
current = current.next;
currentPos++;
}
ListNode newNode = new ListNode(item);
newNode.next = current.next;
current.next = newNode;
count++;
}
public Object removeItemAt(int n) throws EmptyListException{
if(n<0 || n>=count)
throw new IndexOutOfBoundsException();
if(n==0)
removeFromHead();
int currentPos=0;
ListNode current=head;
while(currentPos<(n-1)){
current = current.next;
currentPos++;
}
Object item = current.next.data;
current.next = current.next.next;
count--;
return item;
}
}

View File

@@ -0,0 +1,39 @@
public class LinkedStack implements Stack {
private LinkedList sll = new LinkedList();
@Override
public int size() {
return sll.getCount();
}
@Override
public boolean isEmpty() {
return sll.isEmpty();
}
@Override
public void push(Object item) throws StackFullException {
sll.addToHead(item);
}
@Override
public Object pop() throws StackEmptyException {
try {
Object item = sll.removeFromHead();
return item;
} catch (EmptyListException e) {
throw new StackEmptyException();
}
}
@Override
public Object top() throws StackEmptyException {
try {
Object item = sll.removeFromHead();
sll.addToHead(item);
return item;
} catch (EmptyListException e) {
throw new StackEmptyException();
}
}
}

View File

@@ -0,0 +1,13 @@
public class ListNode {
public Object data;
public ListNode next;
public ListNode(Object ListNode){
this.data = ListNode;
this.next = null;
}
public ListNode(Object ListNode, ListNode next){
this.data = ListNode;
this.next = next;
}
}

View File

@@ -0,0 +1,44 @@
import java.util.Scanner;
public class ParenthesisMatching {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Expression? ");
String exp = keyboard.nextLine();
try {
if (parenthesisMatching(exp)) {
System.out.println("Parenthes is matched");
} else {
System.out.println("Parenthesis not matched");
}
} catch (StackEmptyException e) {
System.out.println("Parenthesis not matched");
} catch (StackFullException e) {
System.out.println("Parenthesis not matched");
}
keyboard.close();
}
public static boolean parenthesisMatching(String exp) throws StackFullException, StackEmptyException {
Stack s = new LinkedStack();
char item;
for (int i = 0; i < exp.length(); i++) {
item = exp.charAt(i);
if (item == '{' || item == '(' || item == '[') {
s.push(item);
} else if (item == '}' || item == ')' || item == ']') {
char x = (char) s.pop();
if ((x != '(') && (x != '[') && (x != '{')) {
return false;
}
}
}
if (s.isEmpty()) {
return true;
} else {
return false;
}
}
}

View File

@@ -0,0 +1,16 @@
public class Reverse {
public static void main(String [ ] args) throws StackFullException, StackEmptyException {
String [ ] arr = {"Abby", "Boris", "Cat", "Dino", "Elvis"};
Stack stack = new ArrayStack();
for (int i=0; i<arr.length; i++) {
stack.push(arr[i]);
}
for (int i=0; i<arr.length; i++) {
arr[i] = (String) stack.pop();
}
for (int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}

View File

@@ -0,0 +1,7 @@
public interface Stack {
public abstract int size();
public abstract boolean isEmpty();
public abstract void push(Object item) throws StackFullException;
public abstract Object pop() throws StackEmptyException;
public abstract Object top() throws StackEmptyException;
}

View File

@@ -0,0 +1,3 @@
public class StackEmptyException extends Exception {
}

View File

@@ -0,0 +1,3 @@
public class StackFullException extends Exception {
}