zoukankan      html  css  js  c++  java
  • 使用LinkedList实现堆栈和队列数据结构存储方式

    package cn.itcast.collection;
    
    import java.util.LinkedList;
    
    /*使用LinkedList实现模拟堆栈和队列的数据结构存储方式*/
    
    // 模拟堆栈数据结构存储方式
    class StackList {
        LinkedList list;
        public StackList() {
            list = new LinkedList();
        }
        
        public void add(Object o) {
            list.push(o);
        }
        
        public Object pop() {
            return list.pop();
        }
        
        public int size() {
            return list.size();
        }
    }
    
    // 队列数据结构存储方式
    class TeamList {
        LinkedList list;
        public TeamList() {
            list = new LinkedList();
        }
        
        public void add(Object o) {
            list.offer(o);
        }
        
        public Object poll() {
            return list.poll();
        }
        
        public int size() {
            return list.size();
        }
    }
    
    public class Demo2 {
    
        public static void main(String[] args) {
            StackList s = new StackList();
            s.add("张三");
            s.add("李四");
            s.add("王五");
            int size = s.size();
            for(int i = 0;i < size; i++) {
                System.out.println(s.pop());
            }
            
            TeamList t = new TeamList();
            t.add("张三");
            t.add("李四");
            t.add("王五");
            size = t.size();
            for(int i = 0;i < size; i++) {
                System.out.println(t.poll());
            }
        }
        
    
    }
  • 相关阅读:
    Makefile 之 $(Q)
    LeetCode-50-Pow(x, n)
    LeetCode-49. Group Anagrams
    全排列问题全面解析
    LeetCode-47. Permutations II
    LeetCode-46. Permutations
    LeetCode-43. Multiply Strings
    LeetCode-40. Combination Sum II
    LeetCode-39. Combination Sum
    LeetCode-36. Valid Sudoku
  • 原文地址:https://www.cnblogs.com/running-fish/p/9511175.html
Copyright © 2011-2022 走看看