二、栈、队列和链表

来源:互联网 发布:豆豆软件 编辑:程序博客网 时间:2024/05/01 11:31

简介

栈的思想是先进后出,也就是说一个房屋只有一个入口和出口,入口即出口。每次有数据进入时,从栈顶将它压入栈中,每次有数据取出时,将它弹出栈中。栈一般用数组来实现,也可以使用链表。栈主要的操作即入栈和出栈。

操作 时间复杂度 入栈 O(1) 出栈 O(1) 搜索 O(n)

Java实现栈

public class Stack {    private static int[] values = new int[10];    private static int index = 0;    /**     * 入栈     **/    public static void push(int value) {        values[++index] = value;    }    /**     * 出栈     **/    public static int pop() {        return values[index--];    }    public static void main(String[] args) {        push(2);        push(3);        System.out.println(pop());        push(8);        push(1);        push(22);        System.out.println(pop());        System.out.println(pop());        System.out.println(pop());        System.out.println(pop());    }    /*     * 输出结果:3 22 1 8 2     */}

应用场景

栈的应用场景用的范围似乎不是很广,不过一些操作用栈来实现似乎更方便。
例如复杂计算器的实现。当输入1+2*3时,不能从左到右依次计算,要将操作符压入栈中,遇到优先级高的,依次出栈。

队列

简介

队列和栈相反,它是先进先出。队列的效率和栈一样。进栈出栈时间复杂度都是o(1)

Java实现队列

public class Queue {    private static int[] values = new int[10];    private static int top = 0;    private static int bottom = 0;    private static int itemLength = 0;    public static void insert(int value) throws Exception {        if (itemLength < values.length) {            values[bottom] = value;            if (bottom == values.length - 1) {                bottom = 0;            } else {                bottom++;            }            itemLength++;            return;        }        throw new Exception("队列已满");    }    public static int pop() throws Exception {        if (itemLength > 0) {            int value = values[top];            values[top] = 0;            if (top == values.length - 1) {                top = 0;            } else {                top++;            }            itemLength--;            return value;        }        throw new Exception("空队列");    }    public static void main(String[] args) throws Exception {        insert(10);        insert(2);        insert(5);        insert(9);        System.out.println(pop());        System.out.println(pop());        System.out.println(pop());        System.out.println(pop());    }}

应用场景

如消息队列,就是使用队列来实现。

链表

简介

链表是数据结构中第二个重要的一个结构。它不像数组一样,具有下标,它是通过前一个节点通过指针指向下一个节点,这样链式的存储。
这里写图片描述

Java实现链表

public class LinkList {    private Link first;    public LinkList() {        this.first = null;    }    /**     * 链表的插入     */    public void insert(int value) {        Link newLink = new Link();        newLink.value = value;        newLink.next = first;        first = newLink;    }    /**     * 链表的删除     */    public void delete() {        if (this.first == null) {            System.out.println("空链表,无需再删除");        } else {            this.first = this.first.next;        }    }    /**     * 链表的查找     */    public Link search(int value) {        Link temp = this.first;        while (temp != null) {            if (temp.value == value) {                return temp;            }            temp = temp.next;        }        System.out.println("您搜索的值不存在");        return null;    }    public void display() {        Link temp = this.first;        while (temp != null) {            System.out.println(temp.value);            temp = temp.next;        }    }    private static class Link {        private int value;        private Link next;    }    public static void main(String[] args) {        LinkList linkList = new LinkList();        linkList.insert(10);        linkList.insert(20);        linkList.insert(30);        linkList.insert(40);        linkList.delete();        linkList.search(40);        linkList.display();    }}

优缺点

链表的优点是弥补了数组的一些不足。数组插入一个数据,后面的数据都要相应的做一次移动,效率较低。而使用链表,插入只需要改变引用即可。同时,对于删除操作,链表也更有优势。
唯一的不足是,链表的查询较慢。查询一个数据,都需要从头开始去遍历,因为你只能通过上一个节点拿到下一个节点,而不能直接从头节点拿到第n个节点。