黑马程序员_集合框架_LinkedList模拟栈和队列

----------- android培训java培训、java学习技术总结、期待与您交流! ------------

   LInkedList模拟队列:

package com.itcast.test;

import java.util.LinkedList;

class Queue{
	LinkedList lin=new LinkedList();
    public void add(Object obj){
          lin.addFirst(obj);
    }
    public Object get(){
    	return lin.removeLast();
    }
    public Boolean isNull(LinkedList lin){
		return lin.isEmpty();
		}
}

public class QueueDemo {
	public static void sop(Object obj){
		System.out.println(obj);
	}
	public static void main(String[] args){
		Queue q=new Queue();
		q.add("java_1");
		q.add("java_2");
		q.add("java_3");
		q.add("java_4");
		sop(q.get());
		sop(q.get());
		sop(q.get());
		sop(q.get());
		
	}

}

      LinkedList模拟栈:

package com.itcast.test;

import java.util.LinkedList;


class Stack{
	LinkedList lin;
	public Stack(){
		lin=new LinkedList();
	}
	public void add(Object obj){
		 lin.addFirst(obj);	
	}
	public Object get(){
		return lin.removeFirst();
	}
	public Boolean isNull(LinkedList lin){
		return lin.isEmpty();
		}
}
public class StackDemo {

	/**
	 * @param args
	 */
	public static void sop(Object obj){
		System.out.println(obj);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Stack st=new Stack();
		st.add("java_1");
		st.add("java_2");
		st.add("java_3");
		st.add("java_4");
		sop(st.get());
		sop(st.get());
		sop(st.get());
		sop(st.get());
	}

}

 

 

----------- android培训java培训、java学习技术总结、期待与您交流! ------------

你可能感兴趣的:(java)