diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..a571cdd90950bcf2eb8d3103c79a38feddee5ee7 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../../:\JAVA\数据结构\lec03-stack-queue\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..1763e153b6f24f136d4d5567320e74f68b248b87 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..d799b010b372da3effd2cb0050b812b5648951c3 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Lianbiaoduilie.java b/Lianbiaoduilie.java new file mode 100644 index 0000000000000000000000000000000000000000..ee4bd294194bbc3413e3b7abfe4f7db4fdfae148 --- /dev/null +++ b/Lianbiaoduilie.java @@ -0,0 +1,50 @@ +public class Lianbiaoduilie { + private MYLinkedList myLinkedList = new MYLinkedList<>(); + private int size; + + //插入 + public void add(E item) { + myLinkedList.addToRear(item); + size++; + } + + + //移除头部元素 + public E remove() { + if (isEmpty()) { + System.out.println("无元素"); + return null; + } + try { + E e = myLinkedList.delTopNode(); + size--; + return e; + } catch (Exception e) { + System.out.println("移除失败"); + } + return null; + + } + + // 获取头部元素 + public E peek() { + if (isEmpty()) { + System.out.println("无元素"); + return null; + } + E e = myLinkedList.inquiryTopNode(); + return e; + } + + public int size() { + return size; + } + + public boolean isEmpty() { + if (size == 0) { + return true; + } + return false; + } + +} diff --git a/Lianbiaozan.java b/Lianbiaozan.java new file mode 100644 index 0000000000000000000000000000000000000000..6499ec2f3966f6beef49a57373e9d434253966b5 --- /dev/null +++ b/Lianbiaozan.java @@ -0,0 +1,48 @@ +public class Lianbiaozan { + //栈顶即为链表头部元素 + + private MYLinkedList myLinkedList = new MYLinkedList<>(); + private int size=0; + + // 入栈(插入到链表头部) + public void push(E value) { + if (value==null){//输入值为空 + System.out.println("插入失败:元素为空"); + return; + } + myLinkedList.addToFront(value); + size++; + } + + // 出栈(从链表头部取出) + public E pop() { + if(isEmpty()){ + return null; + } + E e = myLinkedList.delTopNode(); + size--; + return e; + } + + //查看栈顶元素 + public E peek() { + if(isEmpty()){ + return null; + } + E e = myLinkedList.inquiryTopNode();//链表头元素赋值 + return e; + } + + public int size() { + return this.size; + }//获取长度 + + public boolean isEmpty() { + if (this.size == 0) { + return true; + } + return false; + } + + +} diff --git a/MYLinkedList.java b/MYLinkedList.java new file mode 100644 index 0000000000000000000000000000000000000000..cb7d50bfba3d718183cb47c93136bc655ea8554d --- /dev/null +++ b/MYLinkedList.java @@ -0,0 +1,54 @@ +public class MYLinkedList { + private Node head, tail; + private int size = 0; + + public int getSize() { + return size; + } + + public void addToFront(E value) { + Node node = new Node(value); + if (head == null) { + head = node;//判断头节点 + tail = head; + } else { + node.next = head; + head = node; + } + size++; + } + + public void addToRear(E value) { + Node node = new Node(value); + if (tail == null) { + tail = node; + head = tail;//判断尾节点 + } else { + tail.next = node; + tail = node; + } + size++; + } + + + //删除头部节点 + public E delTopNode() { + if (head == null) { + return null; + } + E value = (E) head.value; + head = head.next;//删除头节点 + size--; + return value; + } + + //查看头部节点 + public E inquiryTopNode() { + if (head == null) { + return null; + } + return (E) head.value; + } + + +} diff --git a/Main.java b/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..37e3faf4987715b763885389588f66f217662d1f --- /dev/null +++ b/Main.java @@ -0,0 +1,70 @@ +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + System.out.println("数组实现栈"); + Shuzuzhan Shuzuzhan = new Shuzuzhan(3); + Shuzuzhan.push("1"); + Shuzuzhan.push("2"); + Shuzuzhan.push(null); + Shuzuzhan.push("3"); + Shuzuzhan.push("4"); + System.out.println("查看栈顶元素" + Shuzuzhan.peek()); + System.out.println("出栈" + Shuzuzhan.pop()); + System.out.println("查看栈顶元素" + Shuzuzhan.peek()); + System.out.println("栈内元素个数:" + Shuzuzhan.size()); + System.out.println("栈是是否为空:" + Shuzuzhan.isEmpty()); + + System.out.println("------------------------------------"); + System.out.println("------------------------------------"); + + System.out.println("带尾指针的链表实现栈"); + Lianbiaozan Lianbiaozan = new Lianbiaozan<>(); + Lianbiaozan.push("1"); + Lianbiaozan.push("2"); + Lianbiaozan.push(null); + Lianbiaozan.push("3"); + System.out.println("查看栈顶元素" + Lianbiaozan.peek()); + System.out.println("出栈" + Lianbiaozan.pop()); + System.out.println("查看栈顶元素" + Lianbiaozan.peek()); + System.out.println("栈内元素个数:" + Lianbiaozan.size()); + System.out.println("栈是否为空" + Lianbiaozan.isEmpty()); + + System.out.println("------------------------------------"); + System.out.println("------------------------------------"); + + + + System.out.println("链表实现队列"); + Lianbiaoduilie Lianbiaoduilie = new Lianbiaoduilie<>(); + Lianbiaoduilie.add("1"); + Lianbiaoduilie.add("2"); + Lianbiaoduilie.add("3"); + Lianbiaoduilie.add("4"); + System.out.println("查看队列头部元素" + Lianbiaoduilie.peek()); + System.out.println("移除头部元素" + Lianbiaoduilie.remove()); + System.out.println("查看队列头部元素" + Lianbiaoduilie.peek()); + System.out.println("队列元素个数:" + Lianbiaoduilie.size()); + System.out.println("队列是否为空" + Lianbiaoduilie.isEmpty()); + + System.out.println("------------------------------------"); + System.out.println("------------------------------------"); + + System.out.println("数组实现队列"); + Shuzuduilie Shuzuduilie = new Shuzuduilie<>(4); + Shuzuduilie.add("1"); + Shuzuduilie.add("2"); + Shuzuduilie.add("null"); + Shuzuduilie.add("4"); + System.out.println("查看队列头部元素" + Shuzuduilie.peek()); + System.out.println("移除头部元素" + Shuzuduilie.remove()); + System.out.println("查看队列头部元素" + Shuzuduilie.peek()); + System.out.println("队列元素个数:" + Shuzuduilie.size()); + System.out.println("队列是否为空" + Shuzuduilie.isEmpty()); + + + + + + } +} diff --git a/Node.java b/Node.java new file mode 100644 index 0000000000000000000000000000000000000000..72661db5fcddb5014074d689a9cadbef39bb20b8 --- /dev/null +++ b/Node.java @@ -0,0 +1,8 @@ +public class Node { + E value; + Node next = null; + + Node(E value) { + this.value = value; + } +} diff --git a/Shuzuduilie.java b/Shuzuduilie.java new file mode 100644 index 0000000000000000000000000000000000000000..db171414172b8b29e5e5e434f9be229ced944b6c --- /dev/null +++ b/Shuzuduilie.java @@ -0,0 +1,65 @@ +import java.util.LinkedHashMap; + +public class Shuzuduilie { + private Object[] array; + private int rear = 0; + Shuzuduilie(int length) { + array = new Object[length]; + } //定义长度 + + //插入 + public void add(T item) { + if (item == null) throw new Error("插入失败:元素不能为空"); + + try { + array[rear] = item; + rear++;//成功后自增 + } catch (Exception e) { + System.out.println("插入失败,队列已满"); + } + } + + //移除头部元素 + public T remove() { + if (isEmpty()) { + System.out.println("无元素"); + return null; + } + try { + int i = 0; + T t = (T) array[0]; + for (int j = 0; j < rear - 1; j++) { + array[j] = array[j + 1]; + i++; + } + rear = i; + return t; + } catch (Exception e) { + System.out.println("移除失败"); + } + return null; + + } + + // 获取头部元素 + public T peek() { + if (isEmpty()) { + System.out.println("无元素"); + return null; + } + T t = (T) array[0]; + return t; + } + + public int size() { + return rear; + } + + public boolean isEmpty() { + if (rear == 0) { + return true; + } + return false; + } + +} diff --git a/Shuzuzhan.java b/Shuzuzhan.java new file mode 100644 index 0000000000000000000000000000000000000000..ab9b72d9103913c4a0f810f5b74c0443c9629168 --- /dev/null +++ b/Shuzuzhan.java @@ -0,0 +1,70 @@ +import java.lang.reflect.Array; +import java.util.Arrays; + +public class Shuzuzhan { + private Object[] pen; + private int topPointer;//栈顶指针 + + // 数组现实的栈 + public Shuzuzhan(int length) { + pen = new Object[length]; + } + + // 入栈 + public void push(T t) { + if (t == null) { + System.out.println("插入失败:元素不能为空"); + return; + } + try { + pen[topPointer] = t;//输入得数组值赋值 + ++topPointer;//成功后自增 + } catch (Exception e) {//由于设定了数组长度会出现溢出异常错误 会执行catch + System.out.println("插入失败,栈已满"); + } + } + + // 移除栈顶元素并返回 + //检查栈是否为空并返回空值 + public T pop() { + if (isEmpty()) { + return null; + } + try {//数组栈顶值最后一个进去得值拿出去即t + T t = (T) pen[topPointer - 1]; + pen[topPointer - 1] = null;//取出元素后该位置为空 + topPointer--;//成功长度-1 + return t;//返回t + } catch (Exception e) {//z栈内没有输出局后出异常就执行catch + System.out.println("出栈失败,栈已空"); + } + return null; + } + + //获取栈顶元素 + public T peek() { + if (isEmpty()) { + return null; + } + try {// 栈顶为3 但是数组从0开始终点2 + return (T) pen[topPointer - 1]; + } catch (Exception e) { + System.out.println("查看失败,栈已空"); + } + return null; + } + + public int size() { + return topPointer; + } + //size返回栈内元素个数 + + public boolean isEmpty() {//检查栈内是否为空 + if (topPointer == 0) { + return true; + } + return false; + } + + +} diff --git a/lec03-stack-queue.iml b/lec03-stack-queue.iml new file mode 100644 index 0000000000000000000000000000000000000000..b107a2dd81165eaaf682ad3da030668b937fbb6c --- /dev/null +++ b/lec03-stack-queue.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file