From 62b75a072978ad2da94f3749270c5302e5aba1c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E9=9C=B2=E7=8E=B2?= <8110312+cao-luling@user.noreply.gitee.com> Date: Mon, 19 Oct 2020 15:59:11 +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 --- ArrayStack.java | 66 ++++++++++++++++++++++++++++++++++++++++ LinkQueue.java | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ QueueTest.java | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 225 insertions(+) create mode 100644 ArrayStack.java create mode 100644 LinkQueue.java create mode 100644 QueueTest.java diff --git a/ArrayStack.java b/ArrayStack.java new file mode 100644 index 0000000..e321bc6 --- /dev/null +++ b/ArrayStack.java @@ -0,0 +1,66 @@ +package zhan; + +public class ArrayStack { + private long[] a; + private int size;//栈数组的大小 + private int top;//栈顶 + + public ArrayStack(int maxSize) { + this.size = maxSize; + this.a = new long[size]; + this.top = -1;//表示空栈 + } + + //入栈 + public void push(long value){ + if(isFull()){ + System.out.println("栈已经满!"); + return; + } + a[++top] = value; + } + + //返回栈顶内容,但是不删除 + public void peek(){ + if(isEmpty()){ + System.out.println("栈中没有数据!"); + } + System.out.println(a[top]); + } + + //弹出栈顶内容,删除 + public long pop(){ + if(isEmpty()){ + System.out.println("栈中没有数据!"); + return 0; + } + return a[top--]; + } + + //size栈的大小 + public int size(){ + return top + 1; + } + + //是否为空 + public boolean isEmpty() { + return (top == -1); + } + + //是否满了 + public boolean isFull() { + return (top == size - 1); + } + + //显示 + public void display(){ + System.out.print("["); + for (int i = top; i >=0 ; i--) { + System.out.print(a[i]); + if(i!=0){ + System.out.print(","); + } + } + System.out.println("]"); + } +} diff --git a/LinkQueue.java b/LinkQueue.java new file mode 100644 index 0000000..469a7be --- /dev/null +++ b/LinkQueue.java @@ -0,0 +1,81 @@ +package zhan; + +public class LinkQueue +{ + class Node + { + private 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/QueueTest.java b/QueueTest.java new file mode 100644 index 0000000..7a7f0e1 --- /dev/null +++ b/QueueTest.java @@ -0,0 +1,78 @@ +package zhan; + +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++]; + } + +} -- Gitee