From fb377710aa9240e2ce06a7a151be588583648a8b Mon Sep 17 00:00:00 2001 From: 17605914716 Date: Tue, 17 Nov 2020 23:04:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E6=A0=88=E5=92=8C=E9=98=9F?= =?UTF-8?q?=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- queue/Node.java | 38 +++++++++++++++++++++++++ queue/QueueByArray.java | 49 ++++++++++++++++++++++++++++++++ queue/QueueByLinkList.java | 40 ++++++++++++++++++++++++++ queue/TryToRun.java | 27 ++++++++++++++++++ stack/MainTest.java | 31 ++++++++++++++++++++ stack/Node.java | 39 +++++++++++++++++++++++++ stack/StackByArray.java | 37 ++++++++++++++++++++++++ stack/StackByLinkList.java | 58 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 319 insertions(+) create mode 100644 queue/Node.java create mode 100644 queue/QueueByArray.java create mode 100644 queue/QueueByLinkList.java create mode 100644 queue/TryToRun.java create mode 100644 stack/MainTest.java create mode 100644 stack/Node.java create mode 100644 stack/StackByArray.java create mode 100644 stack/StackByLinkList.java diff --git a/queue/Node.java b/queue/Node.java new file mode 100644 index 0000000..e1f2024 --- /dev/null +++ b/queue/Node.java @@ -0,0 +1,38 @@ +package queue; + +public class Node { + private Object data; + private Node next; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + + public Node(Object data) { + this.data = data; + } + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + @Override + public String toString() { + return "Node{" + + "data=" + data + + '}'; + } +} diff --git a/queue/QueueByArray.java b/queue/QueueByArray.java new file mode 100644 index 0000000..8bca646 --- /dev/null +++ b/queue/QueueByArray.java @@ -0,0 +1,49 @@ +package queue; + +import java.util.Arrays; + +public class QueueByArray { + private Object[] queue; + private int pFront; + private int pRear; + + public QueueByArray(int length) { + this.queue = new Object[length]; + this.pRear = 0; + this.pFront = 0; + } + + public void enQueue(Object data) { + int position = this.pRear; + if (this.queue[position] != null && this.queue[position==this.queue.length-1?0:position+1] !=null){ + System.out.println("队列已满"); + return; + } + System.out.println("添加:" + data.toString()); + this.queue[this.pRear] = data; + if (this.pRear == this.queue.length-1){ + this.pRear = 0; + }else { + pRear++; + } + } + + public void deQueue() { + System.out.println("删除:" + this.queue[this.pFront].toString()); + this.queue[this.pFront] = null; + if (this.pFront == this.queue.length-1){ + this.pFront = 0; + }else { + pFront++; + } + } + + @Override + public String toString() { + return "QueueByArray{" + + "queue=" + Arrays.toString(queue) + + ", pFront=" + pFront + + ", pRear=" + pRear + + '}'; + } +} diff --git a/queue/QueueByLinkList.java b/queue/QueueByLinkList.java new file mode 100644 index 0000000..b86da20 --- /dev/null +++ b/queue/QueueByLinkList.java @@ -0,0 +1,40 @@ +package queue; + +public class QueueByLinkList { + + private Node head; + + public void enQueue(Object data) { + Node temp = new Node(data); + System.out.println("入队:" + temp); + if (this.head == null) { + this.head = new Node(temp); + return; + } + Node head = this.head; + temp.setNext(head); + this.head = temp; + } + + public Object deQueue() { + Node head = this.head; + while (true) { + if(head.getNext().getNext()==null){ + Object result = head.getData(); + System.out.println("出队:" + head.getNext().toString()); + head.setNext(null); + return result; + } + head = head.getNext(); + } + } + + public void print() { + Node head = this.head; + System.out.println(head.toString()); + while (head.getNext() != null) { + head = head.getNext(); + System.out.println(head); + } + } +} diff --git a/queue/TryToRun.java b/queue/TryToRun.java new file mode 100644 index 0000000..e162f89 --- /dev/null +++ b/queue/TryToRun.java @@ -0,0 +1,27 @@ +package queue; + +public class TryToRun { + public static void main(String[] args) { +// QueueByArray queue = new QueueByArray(6); +// queue.enQueue(1); +// queue.enQueue(2); +// queue.enQueue(3); +// queue.enQueue(4); +// queue.enQueue(5); +// queue.enQueue(6); +// queue.deQueue(); +// System.out.println(queue.toString());; +// queue.deQueue(); +// System.out.println(queue.toString());; +// queue.enQueue(7); +// System.out.println(queue.toString());; +// 往下为链表实现的运行代码 + QueueByLinkList queueByLinkList = new QueueByLinkList(); + queueByLinkList.enQueue(1); + queueByLinkList.enQueue(2); + queueByLinkList.enQueue(3); + queueByLinkList.enQueue(4); + queueByLinkList.deQueue(); + queueByLinkList.print(); + } +} diff --git a/stack/MainTest.java b/stack/MainTest.java new file mode 100644 index 0000000..c25097c --- /dev/null +++ b/stack/MainTest.java @@ -0,0 +1,31 @@ +package stack; + + +public class MainTest { + public static void main(String[] args) { +// StackByArray stack = new StackByArray(6); +// int i = 5; +// stack.push(1); +// stack.push(2); +// stack.pop(); +// stack.push(3); +// stack.pop(); +// stack.push(4); +// stack.pop(); +// stack.push(5); +// stack.pop(); +// stack.push(6); +// stack.pop(); +// stack.push(7); +// stack.peek(); +// stack.pop(); +// stack.peek(); +// ========================== + StackByLinkList stackByLinkList = new StackByLinkList(); + stackByLinkList.push(1); + stackByLinkList.push(2); + stackByLinkList.push(3); + stackByLinkList.pop(); + stackByLinkList.print(); + } +} diff --git a/stack/Node.java b/stack/Node.java new file mode 100644 index 0000000..d9ea8c7 --- /dev/null +++ b/stack/Node.java @@ -0,0 +1,39 @@ +package stack; + +public class Node { + private Object data; + private Node next; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + + public Node(Object data) { + this.data = data; + } + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + @Override + public String toString() { + return "Node{" + + "data=" + data + + ", next=" + (next==null?next:next.getData()) + + '}'; + } +} diff --git a/stack/StackByArray.java b/stack/StackByArray.java new file mode 100644 index 0000000..0135f31 --- /dev/null +++ b/stack/StackByArray.java @@ -0,0 +1,37 @@ +package stack; + +public class StackByArray { + + private Object[] stack; + private int p; + + public StackByArray(int length) { + this.p = 0; + this.stack = new Object[length]; + } + + public void push(Object item) { + if (this.p == this.stack.length){ + System.out.println("栈已满。"); + return; + } + this.stack[this.p++] = item; + } + + public void pop() { + if (this.p==0){ + System.out.println("已经到底了"); + return; + } + System.out.println("删除:"+this.stack[--this.p]); + this.stack[this.p] = null; + } + + public void peek() { + if (this.p == 0){ + System.out.println("已经到底了"); + return; + } + System.out.println("栈顶元素:"+this.stack[this.p-1]); + } +} diff --git a/stack/StackByLinkList.java b/stack/StackByLinkList.java new file mode 100644 index 0000000..d46dab2 --- /dev/null +++ b/stack/StackByLinkList.java @@ -0,0 +1,58 @@ +package stack; + +public class StackByLinkList { + private Node head; + + public StackByLinkList() { + this.head = new Node(null); + } + + public void push(Object data) { + Node head = this.head; + Node node = new Node(data); + if (head.getNext() != null) { + node.setNext(head.getNext()); + } + head.setNext(node); + this.head = head; + System.out.println("新增:" + node.toString()); + } + + public Object pop() { + Node head = this.head; + Object temp = null; + if (head.getNext() != null) { + if (head.getNext().getNext() != null) { + temp = head.getNext(); + head.setNext(head.getNext().getNext()); + System.out.println("删除:" + temp.toString()); + return temp; + } + temp = head.getNext(); + head.setNext(null); + } + System.out.println("删除:" + temp.toString()); + return temp; + } + + public Object peek() { + Node head = this.head; + Object temp = null; + if (head.getNext() != null) { + if (head.getNext().getNext() != null) { + temp = head.getNext(); + System.out.println("栈顶元素:" + temp.toString()); + } + } + return temp; + } + + public void print() { + Node head = this.head; + System.out.println(head.toString()); + while (head.getNext() != null) { + head = head.getNext(); + System.out.println(head); + } + } +} -- Gitee