diff --git a/Queue/Linkedlist_Queue.java b/Queue/Linkedlist_Queue.java new file mode 100644 index 0000000000000000000000000000000000000000..d8d19daec20b04cb4ce33adb7fb44b994ca97560 --- /dev/null +++ b/Queue/Linkedlist_Queue.java @@ -0,0 +1,28 @@ +package Queue; + +import java.util.LinkedList; + +public class Linkedlist_Queue{ + public static void main(String[] args) { + Linkque que = new Linkque(); + for (int i = 0; i < 10 ; i++){ + que.put(Integer.toString(i)); + } + while (!que.isEmpty()){ + System.out.println(que.get()); + } + } +} + +class Linkque{ + private LinkedList list = new LinkedList(); + public void put(Object v){ + list.addFirst(v); + } + public Object get(){ + return list.removeLast(); + } + public boolean isEmpty(){ + return list.isEmpty(); + } +} diff --git a/Queue/Queue.java b/Queue/Queue.java new file mode 100644 index 0000000000000000000000000000000000000000..2366ab3fcc62244df2efbd04b67d17cd7aa05d0e --- /dev/null +++ b/Queue/Queue.java @@ -0,0 +1,62 @@ +package Queue; + +public class Queue { + private int[] data;//队列中存放的数据 + private int maxsize; //队列的大小 + private int top; //指向队列头部的指针 + private int rear; + + public Queue(int maxsize){ + this.maxsize = maxsize; + data = new int[maxsize]; + this.top = -1; + } + + // 判断队列是否已满 + public boolean isFull(){ + return rear == maxsize -1 ; + } + + //判断队列是否为空 + public boolean isEmpty(){ + return rear == top; + } + + //添加数据到队列 + public void add(int n){ + if (isFull()){ + System.out.println("队列已满"); + return; + } + data[++rear] = n; + } + + //显示头部数据 + public void head(){ + if (isEmpty()){ + throw new RuntimeException("队列为空"); + } + System.out.println(data[top+1]); + } + + //取出头部数据 + public int pop(){ + if (isEmpty()){ + throw new RuntimeException("队列为空"); + } + int a = data[++top]; + data[top] = 0; + return a; + } + + //打印全部数据 + public void print(){ + if (isEmpty()){ + System.out.println("队列为空"); + return; + } + for (int i=0;i { + private T[] data; + private int maxsize; + private int top; + + //初始化栈 + public ArrayStack(int maxsize){ + this.maxsize=maxsize; + data=(T[])new Object[maxsize]; + this.top=-1; + } + + //判断栈是否为空 + public boolean isEmpty(){ + return (top==maxsize-1); + } + + //判断栈是否已经满了 + public boolean isFull(){ + return (top==maxsize-1); + } + + //压栈 + public boolean push(T value){ + if (isFull()){ + return false; + } + top++; + data[top]=value; + return true; + } + + //取出栈顶元素 + public T pop(){ + if (isEmpty()){ + return null; + } + T temp = data[top]; + data[top]=null; + top--; + return temp; + } + + //返回栈顶元素不出栈 + public T peek(){ + if (isEmpty()){ + return null; + } + return data[top]; + } +} diff --git a/Stack/NodeStack.java b/Stack/NodeStack.java new file mode 100644 index 0000000000000000000000000000000000000000..6132dc151557587b05449b12d258f1f23127f3f1 --- /dev/null +++ b/Stack/NodeStack.java @@ -0,0 +1,81 @@ +package Stack; + +public class NodeStack { + private Nodetop=null; //栈顶 + public NodeStack(){ + this.top = null; + } + + //判断栈是否为空 + public boolean isEmpty(){ + if (top!=null){ + return false; + } + return true; + } + + //压栈 + public boolean push(T value){ + Node node = new Node(value); + node.setNext(top); + top = node; + return true; + } + + //出栈 + public T pop(){ + if (top==null){ + return null; + } + T t = top.data; + top = top.getNext(); + return t; + } + + //取出栈顶的值 + public T peek(){ + if (isEmpty()){ + return null; + } + return top.data; + } + + + class Node{ + private T data; //数据 + private Node next; //指向下一个结点的指针 + //初始化链表 + public Node(T data){ + this.data = data; + } + //获取下一个结点 + public Node getNext(){ + return this.next; + } + //设置下一个结点 + public void setNext(Node n){ + this.next=n; + } + //获取结点数据 + public T getData(){ + return this.data; + } + //设置结点数据 + public void setData(T d){ + this.data = d; + } + } + + //测试 + public static void main(String[] args) { + NodeStack lstack = new NodeStack(); + System.out.println( lstack.isEmpty()); + lstack.push("1"); + lstack.push("dasdasd"); + + while (!lstack.isEmpty()){ + System.out.println(lstack.pop()); + } + } +} + diff --git a/Stack/test.java b/Stack/test.java new file mode 100644 index 0000000000000000000000000000000000000000..2f4d01c9d42fe86aa99cfe0faa456e7d05e40514 --- /dev/null +++ b/Stack/test.java @@ -0,0 +1,16 @@ +package Stack; + +public class test { + public static void main(String[] args) { + ArrayStack as=new ArrayStack(4); + as.push("anhui"); + as.push("shanghai"); + + + //测试栈已经满了的情况 + System.out.println(as.push("aa")); + for(int i=0;i<4;i++) { + System.out.println(as.pop()); + } + } +}