• 用LinkedList集合演示栈和队列的操作


    在数据结构中,栈和队列是两种重要的线性数据结构。它们的主要不同在于:栈中存储的元素,是先进后出;队列中存储的元素是先进先出。我们接下来通过LinkedList集合来演示栈和队列的操作。

    import java.util.LinkedList;
    
    /**
     * 用LinkedList集合演示栈和队列操作
     *
     * @author 李章勇
     */
    public class Test1 {
        public static void main(String[] args) {
            LinkedList<String> test = new LinkedList<>();
    //1.演示栈Stack操作
            System.out.println("1.演示栈Stack操作-----");
            System.out.println("(1)向栈中添加元素  push()");
            test.push("a");
            test.push("b");
            test.push("c");
            test.push("e");
            test.push("f");
            test.push("g");
            System.out.println(test.toString());
            System.out.println("(2)从栈中取元素,但是不删除  peek()");
            String peek = test.peek();
            System.out.println("用peek()取出一个元素:" + peek.toString());
            System.out.println("取出一个元素后,集合为:" + test.toString());
            System.out.println("(3)从栈中取元素,并且删除  pop()");
            String pop = test.pop();
            System.out.println("用pop()取出一个元素:" + pop);
            System.out.println("取出一个元素后,集合为:" + test.toString());
            test.clear();
    //2.演示队列Queue操作
            System.out.println("2.演示队列Queue操作-----");
            System.out.println("(1)向队列中添加元素  add()");
            test.add("a");
            test.add("b");
            test.add("c");
            test.add("d");
            test.add("e");
            test.add("f");
            System.out.println(test.toString());
            System.out.println("(2)从队列中移除元素  element()");
    //注意:与peek()不同的是,如果队列为空,element()会返回一个异常,为此,需要先判断test集合是否为空
            if (test != null) {
                String element = test.element();
                System.out.println("用element取出一个元素:" + element);
                System.out.println("取出一个元素后,集合为:" + test.toString());
            }
            System.out.println("(3)从队列中移除元素  poll()");
            String poll = test.poll();
            System.out.println("用poll()取出一个元素:" + poll);
            System.out.println("取出一个元素后,集合为:" + test.toString());
        }
    }  

      运行结果如下:

    1.演示栈Stack操作-----
    (1)向栈中添加元素  push()
    [g, f, e, c, b, a]
    (2)从栈中取元素,但是不删除  peek()
    用peek()取出一个元素:g
    取出一个元素后,集合为:[g, f, e, c, b, a]
    (3)从栈中取元素,并且删除  pop()
    用pop()取出一个元素:g
    取出一个元素后,集合为:[f, e, c, b, a]
    2.演示队列Queue操作-----
    (1)向队列中添加元素  add()
    [a, b, c, d, e, f]
    (2)从队列中移除元素  element()
    用element取出一个元素:a
    取出一个元素后,集合为:[a, b, c, d, e, f]
    (3)从队列中移除元素  poll()
    用poll()取出一个元素:a
    取出一个元素后,集合为:[b, c, d, e, f]
    
  • 相关阅读:
    Docker容器(分享十五)
    oracle数据库迁移主从复制ADG(分享十四)
    mysql数据库迁移主从复制(分享十三)
    rsync+inotify实现服务器之间文件实时同步(分享十三)
    mysql数据库迁移(分享十二)
    数据库迁移(分享十一续集)
    Codeforces 1185G2 Playlist for Polycarp (hard version) 背包,暴力
    Codeforces 747F Igor and Interesting Numbers DP 组合数
    Codeforces 745E Hongcow Buys a Deck of Cards 状压DP / 模拟退火
    Codeforces 1140F 线段树 分治 并查集
  • 原文地址:https://www.cnblogs.com/lizhangyong/p/8137061.html
Copyright © 2020-2023  润新知