Java基于数组和链表两种方式分别实现栈和队列

(stack)也叫堆栈,它是一种运算受限的线性表。

  • 特征:
    (1) 限定仅在表尾进行插入和删除操作,这一端被称为栈顶,相对地,把另一端称为栈底。
    (2) 向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。
    (3) 出栈的顺序为:后进先出(LIFO—last in first out)

在这里插入图片描述

  • 基于数组的实现
package com.ashley.stack;

/**
 * @Author ashley
 * @Date 2019/9/10 11:16
 */
public class ArrayStack {
    private int size;//-----数组的长度(栈容量)
    private int top = -1;//-----初始化栈顶元素的下标
    private int[] arr;//-----数组,存放元素

    public ArrayStack() {
        arr = new int[3];//-----设置栈的初始容量为3
    }

    //判断栈容量的设置是否合法
    public ArrayStack(int size) {
        if (size > 0) {
            this.size = size;
            arr = new int[size];
        }
        else throw new RuntimeException("数组长度不正确!");
    }

    //入栈
    public void push(int data) {
        if (top >= size - 1) {
            System.out.println("栈已满,无法进行入栈操作!");
            return;
        }
        arr[++top] = data;
    }

    //出栈
    public int pop() {
        if (top < 0) {
            throw new RuntimeException("栈已空,无法进行出栈操作!");
        } else {
            int temp = arr[top];
            arr[top] = 0;
            top--;
            return temp;
        }
    }

    //查看栈顶元素,但不出栈
    public int peek() {
        if (top < 0) {
            throw new RuntimeException("栈为空!");
        } else {
            return arr[top];
        }
    }

    //判断栈是否为空
    public boolean isEmpty() {
        return top == -1;
    }

    //判断栈满
    public boolean isFull() {
        return top == size - 1;
    }

    //判断栈中元素的个数
    public int sum() {
        int count = top + 1;
        return count;
    }
}
package com.ashley.stack;

/**
 * @Author ashley
 * @Date 2019/9/10 14:53
 */
public class ArrayStackTest {
    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(5);//设置栈容量为5

        System.out.println(stack.isEmpty());//true

        //入栈
        stack.push(1);
        stack.push(3);
        stack.push(5);
        stack.push(7);
        stack.push(9);

        System.out.println(stack.isFull());//true
        System.out.println(stack.sum());//5

        System.out.println(stack.peek());//9

        System.out.println(stack.pop());//9
        System.out.println(stack.pop());//7
        System.out.println(stack.pop());//5

        System.out.println(stack.isEmpty());//false
        System.out.println(stack.peek());//3

        System.out.println(stack.pop());//3
        System.out.println(stack.pop());//1

        System.out.println(stack.isEmpty());//true

    }
}

  • 基于链表的实现
package com.ashley.stack;

/**
 * @Author ashley
 * @Date 2019/9/10 19:04
 */
public class LinkedListStack {

    private Node top;//-----记录栈顶元素
    private int size = 0;//-----记录栈大小

    //空参构造器,初始化栈顶元素
    public LinkedListStack() {
        top = null;
    }

    //属性的get set方法
    public Node getTop() {
        return top;
    }

    public void setTop(Node top) {
        this.top = top;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }


    //入栈
    public void push(int data) {
        Node newNode = new Node(data);
        if (top == null) {
            top = newNode;

        } else {
            newNode.next = top;//将新元素插到链表头部
            top = newNode;
        }
        size++;
    }

    //出栈
    public Integer pop() {
        if (top == null) throw new RuntimeException("栈为空");
        else {
            int val = top.data;
            top = top.next;
            size--;
            return val;
        }

    }

    //查看栈顶元素但不出栈
    public Integer peek() {
        if (top == null) throw new RuntimeException("栈为空");
        else {
            return top.data;
        }

    }

    public boolean isEmpty() {
        return top == null;
    }


    public class Node {
        public int data; //记录数据
        public Node next;

        Node(int data) {
            this.data = data;
        }
    }
}

package com.ashley.stack;

/**
 * @Author ashley
 * @Date 2019/9/10 19:09
 */
