連結串列實現棧和佇列的方法
阿新 • • 發佈:2019-02-19
介面的實現方法如下
public class LinkedQueue implements Serializable,QueueInterface { //佇列前端的節點 private Node firstNode; //對列後端的節點 private Node lastNode; //構造方法初始化 public LinkedQueue(){ firstNode=null; lastNode=null; } @Override public void enqueue(Object newEntity) { Node newNode=new Node(newEntity, null); if(isEmpty()){ firstNode=newNode; }else{ lastNode.setNextNode(newNode); } } @Override public Object dequeue() { Object front=null; if(!isEmpty()){ front=firstNode.getData(); firstNode=firstNode.getNextNode(); if(firstNode==null) lastNode=null; } return front; } @Override public Object getFront() { Object front =null; if(!isEmpty()) front=firstNode.getData(); return front; } @Override public boolean isEmpty() { return firstNode==null; } }