Java LinkedList 实现栈和队列
package com. zhong. collection ; import java. util. LinkedList ; public class LinkedListDemo { public static void main ( String [ ] args) { LinkedList < String > queue = new LinkedList < > ( ) ; System . out. println ( "-------------进队-------------" ) ; queue. addLast ( "第一位嘉宾" ) ; queue. addLast ( "第二位嘉宾" ) ; queue. addLast ( "第三位嘉宾" ) ; queue. addLast ( "第四位嘉宾" ) ; queue. addLast ( "第五位嘉宾" ) ; System . out. println ( queue) ; System . out. println ( "-------------出队-------------" ) ; System . out. println ( queue. removeFirst ( ) ) ; System . out. println ( queue. removeFirst ( ) ) ; System . out. println ( queue. removeFirst ( ) ) ; System . out. println ( queue) ; LinkedList < String > stack = new LinkedList < > ( ) ; System . out. println ( "-------------进队-------------" ) ; stack. push ( "第一颗子弹" ) ; stack. push ( "第二颗子弹" ) ; stack. push ( "第三颗子弹" ) ; stack. push ( "第四颗子弹" ) ; stack. push ( "第五颗子弹" ) ; System . out. println ( stack) ; System . out. println ( "-------------进队-------------" ) ; System . out. println ( stack. pop ( ) ) ; System . out. println ( stack. pop ( ) ) ; System . out. println ( stack. pop ( ) ) ; System . out. println ( stack) ; }
}