From 2904f6d87d89240357541207abc76c4a63a405ba Mon Sep 17 00:00:00 2001 From: hmy <1366553174@qq.com> Date: Sun, 15 Nov 2020 12:14:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArrayPen.java | 68 +++++++++++++++++++++++ ArrayQueue.java | 69 ++++++++++++++++++++++++ LinkedListPen.java | 48 +++++++++++++++++ LinkedListQueue.java | 50 +++++++++++++++++ MYLinkedList.java | 54 +++++++++++++++++++ Main.java | 82 ++++++++++++++++++++++++++++ Node.java | 8 +++ Stack-pseudocode.java => Stack.java | 84 ++++++++++++++--------------- 8 files changed, 421 insertions(+), 42 deletions(-) create mode 100644 ArrayPen.java create mode 100644 ArrayQueue.java create mode 100644 LinkedListPen.java create mode 100644 LinkedListQueue.java create mode 100644 MYLinkedList.java create mode 100644 Main.java create mode 100644 Node.java rename Stack-pseudocode.java => Stack.java (82%) mode change 100755 => 100644 diff --git a/ArrayPen.java b/ArrayPen.java new file mode 100644 index 0000000..2565bb7 --- /dev/null +++ b/ArrayPen.java @@ -0,0 +1,68 @@ +import java.lang.reflect.Array; +import java.util.Arrays; + +public class ArrayPen { + private Object[] pen; + private int topPointer;//栈顶指针 + + // 数组现实的栈 + public ArrayPen(int length) { + pen = new Object[length]; + } + + // 入栈 + public void push(T t) { + if (t == null) { + System.out.println("插入失败:元素不能为空"); + return; + } + try { + pen[topPointer] = t; + ++topPointer;//成功后自增 + } catch (Exception e) { + System.out.println("插入失败,栈已满"); + } + } + + // 出栈 + public T pop() { + if (isEmpty()) { + return null; + } + try { + T t = (T) pen[topPointer - 1]; + pen[topPointer - 1] = null;//取出元素后清空 + topPointer--;//成功后 + return t; + } catch (Exception e) { + System.out.println("出栈失败,栈已空"); + } + return null; + } + + //查看栈顶元素 + public T peek() { + if (isEmpty()) { + return null; + } + try { + return (T) pen[topPointer - 1]; + } catch (Exception e) { + System.out.println("查看失败,栈已空"); + } + return null; + } + + public int size() { + return topPointer; + } + + public boolean isEmpty() { + if (topPointer == 0) { + return true; + } + return false; + } + + +} diff --git a/ArrayQueue.java b/ArrayQueue.java new file mode 100644 index 0000000..f14b82c --- /dev/null +++ b/ArrayQueue.java @@ -0,0 +1,69 @@ +import java.util.LinkedHashMap; + +public class ArrayQueue { + // 先进先出 + private Object[] array; + private int rear = 0; + + ArrayQueue(int length) { + array = new Object[length]; + } + + //插入 + public void add(T item) { + if (item == null) { + System.out.println("插入失败:元素不能为空"); + return; + } + try { + array[rear] = item; + rear++;//成功后自增 + } catch (Exception e) { + System.out.println("插入失败,队列已满"); + } + } + + //移除头部元素 + public T remove() { + if (isEmpty()) { + System.out.println("无元素"); + return null; + } + try { + int i = 0; + T t = (T) array[0]; + for (int j = 0; j < rear - 1; j++) { + array[j] = array[j + 1]; + i++; + } + rear = i; + return t; + } catch (Exception e) { + System.out.println("移除失败"); + } + return null; + + } + + // 获取头部元素 + public T peek() { + if (isEmpty()) { + System.out.println("无元素"); + return null; + } + T t = (T) array[0]; + return t; + } + + public int size() { + return rear; + } + + public boolean isEmpty() { + if (rear == 0) { + return true; + } + return false; + } + +} diff --git a/LinkedListPen.java b/LinkedListPen.java new file mode 100644 index 0000000..617f202 --- /dev/null +++ b/LinkedListPen.java @@ -0,0 +1,48 @@ +public class LinkedListPen { + //栈顶即为链表头部元素 + + private MYLinkedList myLinkedList = new MYLinkedList<>(); + private int size=0; + + // 入栈(插入到链表头部) + public void push(E value) { + if (value==null){ + System.out.println("插入失败:元素不能为空"); + return; + } + myLinkedList.addToFront(value); + size++; + } + + // 出栈(从链表头部取出) + public E pop() { + if(isEmpty()){ + return null; + } + E e = myLinkedList.delTopNode(); + size--; + return e; + } + + //查看栈顶元素 + public E peek() { + if(isEmpty()){ + return null; + } + E e = myLinkedList.inquiryTopNode(); + return e; + } + + public int size() { + return this.size; + } + + public boolean isEmpty() { + if (this.size == 0) { + return true; + } + return false; + } + + +} diff --git a/LinkedListQueue.java b/LinkedListQueue.java new file mode 100644 index 0000000..6787752 --- /dev/null +++ b/LinkedListQueue.java @@ -0,0 +1,50 @@ +public class LinkedListQueue { + private MYLinkedList myLinkedList = new MYLinkedList<>(); + private int size; + + //插入 + public void add(E item) { + myLinkedList.addToRear(item); + size++; + } + + + //移除头部元素 + public E remove() { + if (isEmpty()) { + System.out.println("无元素"); + return null; + } + try { + E e = myLinkedList.delTopNode(); + size--; + return e; + } catch (Exception e) { + System.out.println("移除失败"); + } + return null; + + } + + // 获取头部元素 + public E peek() { + if (isEmpty()) { + System.out.println("无元素"); + return null; + } + E e = myLinkedList.inquiryTopNode(); + return e; + } + + public int size() { + return size; + } + + public boolean isEmpty() { + if (size == 0) { + return true; + } + return false; + } + +} diff --git a/MYLinkedList.java b/MYLinkedList.java new file mode 100644 index 0000000..67ea66a --- /dev/null +++ b/MYLinkedList.java @@ -0,0 +1,54 @@ +public class MYLinkedList { + private Node head, tail; + private int size = 0; + + public int getSize() { + return size; + } + + public void addToFront(E value) { + Node node = new Node(value); + if (head == null) { + head = node; + tail = head; + } else { + node.next = head; + head = node; + } + size++; + } + + public void addToRear(E value) { + Node node = new Node(value); + if (tail == null) { + tail = node; + head = tail; + } else { + tail.next = node; + tail = node; + } + size++; + } + + + //删除头部节点 + public E delTopNode() { + if (head == null) { + return null; + } + E value = (E) head.value; + head = head.next;//删除头节点 + size--; + return value; + } + + //查看头部节点 + public E inquiryTopNode() { + if (head == null) { + return null; + } + return (E) head.value; + } + + +} diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..25d9477 --- /dev/null +++ b/Main.java @@ -0,0 +1,82 @@ +public class Main { + public static void main(String[] args) { + System.out.println("基础部分"); + System.out.println("1.用数组实现栈*********************************************"); + ArrayPen arrayPen = new ArrayPen(3); + System.out.println("栈是否为空" + arrayPen.isEmpty()); + arrayPen.push("1"); + arrayPen.push("2"); + arrayPen.push(null); + arrayPen.push("4"); + System.out.println("查看栈顶元素" + arrayPen.peek()); + System.out.println("出栈" + arrayPen.pop()); + System.out.println("查看栈顶元素" + arrayPen.peek()); + System.out.println("栈内元素个数:" + arrayPen.size() + "个"); + System.out.println("栈是否为空" + arrayPen.isEmpty()); + + System.out.println(); + System.out.println(); + + System.out.println("2.用带尾指针的链表实现栈************************************"); + LinkedListPen linkedListPen = new LinkedListPen<>(); + System.out.println("栈是否为空" + linkedListPen.isEmpty()); + linkedListPen.push("1"); + linkedListPen.push("2"); + linkedListPen.push(null); + linkedListPen.push("4"); + System.out.println("查看栈顶元素" + linkedListPen.peek()); + System.out.println("出栈" + linkedListPen.pop()); + System.out.println("查看栈顶元素" + linkedListPen.peek()); + System.out.println("栈内元素个数:" + linkedListPen.size() + "个"); + System.out.println("栈是否为空" + linkedListPen.isEmpty()); + + System.out.println(); + System.out.println(); + + System.out.println("3.用数组实现队列*******************************************"); + ArrayQueue arrayQueue = new ArrayQueue<>(4); + System.out.println("队列是否为空" + arrayQueue.isEmpty()); + arrayQueue.add("1"); + arrayQueue.add("2"); + arrayQueue.add("3"); + arrayQueue.add("4"); + System.out.println("查看队列头部元素" + arrayQueue.peek()); + System.out.println("移除头部元素" + arrayQueue.remove()); + System.out.println("查看队列头部元素" + arrayQueue.peek()); + System.out.println("队列元素个数:" + arrayQueue.size() + "个"); + System.out.println("队列是否为空" + arrayQueue.isEmpty()); + + System.out.println(); + System.out.println(); + + + System.out.println("4.用链表实现队列*******************************************"); + LinkedListQueue linkedListQueue = new LinkedListQueue<>(); + System.out.println("队列是否为空" + linkedListQueue.isEmpty()); + linkedListQueue.add("1"); + linkedListQueue.add("2"); + linkedListQueue.add("3"); + linkedListQueue.add("4"); + System.out.println("查看队列头部元素" + linkedListQueue.peek()); + System.out.println("移除头部元素" + linkedListQueue.remove()); + System.out.println("查看队列头部元素" + linkedListQueue.peek()); + System.out.println("队列元素个数:" + linkedListQueue.size() + "个"); + System.out.println("队列是否为空" + linkedListQueue.isEmpty()); + + System.out.println(); + System.out.println(); + System.out.println(); + System.out.println(); + + System.out.println("进阶部分"); + System.out.println("1.实现循环队列*********************************************"); + + + + System.out.println("2.用栈解决迷宫问题******************************************"); + System.out.println("3.用队列解决迷宫问题****************************************"); + System.out.println("4.后缀表达式计算*******************************************"); + System.out.println("4.编程模拟实现一个浏览器的前进、后退功能***********************"); + + } +} diff --git a/Node.java b/Node.java new file mode 100644 index 0000000..72661db --- /dev/null +++ b/Node.java @@ -0,0 +1,8 @@ +public class Node { + E value; + Node next = null; + + Node(E value) { + this.value = value; + } +} diff --git a/Stack-pseudocode.java b/Stack.java old mode 100755 new mode 100644 similarity index 82% rename from Stack-pseudocode.java rename to Stack.java index 567e476..d2da4eb --- a/Stack-pseudocode.java +++ b/Stack.java @@ -1,42 +1,42 @@ -public class Stack { - public int[] stack; - public int top; - private int size; - - Stack(int size){ - // 构建一个长度为size大小的空数组来模拟栈 - this.size = size; - stack = new int[size]; - top = 0; // top永远指向下一个可放入的位置 - } - - - public void push(int item){ - if(top >= size ){ - throw new Exception("StackOverflowError"); - } - - stack[top] = item; - top++; - } - - - public int pop(){ - if(top <=0 ){ - throw new Exception("StackEmpty"); - } - - return stack[top--]; - - } - - public int peek(){ - - if(top <=0 ){ - throw new Exception("StackEmpty"); - } - return stack[top-1]; - } -} - - +public class Stack { + public int[] stack; + public int top; + private int size; + + Stack(int size){ + // 构建一个长度为size大小的空数组来模拟栈 + this.size = size; + stack = new int[size]; + top = 0; // top永远指向下一个可放入的位置 + } + + + public void push(int item) throws Exception { + if(top >= size ){ + throw new Exception("StackOverflowError"); + } + + stack[top] = item; + top++; + } + + + public int pop() throws Exception{ + if(top <=0 ){ + throw new Exception("StackEmpty"); + } + + return stack[top--]; + + } + + public int peek() throws Exception{ + + if(top <=0 ){ + throw new Exception("StackEmpty"); + } + return stack[top-1]; + } +} + + -- Gitee From db7b143e5f6ba32db678e7bbdba07018dce5ccbc Mon Sep 17 00:00:00 2001 From: hmy <1366553174@qq.com> Date: Sun, 15 Nov 2020 16:20:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=83=A8=E5=88=86?= =?UTF-8?q?=E8=BF=9B=E9=98=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CircularQueue.java | 63 +++++++++++++++++++++++++++++++++++++++++++ Main.java | 44 ++++++++++++++++++++++++++++-- Maze.java | 37 +++++++++++++++++++++++++ PenMaze.java | 5 ++++ SuffixCalculator.java | 61 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 208 insertions(+), 2 deletions(-) create mode 100644 CircularQueue.java create mode 100644 Maze.java create mode 100644 PenMaze.java create mode 100644 SuffixCalculator.java diff --git a/CircularQueue.java b/CircularQueue.java new file mode 100644 index 0000000..842c4e7 --- /dev/null +++ b/CircularQueue.java @@ -0,0 +1,63 @@ +public class CircularQueue { + private Object[] array; + private int count, head, tail; + + CircularQueue(int size) { + array = new Object[size]; + } + + public void add(E value) { + if (value == null) { + System.out.println("插入失败内容不能为空"); + return; + } + if (count==array.length){ + System.out.println("插入失败队列已满"); + return; + } + array[tail] = value; + tail++; + count++; + if (tail>array.length-1){ + tail=0; + } + + } + + + //移除头部元素 + public E remove() { + if (isEmpty()) { + System.out.println("移除失败队列已空"); + return null; + } + E e = (E) array[head]; + head++; + count--; + if (head > array.length - 1) { + head = 0; + } + return e; + } + + + // 获取头部元素 + public E peek() { + if (isEmpty()) { + System.out.println("队列无元素"); + return null; + } + return (E) array[head]; + } + + public int size() { + return count; + } + + public boolean isEmpty() { + if (count == 0) { + return true; + } + return false; + } +} diff --git a/Main.java b/Main.java index 25d9477..36e16db 100644 --- a/Main.java +++ b/Main.java @@ -1,3 +1,5 @@ +import java.util.Arrays; + public class Main { public static void main(String[] args) { System.out.println("基础部分"); @@ -70,13 +72,51 @@ public class Main { System.out.println("进阶部分"); System.out.println("1.实现循环队列*********************************************"); + CircularQueue circularQueue = new CircularQueue<>(3); + System.out.println("队列是否为空" + circularQueue.isEmpty()); + circularQueue.add("1"); + circularQueue.add("2"); + circularQueue.add("3"); + circularQueue.add("4"); + System.out.println("移除头部元素" + circularQueue.remove()); + System.out.println("移除头部元素" + circularQueue.remove()); + System.out.println("移除头部元素" + circularQueue.remove()); + System.out.println("移除头部元素" + circularQueue.remove()); + circularQueue.add("1"); + System.out.println("查看队列头部元素" + circularQueue.peek()); - - + System.out.println(); + System.out.println(); System.out.println("2.用栈解决迷宫问题******************************************"); +// int[][] maze = { +// {0,0,0,0,0,0,0,0}, +// {0,1,1,1,1,1,1,0}, +// {0,1,0,1,0,0,0,0}, +// {0,0,0,1,1,1,1,0}, +// {0,1,0,0,0,0,1,0}, +// {0,1,0,0,1,0,1,0}, +// {0,1,1,1,1,0,1,0}, +// {0,0,0,0,0,0,0,0}, +// }; +// for (int i=0;i objectLinkedListPen; + + public String calculate(String expression) { + //未考虑表达式合法性 + objectLinkedListPen = new LinkedListPen<>(); + if (expression.length() > 0) { + String[] expressionArray = expression.split(" "); + for (int i = 0; i < expressionArray.length; i++) { + String value = expressionArray[i]; + switch (value) { + case "+": + case "-": + case "*": + case "/": + fundamentalOperation(value); + break; + default: + objectLinkedListPen.push(value); + break; + } + + } + return objectLinkedListPen.pop(); + } + return ""; + } + + private void fundamentalOperation(String value) { + double b1 = Double.valueOf(objectLinkedListPen.pop()); + double b2 = Double.valueOf(objectLinkedListPen.pop()); + String newValue = ""; + switch (value) { + case "+": + newValue = String.valueOf(b1 + b2); + break; + + case "-": + newValue = String.valueOf(b1 - b2); + break; + + case "*": + newValue = String.valueOf(b1 * b2); + break; + + case "/": + newValue = String.valueOf(b1 / b2); + break; + } + objectLinkedListPen.push(newValue); + } + + +} -- Gitee