diff --git a/ArrayQueue.class b/ArrayQueue.class new file mode 100644 index 0000000000000000000000000000000000000000..148d5dd5826f449005f7f9b9d8773d0f5e475d7b Binary files /dev/null and b/ArrayQueue.class differ diff --git a/ArrayQueue.java b/ArrayQueue.java new file mode 100644 index 0000000000000000000000000000000000000000..c61d0cee0a17c6a9885ff39465a13c85c25b9f99 --- /dev/null +++ b/ArrayQueue.java @@ -0,0 +1,60 @@ +class ArrayQueue { + 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 diff --git a/ArrayStack.class b/ArrayStack.class new file mode 100644 index 0000000000000000000000000000000000000000..73d8a656b378f18c4cb156deac8b3eecfbce8ab1 Binary files /dev/null and b/ArrayStack.class differ diff --git a/ArrayStack.java b/ArrayStack.java new file mode 100644 index 0000000000000000000000000000000000000000..153e354c5593178b27ca66427d2b2e287eab7187 --- /dev/null +++ b/ArrayStack.java @@ -0,0 +1,71 @@ +public class ArrayStack{ + private int[] a; + private int size; //栈的大小 + private int top;//栈顶 + + public ArrayStack(int maxSize){ + this.size = maxSize; + this.a = new int[size]; + this.top = -1; + } + + //入栈 + public void push(int value){ + if(isFull()){ + System.out.println("栈满"); + return; + } + a[++top] = value; + } + + //返回栈顶内容,但是不删除 + public void peek(){ + if(isEmpty()){ + System.out.println("栈中没有数据!"); + + } + System.out.println(a[top]); + } + + //弹出栈顶内容 + + public int pop(){ + if(isEmpty()){ + System.out.println("栈中没有数据!"); + return 0; + } + return a[top--]; + } + + + //size栈的大小 + + public int size(){ + return top +1; + } + + //是否为空 + + public boolean isEmpty(){ + return (top == -1); + } + + //是否满了 + + public boolean isFull(){ + return (top == size -1); + } + + //显示 + public void display(){ + System.out.print("["); + for (int i=top;i>=0; i--){ + System.out.print(a[i]); + if(i!=0){ + System.out.print(","); + } + } + System.out.println("]"); + } + +} \ No newline at end of file diff --git a/QueueLinked.class b/QueueLinked.class new file mode 100644 index 0000000000000000000000000000000000000000..638f74826d0bce0dd0f1dfef2926c79f76f6d186 Binary files /dev/null and b/QueueLinked.class differ diff --git a/QueueLinked.java b/QueueLinked.java new file mode 100644 index 0000000000000000000000000000000000000000..9f795ad039e51d237f00ca91e22a503dfdafa0ac --- /dev/null +++ b/QueueLinked.java @@ -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("a"); + q.enqueue("b"); + q.enqueue("c"); + q.enqueue("d"); + for (int i = 0; i < 4; i++) { + System.out.println(q.dequeue()); + } + } +} \ No newline at end of file diff --git a/QueueNode.class b/QueueNode.class new file mode 100644 index 0000000000000000000000000000000000000000..4cfa3c0e792bb1592d363ab88a6578ae1e70c158 Binary files /dev/null and b/QueueNode.class differ diff --git a/Stack.java b/Stack.java new file mode 100644 index 0000000000000000000000000000000000000000..a41aed2dd430fbad700856cf9e0e8b0c1eada266 --- /dev/null +++ b/Stack.java @@ -0,0 +1,42 @@ +public class Stack { + public int[] stack; + public int top; + private int size; + + Stack(int size){ + // 构建一个长度为size大小的空数组来模拟栈 + this.size = size; + stack = new int[size]; + top = 0; // top永远指向下一个可放入的位置 + } + + + public void push(int item){ + if(top >= size ){ + throw new Exception("StackOverflowError"); + } + + stack[top] = item; + top++; + } + + + public int pop(){ + if(top <=0 ){ + throw new Exception("StackEmpty"); + } + + return stack[top--]; + + } + + public int peek(){ + + if(top <=0 ){ + throw new Exception("StackEmpty"); + } + return stack[top-1]; + } +} + + diff --git a/StackTest.class b/StackTest.class new file mode 100644 index 0000000000000000000000000000000000000000..0f0a12c40ff2db87039365a5b6f018c9a42fce5b Binary files /dev/null and b/StackTest.class differ diff --git a/StackTest.java b/StackTest.java new file mode 100644 index 0000000000000000000000000000000000000000..521d134d1c98b762c3428c3d8a88865f407dd85f --- /dev/null +++ b/StackTest.java @@ -0,0 +1,18 @@ +import java.util.Stack; + +public class StackTest { + public static void main(String[] args) { + + ArrayStack arrayStack = new ArrayStack(5); + arrayStack.display(); + System.out.println(arrayStack.isEmpty()); + System.out.println(arrayStack.isFull()); + arrayStack.push(1); + arrayStack.push(2); + arrayStack.push(3); + arrayStack.display(); + arrayStack.peek(); + arrayStack.pop(); + arrayStack.display(); + } +}