From 7b766e7610ba6c40381adc0f91bd84e40e5233d4 Mon Sep 17 00:00:00 2001 From: unknown <1939123910@qq.com> Date: Tue, 17 Nov 2020 22:08:33 +0800 Subject: [PATCH] zzx --- "\344\275\234\344\270\232/ArrayQueue.java" | 47 ++++++++++++++++ "\344\275\234\344\270\232/LinkListQueue.java" | 56 +++++++++++++++++++ "\344\275\234\344\270\232/LinkListStack.java" | 55 ++++++++++++++++++ "\344\275\234\344\270\232/Stack.java" | 51 +++++++++++++++++ 4 files changed, 209 insertions(+) create mode 100644 "\344\275\234\344\270\232/ArrayQueue.java" create mode 100644 "\344\275\234\344\270\232/LinkListQueue.java" create mode 100644 "\344\275\234\344\270\232/LinkListStack.java" create mode 100644 "\344\275\234\344\270\232/Stack.java" diff --git "a/\344\275\234\344\270\232/ArrayQueue.java" "b/\344\275\234\344\270\232/ArrayQueue.java" new file mode 100644 index 0000000..f616314 --- /dev/null +++ "b/\344\275\234\344\270\232/ArrayQueue.java" @@ -0,0 +1,47 @@ +public class ArrayQueue { + int front,rear,nowsize; + int capacity; + int arrayQueue[]; + public ArrayQueue(int capacity){ + this.capacity = capacity; + front = rear = nowsize =0; + arrayQueue =new int[capacity]; + + } + public boolean NullArrayQueue(){ //队列为空 + return nowsize == 0; + } + public boolean FullQueue(){ + return nowsize == capacity; + } + public void enQueue(int item) { + if (FullQueue()) + // return; + System.out.println("队列已满"); + else { + arrayQueue[rear] = item; + rear = (rear + 1) % capacity; + nowsize++; + System.out.println(item + "入队"); + } + } + public void outQueue(){ + if(NullArrayQueue()){ + System.out.println("队列为空"); + } + else { + int val = arrayQueue[front]; + front = (front + 1) % capacity; + nowsize--; + System.out.println(val + "出队"); + } + } + public static void main(String args[]){ + ArrayQueue test1 = new ArrayQueue(20); + test1.enQueue(1); + test1.enQueue(2); + test1.enQueue(3); + test1.outQueue(); + + } +} diff --git "a/\344\275\234\344\270\232/LinkListQueue.java" "b/\344\275\234\344\270\232/LinkListQueue.java" new file mode 100644 index 0000000..2319c3e --- /dev/null +++ "b/\344\275\234\344\270\232/LinkListQueue.java" @@ -0,0 +1,56 @@ +import jdk.swing.interop.SwingInterOpUtils; + +public class LinkListQueue { + Queue front; + Queue rear; + + static class Queue { + int value; + Queue next; + + public Queue(int value) { + this.value = value; + } + } + public void enQueue(int value){ + Queue newNode = new Queue(value); + if(this.rear ==null){ + this.front = this.rear = newNode; + return; + } + this.rear.next = newNode; + this.rear = newNode; + + } + public void PrintQueue(){ + Queue temp=this.front; + while (temp!=null){ + System.out.print(temp.value+"==>"); + temp=temp.next; + } + System.out.println(); + } + public int outQueue(){ + if(this.front == null){ + System.out.println("队列为空"); + } + Queue front = this.front; + this.front = this.front.next; + if(this.front ==null){ //如果只有一个节点 + this.rear = null; + } + return front.value; + } + + public static void main(String[] args) { + LinkListQueue test3 = new LinkListQueue(); + test3.enQueue(1); + test3.enQueue(1); + test3.enQueue(2); + test3.enQueue(3); + test3.outQueue(); + test3.PrintQueue(); + + } + } + diff --git "a/\344\275\234\344\270\232/LinkListStack.java" "b/\344\275\234\344\270\232/LinkListStack.java" new file mode 100644 index 0000000..7d010d2 --- /dev/null +++ "b/\344\275\234\344\270\232/LinkListStack.java" @@ -0,0 +1,55 @@ +public class LinkListStack { + int value; + StackNode TopofNode; + public class StackNode{ + int value; + StackNode next; + + public StackNode(int value){ + this.value = value; + } + } + public LinkListStack(){ + TopofNode = null; + } + public void push(int value){ + StackNode newNode = new StackNode(value); + if(TopofNode == null){ + TopofNode = newNode; + } + else { + StackNode preNode = TopofNode; //将原来的节点先保存在上一个节点 + TopofNode = newNode; //将新压栈节点保存在栈顶 + newNode.next = preNode; + } + System.out.println("以入栈"); + } + public void pop(){ + if(TopofNode == null){ + System.out.println("栈为空"); + } + else{ + StackNode popNode = TopofNode; + TopofNode = TopofNode.next; + } + System.out.println("出栈成功"); + } + public int peek(){ + if(TopofNode == null){ + System.out.println("栈为空"); + } + + return TopofNode.value; + } + public static void main(String[] args) { + LinkListStack item = new LinkListStack(); + item.push(1); + item.push(2); + item.push(3); + item.pop(); + item.pop(); + item.peek(); + } +} + + diff --git "a/\344\275\234\344\270\232/Stack.java" "b/\344\275\234\344\270\232/Stack.java" new file mode 100644 index 0000000..3e33873 --- /dev/null +++ "b/\344\275\234\344\270\232/Stack.java" @@ -0,0 +1,51 @@ +import jdk.swing.interop.SwingInterOpUtils; + +public class Stack { + static final int A = 1997; + private int[] stack; + private int topofstack; //栈顶 + + public Stack() { //初始化一个栈 长度为20 + topofstack = -1; + stack = new int[A]; + } + public boolean push(int val){ + if(topofstack >= A){ + System.out.println("栈满"); + return false; + } + stack[++topofstack] = val; + topofstack++; + return true; + } + public void pop() { + if (topofstack < 0) { + System.out.println("栈为空,没有可以元素可以出栈"); + + } else { + stack[topofstack] = 0; //将栈顶的元素置为空 + topofstack--; //指针减一 + } + } + public int peek(){ + if(topofstack < 0){ + System.out.println("没有元素"); + return -1; + } + else { + return stack[topofstack]; + } + } + public static void main(String[] args) { + Stack test1 = new Stack(); + test1.push(1); + test1.push(9); + test1.push(9); + System.out.println(test1.push(7)); + for (int a = 0;a < 4;a++){ + System.out.println(test1.stack[a]); + } + } +} + + -- Gitee