diff --git "a/\346\240\210\344\270\216\351\230\237\345\210\227/ArrayStack.class" "b/\346\240\210\344\270\216\351\230\237\345\210\227/ArrayStack.class" new file mode 100644 index 0000000000000000000000000000000000000000..fb404ea3b27e1d46be005173c98ee1270b3aa963 Binary files /dev/null and "b/\346\240\210\344\270\216\351\230\237\345\210\227/ArrayStack.class" differ diff --git "a/\346\240\210\344\270\216\351\230\237\345\210\227/ArrayStackDemo.class" "b/\346\240\210\344\270\216\351\230\237\345\210\227/ArrayStackDemo.class" new file mode 100644 index 0000000000000000000000000000000000000000..c368234bb393e9334d87ec75f088cb22ddf8bf07 Binary files /dev/null and "b/\346\240\210\344\270\216\351\230\237\345\210\227/ArrayStackDemo.class" differ diff --git "a/\346\240\210\344\270\216\351\230\237\345\210\227/ArrayStackDemo.java" "b/\346\240\210\344\270\216\351\230\237\345\210\227/ArrayStackDemo.java" new file mode 100644 index 0000000000000000000000000000000000000000..f55ff12c0da191a455f4de647a84fd2944ac6a19 --- /dev/null +++ "b/\346\240\210\344\270\216\351\230\237\345\210\227/ArrayStackDemo.java" @@ -0,0 +1,108 @@ +package cn.mrlij.stack; + +import java.util.Arrays; +import java.util.Scanner; + +public class ArrayStackDemo { + public static void main(String[] args) { + // 测试 + ArrayStack a = new ArrayStack(5); + boolean flag = true;// 用于判断循环结束的标志 + Scanner sc = new Scanner(System.in); + String key = "";// 用于接受菜单的选项 + while (flag) { + System.out.println("show:显示栈"); + System.out.println("exit:退出程序"); + System.out.println("push:进栈"); + System.out.println("pop:出栈"); + key = sc.nextLine(); + switch (key) { + case "show": + a.show(); + break; + case "exit": + flag = false; + System.out.println("程序结束!"); + break; + + case "push": + + System.out.println("请输入要进栈的数据:"); + int val = sc.nextInt(); + a.push(val); + + break; + case "pop": + try { + int pop = a.pop(); + System.out.println("出栈的值是:" + pop); + } catch (Exception e) { + // TODO: handle exception + System.out.println(e.getMessage()); + } + break; + default: + break; + } + + } + + } +} + +class ArrayStack { + private int MaxSize;// 定义数组的最大长度 + private int[] arr;// 定义数组,数据就放在该数组 + private int top = -1;// 定义栈顶,初始化数据为-1 + + public ArrayStack(int maxSize) { + this.MaxSize = maxSize; + arr = new int[MaxSize]; + } + + // 判断数组是否为空 + public boolean isEmpty() { + + return top == -1; + } + + // 判断数组是否满了 + public boolean isFull() { + System.out.println("栈顶:" + top + "最大长度:" + MaxSize); + return top == MaxSize - 1; + } + + // 进栈 + public void push(int val) { + // 先判断栈是否满了,满了就不能添加进去 + if (isFull()) { + System.out.println("栈已经满了~~"); + return; + } + top++; + arr[top] = val; + } + + // 出栈 + public int pop() { + // 先判断栈是否为空 + if (isEmpty()) { + throw new RuntimeException("栈为空,无法出栈!"); + } + int val = arr[top]; + top--; + return val; + } + + public void show() { + if (isEmpty()) { + System.out.println("没有数据"); + return; + } + for (int i = top; i >= 0; i--) { + System.out.print(arr[i] + "\t"); + } + System.out.println(); + } + +} \ No newline at end of file diff --git "a/\346\240\210\344\270\216\351\230\237\345\210\227/LinkedQueue.java" "b/\346\240\210\344\270\216\351\230\237\345\210\227/LinkedQueue.java" new file mode 100644 index 0000000000000000000000000000000000000000..83400a4eee1d0e30ddea3d73c1333009991d6c7e --- /dev/null +++ "b/\346\240\210\344\270\216\351\230\237\345\210\227/LinkedQueue.java" @@ -0,0 +1,90 @@ + +public class LinkedQueue { + + Node front;//队头指针,指向队头节点 + Node rail;//队尾指针,指向队尾节点 + + int size = 0;//记录队列长度 + + + //构造函数 + public LinkedQueue() { + front = rail = null; + } + + public boolean isEmpty() { + return size == 0 ? true : false; + } + + //添加元素 + public boolean addQueue(Object ele) { + if (size == 0) { + front = new Node(null, ele); + rail = front; + size++; + return true; + } + Node s = new Node(null, ele); + rail = s; + size++; + return true; + } + + + /** + * 删除元素,出队列 + * @return + */ + public boolean deleteQueue() { + if (isEmpty()) { + System.out.println("当前队列为空"); + return false; + } + + front = front.next; + size--; + return true; + + } + + + public static void main(String[] args) { + LinkedQueue queue = new LinkedQueue(); + + queue.addQueue(1); + queue.addQueue(2); + queue.addQueue(3); + queue.deleteQueue(); + + } + + +} + +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; + } + +} diff --git "a/\346\240\210\344\270\216\351\230\237\345\210\227/ListStack.java" "b/\346\240\210\344\270\216\351\230\237\345\210\227/ListStack.java" new file mode 100644 index 0000000000000000000000000000000000000000..178a4beb517fa0c37b7e3ce212b25c28d4641a4a --- /dev/null +++ "b/\346\240\210\344\270\216\351\230\237\345\210\227/ListStack.java" @@ -0,0 +1,108 @@ + +public class Node { + + Object element; + Node next; + + public Node(Object element){ + this(element,null); + } + + + public Node(Object element,Node n){ + this.element=element; + next=n; + } + + public Object getElement() { + return element; + } + +} +public class ListStack { + + Node header;//栈顶元素 + int elementCount;//栈内元素个数 + int size;//栈的大小 + + /** + * 构造函数,构造一个空的栈 + */ + public ListStack(){ + header=null; + elementCount=0; + size=0; + } + + /** + * 通过构造器自定义栈的大小 + * @param size + */ + public ListStack(int size) { + header=null; + elementCount=0; + this.size=size; + } + + public void setHeader(Node header) { + this.header=header; + } + + public boolean isFull() { + if (elementCount==size) { + return true; + } + + return false; + } + + public boolean isEmpty() { + if (elementCount==0) { + return true; + } + + return false; + } + + /** + * 入栈 + * @param value + */ + public void push(Object value) { + if (this.isFull()) { + throw new RuntimeException("Stack is Full"); + } + header=new Node(value, header); + elementCount++; + } + + /** + * 出栈 + * @return + */ + public Object pop() { + if (this.isEmpty()) { + throw new RuntimeException("Stack is empty"); + } + Object object=header.getElement(); + + header=header.next; + + elementCount--; + + return object; + } + + /** + * 返回栈顶元素 + */ + public Object peak(){ + + if (this.isEmpty()) { + throw new RuntimeException("Stack is empty"); + } + + return header.getElement(); + } + +} \ No newline at end of file diff --git "a/\346\240\210\344\270\216\351\230\237\345\210\227/Queue.java" "b/\346\240\210\344\270\216\351\230\237\345\210\227/Queue.java" new file mode 100644 index 0000000000000000000000000000000000000000..11d4c76df0f32634a5d5f40453fa301ffb079b6c --- /dev/null +++ "b/\346\240\210\344\270\216\351\230\237\345\210\227/Queue.java" @@ -0,0 +1,60 @@ +class Queue { + private int size;//队列的长度 + private int[] queue; //队列 + private int front; //后指针 + private int rear; //前指针 + private static final int DEFALUT_SIZE = 10; + + public ArrayQueue() { + this.size = DEFALUT_SIZE; + } + + public ArrayQueue(int queueSize) { + if (queueSize <= 0 ) { + size = DEFALUT_SIZE; + queue = new int[DEFALUT_SIZE]; + } else { + size = queueSize; + queue = new int[queueSize]; + } + front = -1; + rear = -1; + } + + public boolean isFull() { + return rear == size - 1; + } + + public boolean isEmpty() { + return rear == front; + } + + public void add(int n) { + if (isFull()) { + System.out.println("队列已满,不能再添加数据"); + return; + } + queue[++rear] = n; + } + + public int get() { + if (isEmpty()) { + throw new RuntimeException("队列已空,没有数据了"); + } + return queue[++front]; + } + + public int peek() { + if (isEmpty()) { + throw new RuntimeException("队列已空,没有头元素了"); + } + return queue[front + 1]; + } + + public void list() { + for (int i : queue) { + System.out.printf("%d\t", i); + } + System.out.println(); + } +} \ No newline at end of file