From 70162de20e3fa29050f03c5b69dd9c6a28dd6633 Mon Sep 17 00:00:00 2001 From: null796 <982254607@qq.com> Date: Tue, 17 Nov 2020 22:23:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=88=E5=92=8C=E9=98=9F=E5=88=97=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Queue/ArrayQueue.java | 56 +++++++++++++++++++++++++ Queue/LinkListQueue.java | 80 +++++++++++++++++++++++++++++++++++ Stack/ArrayStack.java | 51 +++++++++++++++++++++++ Stack/LinkListStack.java | 90 ++++++++++++++++++++++++++++++++++++++++ Stack/Node.java | 29 +++++++++++++ 5 files changed, 306 insertions(+) create mode 100644 Queue/ArrayQueue.java create mode 100644 Queue/LinkListQueue.java create mode 100644 Stack/ArrayStack.java create mode 100644 Stack/LinkListStack.java create mode 100644 Stack/Node.java diff --git a/Queue/ArrayQueue.java b/Queue/ArrayQueue.java new file mode 100644 index 0000000..72767cd --- /dev/null +++ b/Queue/ArrayQueue.java @@ -0,0 +1,56 @@ +package Queue; + +public class ArrayQueue { + public static void main(String[] args) { + Array queue = new Array(6); + for (int i = 0; i < 6; i++) { + queue.insert(i); + } + System.out.println(queue.isEmpty()); + + System.out.println(queue.isFull()); + while (!queue.isEmpty()) { + System.out.println(queue.remove()); + } + } +} +class Array { + private int[] arrInt;// 内置数组 + private int front;// 头指针 + private int rear;// 尾指针 + public Array(int size) { + this.arrInt = new int[size]; + front = 0; + rear = -1; + } + //判断队列是否为空 + public boolean isEmpty() { + return front == arrInt.length; + } + //判断队列是否已满 + public boolean isFull() { + return arrInt.length - 1 == rear; + } + //向队列的队尾插入一个元素 + public void insert(int item) { + if (isFull()) { + throw new RuntimeException("队列已满"); + } + arrInt[++rear] = item; + } + //获得队头元素 + public int peekFront() { + return arrInt[front]; + } + //获得队尾元素 + public int peekRear() { + return arrInt[rear]; + } + //从队列的队头移出一个元素 + public int remove() { + if (isEmpty()) { + throw new RuntimeException("队列为空"); + } + return arrInt[front++]; + } +} diff --git a/Queue/LinkListQueue.java b/Queue/LinkListQueue.java new file mode 100644 index 0000000..b47ef96 --- /dev/null +++ b/Queue/LinkListQueue.java @@ -0,0 +1,80 @@ +package Queue; + +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; + } +} + +public class LinkListQueue { + public static void main(String[] args) { + LinkQueue linkQueue=new LinkQueue(); + //入队5个 + for(int i=0;i<5;i++){ + linkQueue.inQueue(i); + } + //出队 + for(int i=0;!linkQueue.isEmpty();i++){ + System.out.print(linkQueue.outQueue()+" "); + } + + } +} diff --git a/Stack/ArrayStack.java b/Stack/ArrayStack.java new file mode 100644 index 0000000..eae4abc --- /dev/null +++ b/Stack/ArrayStack.java @@ -0,0 +1,51 @@ +package Stack; + +class Stack{ + private int maxSize; + private long[] stackArray; + private int top;//记录栈顶的下标 + + public Stack(int s) { + maxSize=s; + stackArray=new long[maxSize];//创建数组 + top=-1;//初始栈顶为-1,数组便无法访问 + } + + public void push(long j) {//进栈 + stackArray[++top]=j;//把数据压入栈中 + } + + public long pop() {//出栈,无需参数,后进先出 + return stackArray[top--]; + } + + public long peek() {//查看栈顶元素 + return stackArray[top]; + } + + public boolean isEmpty() {//判断栈是否为空 + return (top==-1); + } + + public boolean isfull() {//判断栈是否存满 + return (top==maxSize-1); + } + +} +public class ArrayStack { + public static void main(String[] args) { + Stack theStack=new Stack(10);//创建一个可以容纳10个数据项的栈 + theStack.push(1); + theStack.push(2); + theStack.push(3); + theStack.push(4); + theStack.push(5); + theStack.push(6); + + while(!theStack.isEmpty()) {//输出栈内元素,后进先出,直到判断栈为空停止 + long value =theStack.pop(); + System.out.print(value+" "); + } + + } +} diff --git a/Stack/LinkListStack.java b/Stack/LinkListStack.java new file mode 100644 index 0000000..8f933e9 --- /dev/null +++ b/Stack/LinkListStack.java @@ -0,0 +1,90 @@ +package Stack; + +public class LinkListStack { + int count;//当前栈内元素个数 + Node top;//栈顶指针 + int size;//栈大小 + + + //初始化一个空栈 + LinkListStack() { + top = null; + count = 0; + size = 10; + } + + //构造一个指定大小的空栈 + LinkListStack(int size) { + top = null; + count = 0; + this.size = size; + } + + + /** + * 采用这种方法代表这每插入一个元素,新的变成栈顶元素,同时每次都是链表的头结点 + * + * @param element + * @return + */ + public boolean push(Object element) { + + if (isFull()) { + return false; + } + + //1.将插入前的栈顶元素变成新插入结点的next指针指向元素,2.新插入元素变成栈顶结点 + + top = new Node(top, element); + + count++; + + return false; + } + + public Object pop() { + if (isEmpty()) { + System.out.println("栈为空"); + return null; + } + Object element = top.getElement(); + top = top.getNext(); + count--; + return element; + } + + public boolean isEmpty() { + return count == 0 ? true : false; + } + + public int size() { + return count; + } + + public Object peek() { + if(isEmpty()){ + return null; + } + return top.getElement(); + } + + public boolean isFull() { + return this.count == this.size ? true : false; + } + + public static void main(String[] args) { + + LinkListStack stack = new LinkListStack(); + + stack.push("元素1"); + stack.push("元素2"); + stack.push("元素3"); + System.out.println(stack.peek()); + stack.pop(); + System.out.println(stack.peek()); + stack.pop(); + System.out.println(stack.peek()); + + } + +} diff --git a/Stack/Node.java b/Stack/Node.java new file mode 100644 index 0000000..b7f65e3 --- /dev/null +++ b/Stack/Node.java @@ -0,0 +1,29 @@ +package Stack; + +class Node { + + Node next; + Object element; + + public Node(Node next, Object element) { + this.next = next; + this.element = element; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + + public Object getElement() { + return element; + } + + public void setElement(Object element) { + this.element = element; + } + +} -- Gitee