diff --git "a/\350\257\276\345\220\216\344\275\234\344\270\232/ArrayQueue.class" "b/\350\257\276\345\220\216\344\275\234\344\270\232/ArrayQueue.class" new file mode 100644 index 0000000000000000000000000000000000000000..d54280d8ac513ae6a17e029c482d308af2745b3d Binary files /dev/null and "b/\350\257\276\345\220\216\344\275\234\344\270\232/ArrayQueue.class" differ diff --git "a/\350\257\276\345\220\216\344\275\234\344\270\232/ArrayStack.class" "b/\350\257\276\345\220\216\344\275\234\344\270\232/ArrayStack.class" new file mode 100644 index 0000000000000000000000000000000000000000..a5357d559bb6583ec25cb1966668b801f353af90 Binary files /dev/null and "b/\350\257\276\345\220\216\344\275\234\344\270\232/ArrayStack.class" differ diff --git "a/\350\257\276\345\220\216\344\275\234\344\270\232/ArrayStack.java" "b/\350\257\276\345\220\216\344\275\234\344\270\232/ArrayStack.java" new file mode 100644 index 0000000000000000000000000000000000000000..22e5ca554121e4af6463640d2a7fe44757852dcc --- /dev/null +++ "b/\350\257\276\345\220\216\344\275\234\344\270\232/ArrayStack.java" @@ -0,0 +1,57 @@ +//用数组实现栈 +public class ArrayStack { + private int top; + private int capacity; + private Object [] array; + public ArrayStack(){ + top=-1;//栈顶指针为-1 + capacity=5;//初始化容量的大小 + array=new Object[capacity];//初始化数组 + } + + public boolean isEmpty(){//判断是否为空 + return top==-1; + } + + public boolean isFull(){//判断是否是满栈的 + return top==capacity-1; + } + + public void push(Object data){//入栈操作 + if(isFull()){ + System.out.println("栈已经满,插入失败"); + return; + } + array[++top]=data; + } + + public Object pop(){//返回栈顶元素,并且删除 + Object data=0; + if(isEmpty()){ + System.out.println("栈为空,返回-1"); + data=-1; + } + data=array[top--]; + return data; + } + + public Object top(){//返回栈顶元素,但是不删除 + return array[top]; + } + + public int size(){//返回栈顶元素,但是不删除 + return top; + } + + public void showAll(){//显示栈中所有的值 + for(int i=top;i>=0;i--){ + System.out.print(" "+array[i]); + } + } + public static void main(String[] args) { + ArrayStack stack=new ArrayStack(); + stack.push(1); + stack.push(2); + stack.showAll(); + } +} \ No newline at end of file diff --git "a/\350\257\276\345\220\216\344\275\234\344\270\232/LinkedStack.class" "b/\350\257\276\345\220\216\344\275\234\344\270\232/LinkedStack.class" new file mode 100644 index 0000000000000000000000000000000000000000..d56e8060a2fea5da789c0217f29f595fd9825ba7 Binary files /dev/null and "b/\350\257\276\345\220\216\344\275\234\344\270\232/LinkedStack.class" differ diff --git "a/\350\257\276\345\220\216\344\275\234\344\270\232/LinkedStack.java" "b/\350\257\276\345\220\216\344\275\234\344\270\232/LinkedStack.java" new file mode 100644 index 0000000000000000000000000000000000000000..83b4e8a6db8e894c8e971e4db8527cf556490df6 --- /dev/null +++ "b/\350\257\276\345\220\216\344\275\234\344\270\232/LinkedStack.java" @@ -0,0 +1,59 @@ +class Node { + int value; + Node next = null; + Node(int value) { + this.value = value; + } +} +public class LinkedStack { + private Node head = null;//链表 + //进栈 + public void push(int element) { + if(head == null) { + head = new Node(element); + }else{ + Node last = getLast(head); + last.next = new Node(element); + last.next.next = null; + } + } + + private Node getLast(Node node) { + if(node == null) { + return null; + } + Node last = head; + while(node.next != null) { + last = last.next; + } + return last; + } + + //出栈 + public int pop() { + if(head == null) { + return -1; + } + Node last = getLast(head); + if(head.next == null) { + head = null; + return last.value; + } + + Node lastlast = getLastLast(head); + + lastlast.next = null; + return last.value; + } + + private Node getLastLast(Node head) { + if(head == null || head.next == null) { + return null; + } + Node lastLast = head; + while (lastLast.next.next != null) { + lastLast = lastLast.next; + } + return lastLast; + } +} diff --git "a/\350\257\276\345\220\216\344\275\234\344\270\232/Node.class" "b/\350\257\276\345\220\216\344\275\234\344\270\232/Node.class" new file mode 100644 index 0000000000000000000000000000000000000000..56a7f823c0ffbc00aa993d132f9729f2230ef422 Binary files /dev/null and "b/\350\257\276\345\220\216\344\275\234\344\270\232/Node.class" differ diff --git "a/\350\257\276\345\220\216\344\275\234\344\270\232/QueueLinked.class" "b/\350\257\276\345\220\216\344\275\234\344\270\232/QueueLinked.class" new file mode 100644 index 0000000000000000000000000000000000000000..688aa20203ba9b4682da0ac871b58a04e1f66878 Binary files /dev/null and "b/\350\257\276\345\220\216\344\275\234\344\270\232/QueueLinked.class" differ diff --git "a/\350\257\276\345\220\216\344\275\234\344\270\232/QueueLinked.java" "b/\350\257\276\345\220\216\344\275\234\344\270\232/QueueLinked.java" new file mode 100644 index 0000000000000000000000000000000000000000..23cbb4239e1dbcba8fb11f7b82f79e650b186914 --- /dev/null +++ "b/\350\257\276\345\220\216\344\275\234\344\270\232/QueueLinked.java" @@ -0,0 +1,67 @@ +//用链表实现队列 +class QueueNode { + Object data; // 节点存储的数据 + QueueNode next; // 指向下个节点的指针 + + public QueueNode() { + this(null, null); + } + + public QueueNode(Object data) { + this(data, null); + } + + public QueueNode(Object data, QueueNode next) { + this.data = data; + this.next = next; + } +} + +public class QueueLinked { + QueueNode front; // 队首指针 + QueueNode rear; // 队尾指针 + + public QueueLinked() { + this.rear = null; + this.front = null; + } //将一个对象追加到队列的尾部 + public void enqueue(Object obj) { + //如果队列是空的 + if (rear == null && front == null) { + rear = new QueueNode(obj); + front = rear; + } else { + QueueNode node = new QueueNode(obj); + rear.next = node; + rear = rear.next; + } + } + //队首对象出队 + //return 出队的对象,队列空时返回null + public Object dequeue() { + //如果队列空 + if (front == null) { + return null; + } + //如果队列中只剩下一个对象 + if (front == rear && rear != null) { + QueueNode node = front; + rear = null; + front = null; + return node.data; + } + Object obj = front.data; + front = front.next; + return obj; + } + public static void main(String[] args) { + QueueLinked q = new QueueLinked(); + q.enqueue("张三"); + q.enqueue("李斯"); + q.enqueue("赵五"); + q.enqueue("王一"); + for (int i = 0; i < 4; i++) { + System.out.println(q.dequeue()); + } + } +} \ No newline at end of file diff --git "a/\350\257\276\345\220\216\344\275\234\344\270\232/QueueNode.class" "b/\350\257\276\345\220\216\344\275\234\344\270\232/QueueNode.class" new file mode 100644 index 0000000000000000000000000000000000000000..ce63324a4e06f9433f0f623068b9514cb0e3aef2 Binary files /dev/null and "b/\350\257\276\345\220\216\344\275\234\344\270\232/QueueNode.class" differ diff --git "a/\350\257\276\345\220\216\344\275\234\344\270\232/QueueTest.class" "b/\350\257\276\345\220\216\344\275\234\344\270\232/QueueTest.class" new file mode 100644 index 0000000000000000000000000000000000000000..017de1d2d3c75a0e2302ff77fba2f0aacddf03bf Binary files /dev/null and "b/\350\257\276\345\220\216\344\275\234\344\270\232/QueueTest.class" differ diff --git "a/\350\257\276\345\220\216\344\275\234\344\270\232/QueueTest.java" "b/\350\257\276\345\220\216\344\275\234\344\270\232/QueueTest.java" new file mode 100644 index 0000000000000000000000000000000000000000..a17d17f74aad5f1527928b7243554ae04525faee --- /dev/null +++ "b/\350\257\276\345\220\216\344\275\234\344\270\232/QueueTest.java" @@ -0,0 +1,76 @@ +//使用数组实现队列 +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; +} +/** +* 判断队列是否为空 +* +* @return +*/ +public boolean isEmpty() { +return front == arrInt.length; +} +/** +* 判断队列是否已满 +* +* @return +*/ +public boolean isFull() { +return arrInt.length - 1 == rear; +} +/** +* 向队列的队尾插入一个元素 +*/ +public void insert(int item) { +if (isFull()) { +throw new RuntimeException("队列已满"); +} +arrInt[++rear] = item; +} +/** +* 获得对头元素 +* +* @return +*/ +public int peekFront() { +return arrInt[front]; +} +/** +* 获得队尾元素 +* +* @return +*/ +public int peekRear() { +return arrInt[rear]; +} +/** +* 从队列的对头移除一个元素 +* +* @return +*/ +public int remove() { +if (isEmpty()) { +throw new RuntimeException("队列为空"); +} +return arrInt[front++]; +} +} \ No newline at end of file