diff --git a/homeWork push/QueueArray.java b/homeWork push/QueueArray.java new file mode 100644 index 0000000000000000000000000000000000000000..a14f73d27b5c6113c54e5d76543d42a65f108ba7 --- /dev/null +++ b/homeWork push/QueueArray.java @@ -0,0 +1,58 @@ +import com.sun.org.apache.regexp.internal.RE; +/*数组队列*/ +public class QueueArray { + int size; + int[] queue; + int head; + int tail; + boolean queueFull; + boolean queueNull; + + public QueueArray(int size) { + head = 0; + tail = head; + queue = new int[size]; + this.size = size; + queueFull = false; + queueNull = true; + } + + public void queueInput(int item) { + if (queueFull) { + System.out.println("Queue is Full"); + } else { + queue[tail] = item; + tail++; + if (tail ==size) { + queueFull = true; + tail--; + } + queueNull = false; + } + } + + public void queueOutput() { + if (queueNull) { + System.out.println("Queue is Null"); + } else { + if (head== tail){ + queueNull=true; + queue=new int[size]; + return; + } + int[] temps = new int[size]; + head++; + for(int i=head;i<=tail;i++){ + temps[head]=queue[head]; + } + queue=temps; + } + } + + public void print() { + for (int i = 0; i < queue.length; i++) { + System.out.println(queue[i]); + } + } + +} diff --git a/homeWork push/QueueCircular.java b/homeWork push/QueueCircular.java new file mode 100644 index 0000000000000000000000000000000000000000..71e7671141be42038ed9b371545293440fec01a4 --- /dev/null +++ b/homeWork push/QueueCircular.java @@ -0,0 +1,66 @@ +import com.sun.glass.ui.Size; +/*循环队列*/ +public class QueueCircular { + int maxsize; + int head; + int tail; + int[] queue; + boolean queuenUll; + boolean queueFUll; + + + public QueueCircular(int size) { + head = tail = 0; + maxsize = size; + queue = new int[size]; + queueFUll = false; + queuenUll = true; + } + + public void queueInput(int item) { + queuenUll = false; + if (queueFUll) { + System.out.println("Queue is Full"); + } else { + + queue[tail] = item; + tail = (tail + 1) % maxsize; + if (tail == head) { + queueFUll = true; + } else { + queueFUll = false; + } + + } + } + + public void queueOutput() { + if (queuenUll) { + System.out.println("Queue is Null"); + } else { + int[] temps = new int[maxsize]; + for (int i = 0; i < maxsize; i++) { + if (i == head) { + continue; + } + temps[i] = queue[i]; + } + queue = temps; + head = (head + 1) % maxsize; + if (head == tail) { + queuenUll = true; + queue = new int[maxsize]; + queueFUll = false; + } else { + queueFUll = false; + } + } + } + + public void printString() { + for (int i = 0; i < queue.length; i++) { + System.out.println(queue[i]); + } + } +} + diff --git a/homeWork push/QueueList.java b/homeWork push/QueueList.java new file mode 100644 index 0000000000000000000000000000000000000000..38bd2bd04db38302ce4f6d880b640120ac5ab2df --- /dev/null +++ b/homeWork push/QueueList.java @@ -0,0 +1,56 @@ +/*链表队列*/ +public class QueueList { + int[] queuelist; + QueueNode head; + QueueNode rear; + private int count; + + public int getCount() { + return count; + } + + public QueueList() { + head = null; + rear = head; + count = 0; + } + + public void queueInput(int item) { + if (head == null) { + head = new QueueNode(); + head.setValue(item); + rear = head; + count++; + return; + } + QueueNode temp = rear; + QueueNode node = new QueueNode(); + node.setValue(item); + temp.setNext(node); + count++; + rear = node; + } + + public void queueOutput() { + if (count > 0) { + if (count == 1) { + count--; + head = null; + rear = head; + System.out.println("All QueueData over out"); + } else { + head = head.getNext(); + count--; + } + } else { + System.out.println("Queue is null"); + } + } + + public void printString() { + for (int i = 0; i < count; i++) { + System.out.println(head.getValue()); + head = head.getNext(); + } + } +} \ No newline at end of file diff --git a/homeWork push/QueueNode.java b/homeWork push/QueueNode.java new file mode 100644 index 0000000000000000000000000000000000000000..5eefb88ccbf97cc4ba9c0df6f9169aed402aa008 --- /dev/null +++ b/homeWork push/QueueNode.java @@ -0,0 +1,20 @@ +public class QueueNode { + private int value; + private QueueNode next; + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public QueueNode getNext() { + return next; + } + + public void setNext(QueueNode next) { + this.next = next; + } +} diff --git a/homeWork push/StackArray.java b/homeWork push/StackArray.java new file mode 100644 index 0000000000000000000000000000000000000000..b3525b8173b04b2446122ce61585201b66004d55 --- /dev/null +++ b/homeWork push/StackArray.java @@ -0,0 +1,39 @@ +/*数组栈*/ +public class StackArray { + int top; + int[] stackArray; + private int size; + + public StackArray(int size) { + this.size = size; + stackArray = new int[size]; + top = -1; + } + + public void push(int item) { + if (top >= size-1) { + System.out.println("stack is full"); + } else { + top++; + stackArray[top] = item; + } + } + + public void pop() { + if(top<=0){ + stackArray=null; + System.out.println("Stack is null"); + }else { + System.out.println(stackArray[--top]); + } + } + + public void peek() { + if(top<=0){ + stackArray=null; + System.out.println("Stack is null"); + }else { + System.out.println(stackArray[top]); + } + } +} \ No newline at end of file diff --git a/homeWork push/StackCalc.java b/homeWork push/StackCalc.java new file mode 100644 index 0000000000000000000000000000000000000000..951d2608b1d68cc9b806f636a9494e3353bb80c6 --- /dev/null +++ b/homeWork push/StackCalc.java @@ -0,0 +1,63 @@ +锘/*鍚庣紑琛ㄨ揪寮忔爤*/ +public class StackCalc { + int[] list; + int index; + + public StackCalc() { + list = new int[10]; + index = 0; + } + + public void countResult(String s) { + if (isNumber(s)) { + list[index] = Integer.valueOf(s); + index++; + } else { + int result = 0; + if (index >= 2) { + switch (s) { + case "+": + result = list[index - 1] + list[index - 2]; + break; + case "-": + result = list[index - 1] - list[index - 2]; + break; + case "*": + result = list[index - 1] * list[index - 2]; + break; + case "/": + result = list[index - 1] / list[index - 2]; + break; + default: + break; + } + list[index - 2] = result; + int[] temps = new int[10]; + for (int i = 0; i < index - 1; i++) { + temps[i] = list[i]; + } + list = temps; + index--; + } else { + System.out.println("琛ㄨ揪寮忔牸寮忔湁璇紝鎿嶄綔绗﹀墠娌℃湁2涓暟瀛"); + } + + } + } + + public boolean isNumber(String str) { + final String number = "0123456789"; + for (int i = 0; i < str.length(); i++) { + if (number.indexOf(str.charAt(i)) == -1) { + return false; + } + } + return true; + } + + public void printString() { + for (int i = 0; i < index; i++) { + System.out.println(list[i]); + } + } +} diff --git a/homeWork push/StackList.java b/homeWork push/StackList.java new file mode 100644 index 0000000000000000000000000000000000000000..ce61eaeadc2e0c4011aee918693fa30ed622a6e5 --- /dev/null +++ b/homeWork push/StackList.java @@ -0,0 +1,47 @@ +/*链表栈*/ +public class StackList { + StackNode head; + StackNode rear; + public StackList() { + this.head = null; + this.rear=this.head; + } + public void push(StackNode sno){ + if(head==null){ + head=sno; + rear=head; + }else { + StackNode temp=head; + while (temp.next!=null){ + temp=temp.next; + } + temp.next=sno; + rear=sno; + } + } + public void pop(){ + if(head.next==null){ + head=null; + rear=head; + System.out.println("StackList is null"); + return; + } + StackNode temp=head; + StackNode node=null; + while (temp.next.next!=null){ + temp=temp.next; + } + temp.next=null; + rear=temp; + System.out.println(rear.value); + + } + public void peek(){ + if(head== rear){ + System.out.println("StackList is null"); + }else { + System.out.println(rear.value); + } + } + +} diff --git a/homeWork push/StackNode.java b/homeWork push/StackNode.java new file mode 100644 index 0000000000000000000000000000000000000000..52873aec2f587caa3da540517a854f166d87f53a --- /dev/null +++ b/homeWork push/StackNode.java @@ -0,0 +1,28 @@ +public class StackNode { + + int value; + StackNode next; + + public StackNode getNext() { + return next; + } + + public void setNext(StackNode next) { + this.next = next; + } + + + + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public StackNode(int value) { + this.value = value; + } +} diff --git a/homeWork push/Test.java b/homeWork push/Test.java new file mode 100644 index 0000000000000000000000000000000000000000..c239baf18d1a73cef15c0846ead6d03c17453995 --- /dev/null +++ b/homeWork push/Test.java @@ -0,0 +1,84 @@ +public class Test { + public static void main(String[] args) { + /*数组实现stack*/ + StackArray stackArray = new StackArray(5); + stackArray.push(1); + stackArray.push(2); + stackArray.push(3); + stackArray.push(4); + stackArray.push(5); + stackArray.push(6); + stackArray.pop(); + stackArray.pop(); + stackArray.peek(); + + /*链表实现stack*/ + StackList stackList = new StackList(); + stackList.push(new StackNode(5)); + stackList.push(new StackNode(7)); + stackList.push(new StackNode(8)); + stackList.push(new StackNode(1)); + stackList.push(new StackNode(2)); + stackList.pop(); + stackList.pop(); + stackList.peek(); + + /*数组队列*/ + QueueArray queueArray = new QueueArray(5); + queueArray.queueInput(1); + queueArray.queueOutput(); + queueArray.queueInput(2); + queueArray.queueOutput(); + queueArray.queueInput(3); + queueArray.queueOutput(); + queueArray.queueInput(4); + queueArray.queueOutput(); + queueArray.queueInput(5); + queueArray.queueOutput(); + queueArray.print(); + + /*链表队列*/ + QueueList list = new QueueList(); + list.queueInput(1); + list.queueInput(2); + list.queueInput(3); + list.queueInput(4); + list.queueOutput(); + + + list.printString(); + + /*循环队列*/ + QueueCircular queue = new QueueCircular(5); + queue.queueInput(1); + queue.queueInput(2); + queue.queueInput(3); + queue.queueInput(4); + queue.queueInput(5); + queue.queueOutput(); + queue.queueInput(3); + queue.queueOutput(); + queue.queueOutput(); + queue.printString(); + + /*后缀表达式*/ + ArrayList strs = new ArrayList<>(); + strs.add("6"); + strs.add("5"); + strs.add("2"); + strs.add("3"); + strs.add("+"); + strs.add("8"); + strs.add("*"); + strs.add("+"); + strs.add("3"); + strs.add("+"); + strs.add("*"); + StackCalc stackCalc = new StackCalc(); + for (int i = 0; i < strs.size(); i++) { + stackCalc.countResult(strs.get(i)); + } + stackCalc.printString(); + + } +}