栈与队列--java实现

栈:LIFO 后进先出

一,顺序存储结构

package Stack;

public class Stack<E> {
    private Object[] data = null;
    private int maxSize = 0;//栈容量
    private int top = -1;//栈顶指针

    Stack(){
        this.maxSize=10;
    }

    Stack(int initalSize){
        if(initalSize>0){
            this.maxSize = initalSize;
            data = new Object[initalSize];
            top = -1;
        }
        else{
            throw new RuntimeException("初始化大不能为零");
        }
    }

    //判空
    public boolean empty(){
        return top==-1 ? true:false;
    }

    //进栈
    public boolean push(E e){
        if(top == maxSize-1 ){
            throw new RuntimeException("栈已满,无法入栈");
        }
        else{
            top++;
            data[top] = e;
            return true;
        }
    }

    //查看栈顶元素
    public E peek(){
        if(top == -1 ){
            throw new RuntimeException("栈为空");
        }
        else{
            return (E) data[top];
        }
    }

    //弹出栈顶元素
    public E pop(){
        if(top == -1){
            throw new RuntimeException("栈为空");
        }
        else{
            return (E) data[top--];
        }
    }

    //返回对象在栈中的位置,以1为基数
    public int search(E e){
        int i = top;
        while (top != -1){
            if(peek() != e){
                top--;
            }
            else{
                break;
            }
        }
        int result = top+1;
        top = 1;
        return result;
    }
    //测试方法
    public static void main(String[] args) {
        Stack stack = new Stack(5);
        stack.push("a");
        stack.push("b");
        stack.push("c");
        stack.push("d");
        stack.push("e");
        System.out.println(stack.peek());
        stack.pop();
        System.out.println(stack.peek());
        System.out.println(stack.search("b"));
    }
}

二,链式存储结构

package Stack;

public class LinkStack<E> {
    private class Node<E>{
        E e;
        Node<E> next;
        public Node(){};
        public Node(E e,Node next){
            this.e = e;
            this.next = next;
        }
    }

    private Node<E> top;
    private int size=0;

    public LinkStack(){
        top = null;
    }

    //当前栈的大小
    public int length(){
        return size;
    }

    //判空
    public boolean empty(){
        if(size == 0){
            return true;
        }
        else{
            return false;
        }
    }

    //入栈:让top指向新的元素,新元素的next指向原来的栈顶元素
    public boolean push(E e){
        top = new Node(e,top);//第一次调用后,top为null,第二次调用时,top为上一元素
        size++;
        return true;
    }

    //查看栈顶元素
    public Node<E> peek(){
        if(empty()){
            throw new RuntimeException("栈为空");
        }
        else{
            return top;
        }
    }

    //出栈
    public Node<E> pop(){
        if(empty()){
            throw new RuntimeException("栈为空");
        }
        else{
            Node<E> value = top;
            top = top.next;
            value.next = null;
            size--;
            return value;
        }
    }
    //测试方法
    public static void main(String[] args) {
        LinkStack ls = new LinkStack();
        ls.push("a");
        ls.push("b");
        ls.push("c");
        ls.push("d");
        ls.push("e");

        System.out.println(ls.peek().e);
        ls.pop();
        System.out.println(ls.peek().next.e);
    }

}

三,LinkList实现栈结构

package Stack;

import java.util.LinkedList;

public class StackList<E> {
    private LinkedList<E> ll = new LinkedList<E>();

    //入栈
    public void push(E e){
        ll.push(e);
    }

    //查看栈顶元素
    public E peek(){
        return ll.getFirst();
    }

    //出栈
    public void pop(){
        ll.removeFirst();
    }

    //判空
    public boolean empty(){
        return ll.isEmpty();
    }

    //打印栈元素
    public String toString(){
        return ll.toString();
    }
    //测试方法
    public static void main(String[] args) {
        StackList sl = new StackList();
        sl.push("a");
        sl.push("b");
        sl.push("c");
        sl.push("d");
        sl.push("e");
        System.out.println(sl.peek());
        sl.pop();
        System.out.println(sl.peek());
        System.out.println(sl.toString());
    }

}

队列:FIFO 先进先出

一,顺序存储结构

package Queue;

public class Queue<E> {
    private Object[] data = null;
    private int size;//队列大小
    private int head;//队列头,允许删除
    private int foot;//队列尾,允许插入

    public Queue(){
        this.size=10;
    }

    public Queue(int initalSize){
        if(initalSize>=0){
            this.size = initalSize;
            data = new Object[initalSize];
            head = foot = 0;
        }
        else{
            throw new RuntimeException("初始化大小不能小于零");
        }
    }

    //判空
    public boolean empty(){
        return head==foot?true:false;
    }

    //插入
    public boolean add(E e){
        if(foot==size){
            throw new RuntimeException("队列已满");
        }
        else{
            data[foot++] = e;
            return true;
        }
    }

    //查看队首元素
    public E peek(){
        if(empty()){
            throw new RuntimeException("队列为空");
        }
        else{
            return (E) data[head];
        }
    }

    //出队
    public E poll(){
        if(empty()){
            throw new RuntimeException("队列为空");
        }
        else{
            E value = (E) data[head];
            data[head] = null;
            head++;
            return value;
        }
    }

