diff --git "a/\344\275\277\347\224\250\345\270\246\345\260\276\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250\345\256\236\347\216\260\351\230\237\345\210\227.txt" "b/\344\275\277\347\224\250\345\270\246\345\260\276\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250\345\256\236\347\216\260\351\230\237\345\210\227.txt" new file mode 100644 index 0000000000000000000000000000000000000000..23108e6b7afe59d77193f29f7ed6574377ea9ad9 --- /dev/null +++ "b/\344\275\277\347\224\250\345\270\246\345\260\276\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250\345\256\236\347\216\260\351\230\237\345\210\227.txt" @@ -0,0 +1,115 @@ +package LinkedList.LinkedListQueue; + +/** + * @ Description: 使用链表实现队列 + * @ Date: Created in 14:04 12/07/2018 + * @ Author: Anthony_Duan + */ +public class LinkedListQueue implements Queue { + + private class Node { + public E e; + public Node next; + + public Node(E e, Node next) { + this.e = e; + this.next = next; + } + + public Node(E e) { + this(e, null); + } + + public Node() { + this(null, null); + } + + @Override + public String toString() { + return e.toString(); + } + } + + private Node head, tail; + private int size; + + public LinkedListQueue() { + head = null; + tail = null; + size = 0; + } + + @Override + public int getSize() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public void enqueue(E e) { + if (tail == null) { + tail = new Node(e); + head = tail; + } else { + tail.next = new Node(e); + tail = tail.next; + } + size++; + } + + + @Override + public E dequeue() { + if (isEmpty()) { + throw new IllegalArgumentException("Cannot dequeue from an empty queue."); + } + Node retNode = head; + head = head.next; + retNode.next = null; + if (head == null) { + tail = null; + } + size--; + return retNode.e; + } + + @Override + public E getFront() { + if (isEmpty()) { + throw new IllegalArgumentException("Queue is empty."); + } + return head.e; + } + + @Override + public String toString() { + StringBuilder res = new StringBuilder(); + res.append("Queue: front "); + + Node cur = head; + while (cur != null) { + res.append(cur + "->"); + cur = cur.next; + } + res.append("NULL tail"); + return res.toString(); + } + + + public static void main(String[] args) { + LinkedListQueue queue = new LinkedListQueue(); + for (int i = 0; i < 10; i++) { + queue.enqueue(i); + System.out.println(queue); + + if (i % 3 == 2) { + queue.dequeue(); + System.out.println(queue); + } + } + } +} \ No newline at end of file diff --git "a/\344\275\277\347\224\250\346\225\260\347\273\204\345\256\236\347\216\260\351\230\237\345\210\227.txt" "b/\344\275\277\347\224\250\346\225\260\347\273\204\345\256\236\347\216\260\351\230\237\345\210\227.txt" new file mode 100644 index 0000000000000000000000000000000000000000..852181753f1eb9bef1326f9af3c8ef72a52a0eab --- /dev/null +++ "b/\344\275\277\347\224\250\346\225\260\347\273\204\345\256\236\347\216\260\351\230\237\345\210\227.txt" @@ -0,0 +1,76 @@ +package ch04; +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 diff --git "a/\347\224\250\346\225\260\347\273\204\345\256\236\347\216\260\346\240\210.txt" "b/\347\224\250\346\225\260\347\273\204\345\256\236\347\216\260\346\240\210.txt" new file mode 100644 index 0000000000000000000000000000000000000000..87a4602f5cfc47c29e1207e6cfcce43e0f83c659 --- /dev/null +++ "b/\347\224\250\346\225\260\347\273\204\345\256\236\347\216\260\346\240\210.txt" @@ -0,0 +1,53 @@ +package stack01; +import java.util.Arrays; +import java.util.EmptyStackException; +public class StackExer { + class ArrayStack{ + private String[] data=new String[10];//存储数据 + private int size;//记录个数 +//把item压入堆栈顶部 + public void push(String str) { + //判断是否需要扩容 + if (size>data.length) { + data=Arrays.copyOf(data, data.length*2); + } + data[size++]=str; + } + //查看堆栈顶部的对象,但不从堆栈中移除它 + public String peek() { +//判断栈是否为空 + if (size==0) { + throw new EmptyStackException(); + } + return data[size-1]; //获取栈顶元素 + } + //移除堆栈顶部的对象,并作为此函数的值返回该对象 + public String pop() { + String str=this.peek();//获取栈顶元素 + size--; //减少元素个数 + return str; + } + //测试堆栈是否为空 + public boolean empty() { + return size==0; + } + + + //返回对象在堆栈中的位置,以 1 为基数 + + public int research(String str) { + + //顺着放倒着拿(FILO/LIFO) +for (int i = size-1; i >=0; i--) +{ + if (str==data[i]||str!=null&&data[i].equals(str)) { + return size-i; } + } + return -1; //返回栈中不存在该元素 + } + + } + + + +} \ No newline at end of file diff --git "a/\347\224\250\351\223\276\350\241\250\345\256\236\347\216\260\351\230\237\345\210\227.txt" "b/\347\224\250\351\223\276\350\241\250\345\256\236\347\216\260\351\230\237\345\210\227.txt" new file mode 100644 index 0000000000000000000000000000000000000000..a18e392c683626538331548834ab60eb7187e02e --- /dev/null +++ "b/\347\224\250\351\223\276\350\241\250\345\256\236\347\216\260\351\230\237\345\210\227.txt" @@ -0,0 +1,66 @@ +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