From c24e1870f37d71f074583b93e8fc599e956972c0 Mon Sep 17 00:00:00 2001 From: unknown <952966468@qq.com> Date: Tue, 17 Nov 2020 22:41:03 +0800 Subject: [PATCH] yan3 --- ...7\346\234\254\346\226\207\346\241\243.txt" | 324 ++++++++++++++++++ 1 file changed, 324 insertions(+) create mode 100644 "\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" diff --git "a/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" new file mode 100644 index 0000000..e45c043 --- /dev/null +++ "b/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" @@ -0,0 +1,324 @@ +1.数组实现栈 +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; + + 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(); + } + +} +2.尾指针链表实现栈 +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; + } + + 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; + } + + public void push(Object value) { + if (this.isFull()) { + throw new RuntimeException("Stack is Full"); + } + header=new Node(value, header); + elementCount++; + } + + 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(); + } + +} +3.链表实现队列 +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.setNext(s); + rail = s; + size++; + return true; + } + + + 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; + } + +} +4.数组实现队列 +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; +} +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++]; +} +} \ No newline at end of file -- Gitee