    //队列长度
    public int length(){
        return foot - head;
    }
    //测试方法
    public static void main(String[] args) {
        Queue queue = new Queue(5);
        queue.add("a");
        queue.add("b");
        queue.add("c");
        queue.add("d");
        queue.add("e");

        System.out.println(queue.peek());
        System.out.println(queue.poll());
        System.out.println(queue.length());
    }
}

二,链式存储结构

package Queue;

import java.util.LinkedList;

public class LinkQueue<E> {
    private class Node<E> {
        E e;
        Node<E> next;

        public Node() {
        }
        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }
    }

    private Node head;
    private Node foot;
    private int size=0;

    public LinkQueue(){
        head = null;
        foot = null;
    }

    //判空
    public boolean empty(){
        return size==0?true:false;
    }

    //插入
    public boolean add(E e){
        if(empty()){
            head = new Node(e,null);//队列为空时,只有一个节点,head和foot都指向该节点
            foot = head;
        }
        else{
            Node<E> newNode = new Node<E>(e,null);
            foot.next = newNode;//让尾节点的next指向newNode
            foot = newNode;//newNode作为foot节点
        }
        size++;
        return true;
    }

    //查看队首元素
    public Node<E> peek(){
        if(empty()){
            throw new RuntimeException("队列为空");
        }
        else{
            return head;
        }
    }

    //出队
    public Node<E> poll(){
        if(empty()){
            throw new RuntimeException("对列为空");
        }
        else{
            Node<E> value = head;//得到队头元素
            head = head.next;//head指向原队列头元素的下一个元素
            value.next = null;//释放头元素的next引用
            size--;
            return value;
        }
    }
    //队列长度lq.peek();
    public int length(){
        return size;
    }

    //测试方法
    public static void main(String[] args) {
        LinkQueue lq = new LinkQueue();
        lq.add("a");
        lq.add("b");
        lq.add("c");
        lq.add("d");

        System.out.println(lq.peek().e);
        lq.poll();
        System.out.println(lq.peek().e);
        System.out.println(lq.length());
    }
}

三,循环队列

package Queue;

import java.util.Arrays;

public class LoopQueue<E> {
    private Object[] data = null;
    private int maxSize;//队列容量
    private int head;
    private int foot;
    private int size = 0;

    public LoopQueue(){
        this.maxSize=10;
        data = new Object[10];
        head = foot = 0;
    }

    public LoopQueue(int initalSize){
        if(initalSize>=0){
            this.maxSize = initalSize;
            data = new Object[initalSize];
            head = foot = 0;
        }
        else{
            throw new RuntimeException("对列不能小于零");
        }
    }

    //判空
    public boolean empty(){
        return size==0?true:false;
    }

    //插入
    public boolean add(E e){
        if(size == maxSize){
            throw new RuntimeException("队列已满");
        }
        else{
            data[foot] = e;
            foot++;
            size++;
            return true;
        }
    }

    //查看队首元素
    public E peek(){
        if(empty()){
            throw new RuntimeException("队列为空");
        }
        else{
            return (E) data[head];
        }
    }

    //出队
    public E poll(){
        if(empty()){
            throw new RuntimeException("队列为空");
        }
        else{
            E value = (E) data[head];
            data[head] = null;//释放队首元素
            head++;;//队首指针加一
            size--;
            return value;
        }
    }
    //队列长度
    public int length(){
        return size;
    }

    //清空队列
    public void clean(){
        Arrays.fill(data,null);
        head = foot = 0;
        size=0;
    }

    //测试方法
    public static void main(String[] args) {
        LoopQueue lq =  new LoopQueue();
        lq.add("a");
        lq.add("b");
        lq.add("c");
        lq.add("d");

        System.out.println(lq.peek());
        lq.poll();
        System.out.println(lq.peek());
        System.out.println(lq.length());
    }
}

四,LinkedList实现队列

package Queue;

import java.util.LinkedList;
import java.util.Queue;

public class ListQueue<E> {
    private Queue<E> queue = new LinkedList<E>();

    // 将指定的元素插入此队列(如果立即可行且不会违反容量限制),在成功时返回 true,
    //如果当前没有可用的空间,则抛出 IllegalStateException。
    public boolean add(E e){
        return queue.add(e);
    }

    //获取,但是不移除此队列的头。
    public E element() {
        return queue.element();
    }

    //将指定的元素插入此队列(如果立即可行且不会违反容量限制),当使用有容量限制的队列时,
    //此方法通常要优于 add(E),后者可能无法插入元素,而只是抛出一个异常。
    public boolean offer(E e){
        return queue.offer(e);
    }

    //获取但不移除此队列的头;如果此队列为空,则返回 null
    public E peek(){
        return queue.peek();
    }

    //获取并移除此队列的头,如果此队列为空,则返回 null
    public E poll(){
        return queue.poll();
    }

    //获取并移除此队列的头
    public E remove(){
        return queue.remove();
    }

    //判空
    public boolean empty() {
        return queue.isEmpty();
    }

    public static void main(String[] args) {
        ListQueue lq = new ListQueue();
        lq.add("a");
        lq.add("b");
        lq.add("c");
        lq.add("d");

        System.out.println(lq.element());
        System.out.println(lq.remove());
        System.out.println(lq.peek());
    }

}

原文地址:https://www.cnblogs.com/CherishFX/p/4608880.html

猜你喜欢

转载自blog.csdn.net/qq_33371372/article/details/80602760