public class LinkedListStackTest {
    public static void main(String[] args) {
        LinkedListStack stack = new LinkedListStack();

        stack.push(4);
        stack.push(5);
        stack.push(6);

        System.out.println("size=" + stack.getSize());//size=3

        System.out.println(stack.isEmpty());//false

        System.out.println(stack.pop());//6

        System.out.println("size=" + stack.getSize());//size=2

        System.out.println(stack.pop());//5
        System.out.println(stack.pop());//4

        System.out.println(stack.isEmpty());//true

    }
}

队列

队列是一种特殊的线性表,和栈一样,操作受到限制。

  • 特征:
    (1)只允许在表的前端(front)即队头,进行删除操作,而在表的后端(rear)即队尾,进行插入操作。
    (2)出队顺序:先进先出(FIFO—first in first out)

数组:
在这里插入图片描述
双向链表:
在这里插入图片描述

  • 基于数组实现
package com.ashley.queue;

/**
 * @Author ashley
 * @Date 2019/9/11 9:03
 */
public class ArrayQueue {
    private int[] arrInt;//-----数组
    private int front;//-----头指针
    private int rear;//-----尾指针

    //初始化
    public ArrayQueue(int size) {
        this.arrInt = new int[size];
        front = 0;
        rear = -1;
    }
    //判断队列是否为空
    public boolean isEmpty() {
        return front == arrInt.length;
    }

    //判断队列是否已满
    public boolean isFull() {
        return arrInt.length - 1 == rear;
    }

    //在队尾插入元素
    public void insert(int item) {
        if (isFull()) {
            throw new RuntimeException("队列已满");
        }
        arrInt[++rear] = item;
    }
    //获取对头元素
    public int peekFront() {
        return arrInt[front];
    }

    //获取队尾元素
    public int peekRear() {
        return arrInt[rear];
    }

    //从队头移除元素
    public int remove() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arrInt[front++];
    }

}
package com.ashley.queue;

/**
 * @Author ashley
 * @Date 2019/9/11 9:21
 */
public class ArrayQueueTest {
    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(6);

        System.out.println(queue.isEmpty());//false

        for (int i = 0; i < 6; i++) {
            queue.insert(i);
        }
        System.out.println(queue.isFull());//true

        System.out.println(queue.remove());//0
        System.out.println(queue.remove());//1

        System.out.println(queue.peekFront());//2
        System.out.println(queue.peekRear());//5

    }
}
  • 基于链表实现

结点类:

package com.ashley.queue;

/**
 * @Author ashley
 * @Date 2019/9/11 9:43
 */
public class Node {
    private Object data;
    private Node next;
    private Node prev;

    public Node(Object data) {
        this.data = data;
    }

    public Node(Object data, Node next, Node prev) {
        this.data = data;
        this.next = next;
        this.prev = prev;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public Node getPrev() {
        return prev;
    }

    public void setPrev(Node prev) {
        this.prev = prev;
    }
}

队列类:

package com.ashley.queue;

/**
 * @Author ashley
 * @Date 2019/9/11 9:47
 */
public class LinkedListQueue {
    int length = 5;
    int size = 0;

    Node first;
    Node last;

    //判断队列是否已满
    public boolean isFull() {
        return size == length;
    }

    //入队
    public void insert(String value) {
        if (isFull()) {
            throw new RuntimeException("队列已满");
        }
        Node newnode = new Node(value, last, null);
        Node temp = last;
        last = newnode;
        if (temp == null) {
            first = newnode;
        } else {
            temp.setNext(newnode);
        }
        size++;
    }

    //判断队列是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    //出队
    public String remove() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        Node temp = first;
        first = first.getNext();
        size--;
        return (String) temp.getData();

    }
}

测试类:

package com.ashley.queue;

/**
 * @Author ashley
 * @Date 2019/9/11 10:32
 */
public class LinkedListQueueTest {
    public static void main(String[] args) {
        LinkedListQueue queue = new LinkedListQueue();

        System.out.println(queue.isEmpty());//true
        queue.insert("A");
        queue.insert("B");
        queue.insert("C");
        System.out.println(queue.isEmpty());//false
        System.out.println(queue.size);//3

        queue.insert("D");
        queue.insert("E");

        System.out.println(queue.size);//5
        System.out.println(queue.isFull());//true

        System.out.println(queue.remove());//A
        System.out.println(queue.remove());//B


    }
}

发布了49 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ashleyjun/article/details/100701873