From daf1f06b25b5ef1d926b3133d182fc5aa539488f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E5=AE=9B=E5=A9=B7?= <8110397+kyoong56@user.noreply.gitee.com> Date: Mon, 19 Oct 2020 15:43:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=88=E5=92=8C=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stack-queue/.idea/misc.xml | 6 ++ stack-queue/.idea/modules.xml | 8 ++ stack-queue/.idea/uiDesigner.xml | 124 +++++++++++++++++++++++++++++++ stack-queue/.idea/workspace.xml | 74 ++++++++++++++++++ stack-queue/src/LLNode.java | 24 ++++++ stack-queue/src/LLStack.java | 70 +++++++++++++++++ stack-queue/src/LinkQueue.java | 78 +++++++++++++++++++ stack-queue/src/QueueTest.java | 76 +++++++++++++++++++ stack-queue/src/Stack.java | 91 +++++++++++++++++++++++ stack-queue/stack-queue.iml | 11 +++ 10 files changed, 562 insertions(+) create mode 100644 stack-queue/.idea/misc.xml create mode 100644 stack-queue/.idea/modules.xml create mode 100644 stack-queue/.idea/uiDesigner.xml create mode 100644 stack-queue/.idea/workspace.xml create mode 100644 stack-queue/src/LLNode.java create mode 100644 stack-queue/src/LLStack.java create mode 100644 stack-queue/src/LinkQueue.java create mode 100644 stack-queue/src/QueueTest.java create mode 100644 stack-queue/src/Stack.java create mode 100644 stack-queue/stack-queue.iml diff --git a/stack-queue/.idea/misc.xml b/stack-queue/.idea/misc.xml new file mode 100644 index 0000000..6f6666d --- /dev/null +++ b/stack-queue/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/stack-queue/.idea/modules.xml b/stack-queue/.idea/modules.xml new file mode 100644 index 0000000..76c079b --- /dev/null +++ b/stack-queue/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/stack-queue/.idea/uiDesigner.xml b/stack-queue/.idea/uiDesigner.xml new file mode 100644 index 0000000..b93ac08 --- /dev/null +++ b/stack-queue/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/stack-queue/.idea/workspace.xml b/stack-queue/.idea/workspace.xml new file mode 100644 index 0000000..98d684f --- /dev/null +++ b/stack-queue/.idea/workspace.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1603091141120 + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/stack-queue/src/LLNode.java b/stack-queue/src/LLNode.java new file mode 100644 index 0000000..922717f --- /dev/null +++ b/stack-queue/src/LLNode.java @@ -0,0 +1,24 @@ +public class LLNode { + + private Object data;//存放数据 + private LLNode next;//指向下一个节点 + public LLNode(){ + + } + public LLNode(Object data){ + this.data=data; + } + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public LLNode getNext() { + return next; + } + public void setNext(LLNode next) { + this.next = next; + } + +} diff --git a/stack-queue/src/LLStack.java b/stack-queue/src/LLStack.java new file mode 100644 index 0000000..40bfed4 --- /dev/null +++ b/stack-queue/src/LLStack.java @@ -0,0 +1,70 @@ +public class LLStack { + LLNode headnode = null; + + public LLStack() { + headnode = new LLNode(null);//先初始化 + } + + public boolean isEmpty() {//判断是否为空的 + return headnode == null; + } + + public void push(Object data) {//入栈 + if (headnode.getData() == null) {//判断头结点的值为空的时候 + headnode.setData(data); + } else if (headnode == null) { + headnode = new LLNode(data); + } else { + LLNode newnode = new LLNode(data); + newnode.setNext(headnode); + headnode = newnode; + } + } + + + public Object pop() {//出栈(返回栈顶的值,并且删除) + Object data = null; + if (isEmpty()) { + System.out.println("栈为空,返回值为0"); + return 0; + } + data = headnode.getData(); + headnode = headnode.getNext(); + return data; + } + + public Object top() {//返回栈顶的值,但是不删除 + Object data = null; + if (isEmpty()) { + System.out.println("栈为空,返回值为0"); + return 0; + } + data = headnode.getData(); + return data; + } + + public int getLength() {//得到栈里面值的个数 + int count = 0; + LLNode tempnode = headnode; + if (isEmpty() || tempnode.getData() == null)//当头结点为空,并且值也为空的时候就返回0 + { + } else { + while (tempnode != null) { + count++; + tempnode = tempnode.getNext(); + } + } + return count; + } + + public static void main(String[] args) { + LLStack llStack = new LLStack(); + llStack.push(1); + llStack.push(2); + llStack.push(3); + System.out.println("栈里面值的个数为:" + llStack.getLength()); + llStack.pop(); + System.out.println("pop一个之后,栈里面的个数 为 :" + llStack.getLength()); + System.out.println("pop一个之后,栈顶的值为:" + llStack.top()); + } +} diff --git a/stack-queue/src/LinkQueue.java b/stack-queue/src/LinkQueue.java new file mode 100644 index 0000000..9b5bd7e --- /dev/null +++ b/stack-queue/src/LinkQueue.java @@ -0,0 +1,78 @@ +public class LinkQueue { + static class Node + { + private final T data;//数据域 + private Node next;//引用域 + public Node() + { + this.data=null; + this.next=null; + } + public Node(T data) + { + this.data=data; + this.next=null; + } + public void setNext(Node t) + { + this.next=t; + } + public T getData() + { + return this.data; + } + public Node getNext() + { + return this.next; + } + } + private Node head;//队头 + private Node tail;//队尾 + public LinkQueue() + { + this.head=null; + this.tail=null; + } + //入队 + public boolean inQueue(T t) + { + Node p= new Node(t);//生成一个结点 + if(head==null)//如果头等于空 + { + head=p;//头引用指向这个结点 + tail=p;//尾引用指向这个结点 + } + else + { + tail.next=p;//插入尾部 + tail=p;//尾引用指向新的尾结点 + } + return true; + } + //dream it possible + //出队 + public T outQueue() + { + if(head==null) return null; + else + { + T t=head.getData();//取下对队头 + head=head.next;//头引用后移 + return t; + } + + } + //查队头 + public T peek() + { + if(head==null) return null; + else + return head.getData(); + } + //判空 + public boolean isEmpty() + { + return head==null; + } + +} diff --git a/stack-queue/src/QueueTest.java b/stack-queue/src/QueueTest.java new file mode 100644 index 0000000..ac15d0c --- /dev/null +++ b/stack-queue/src/QueueTest.java @@ -0,0 +1,76 @@ +public class QueueTest { + public static void main(String[] args) { + ArrayQueue queue = new ArrayQueue(10); + System.out.println(queue.isEmpty()); + for (int i = 0; i < 10; i++) { + queue.insert(i); + } + System.out.println(queue.isFull()); + while (!queue.isEmpty()) { + System.out.println(queue.remove()); + } + } +} +class ArrayQueue { + private int[] arrInt;// 内置数组 + private int front;// 头指针 + private int rear;// 尾指针 + public ArrayQueue(int size) { + this.arrInt = new int[size]; + front = 0; + rear = -1; + } + /** + * 判断队列是否为空 + * + * @return + */ + public boolean isEmpty() { + return front == arrInt.length; + } + /** + * 判断队列是否已满 + * + * @return + */ + public boolean isFull() { + return arrInt.length - 1 == rear; + } + /** + * 向队列的队尾插入一个元素 + */ + public void insert(int item) { + if (isFull()) { + throw new RuntimeException("队列已满"); + } + arrInt[++rear] = item; + } + /** + * 获得对头元素 + * + * @return + */ + public int peekFront() { + return arrInt[front]; + } + /** + * 获得队尾元素 + * + * @return + */ + public int peekRear() { + return arrInt[rear]; + } + /** + * 从队列的对头移除一个元素 + * + * @return + */ + public int remove() { + if (isEmpty()) { + throw new RuntimeException("队列为空"); + } + return arrInt[front++]; + } + +} diff --git a/stack-queue/src/Stack.java b/stack-queue/src/Stack.java new file mode 100644 index 0000000..af7da95 --- /dev/null +++ b/stack-queue/src/Stack.java @@ -0,0 +1,91 @@ +public class Stack { + //存数据的数组 + int[] data; + + //栈的最大长度 + private final int size; + //栈顶的位置 + private int top; + + public Stack(int size) { + this.size = size; + data = new int[size]; + top = -1; + } + + public int getSize() { + return size; + } + + public int getTop() { + return top; + } + + /** + * 判断是否为空栈 + * @return + */ + public boolean isEmpty() { + return top == -1; + } + + /** + * 判断是否为满栈 + * @return + */ + public boolean isFull() { + return (top+1) == size; + } + + /** + * 压栈操作 + * @param data + */ + public void push(int data) { + if(isFull()) { + System.out.println("the stack is full!"); + } else { + top++; + this.data[top] = data; + } + } + + + /** + * 弹栈操作 + * @return + * @throws Exception + */ + public int pop() throws Exception { + if(isEmpty()) { + throw new Exception("the stack is empty!"); + } else { + return this.data[top--]; + } + } + + /** + * 获取栈顶的元素,但不弹栈 + * @return + */ + public int peek() { + return this.data[getTop()]; + } + + public static void main(String[] args) { + Stack stack = new Stack(20); + stack.push(0); + stack.push(1); + stack.push(2); + stack.push(3); + System.out.println("Now the top_num is:" + stack.peek()); + + while(! stack.isEmpty()) { + try { + System.out.println(stack.pop()); + } catch (Exception e) { + e.printStackTrace(); + } + } + } +} diff --git a/stack-queue/stack-queue.iml b/stack-queue/stack-queue.iml new file mode 100644 index 0000000..9465dd8 --- /dev/null +++ b/stack-queue/stack-queue.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file -- Gitee