一、LinkedList模拟队列数据结构
源码+思路:
import java.util.LinkedList;
public class QueueTest {
public static void main(String[] args) {
//队列先进先出FIFO
//创建一个队列对象
Queue que=new Queue();
//添加元素
que.queAdd("itcast1");
que.queAdd("itcast2");
que.queAdd("itcast3");
que.queAdd("itcast4");
while(!que.isNull()){
System.out.println(que.getQue());
}
}
}
class Queue{
//封装一个链表数据结构
private LinkedList link;
public Queue(){
link=new LinkedList();
}
public void queAdd(Object obj){
link.addFirst(obj);
}
public Object getQue(){
return link.removeLast();
}
//判断队列是否为空
public boolean isNull(){
return link.isEmpty();
}
}
二、LinkedList模拟堆栈数据结构
源码+思路:
import java.util.LinkedList;
public class HeapTest {
public static void main(String[] args) {
//堆栈先进后出FILO
//创建一个堆栈对象
Heap heap=new Heap();
heap.addHeap("itcast1");
heap.addHeap("itcast2");
heap.addHeap("itcast3");
heap.addHeap("itcast4");
while(!heap.isNull()){
System.out.println(heap.getHeap());
}
}
}
class Heap{
//封装一个链表数据结构
private LinkedList link;
Heap(){
link=new LinkedList();
}
public void addHeap(Object obj){
link.addFirst(obj);
}
public Object getHeap(){
return link.removeFirst();
}
public boolean isNull(){
return link.isEmpty();
}
}