90 likes | 265 Views
Double Linked Node Class. public class DoubleLinkedNode{ private String info = new String(); private DoubleLinkedNode nextLeft = null; private DoubleLinkedNode nextRight = null; public DoubleLinkedNode(DoubleLinkedNode nextLeft, String info, DoubleLinkedNode nextRight){
E N D
Double Linked Node Class public class DoubleLinkedNode{ private String info = new String(); private DoubleLinkedNode nextLeft = null; private DoubleLinkedNode nextRight = null; public DoubleLinkedNode(DoubleLinkedNode nextLeft, String info, DoubleLinkedNode nextRight){ this.nextLeft = nextLeft; this.info = info; this.nextRight = nextRight; } public DoubleLinkedNode(String info){ this.nextLeft = null; this.info = info; this.nextRight = null; }
Double Linked Node Class public void setNextRight(DoubleLinkedNode right){ this.nextRight = right; } public void setNextLeft(DoubleLinkedNode left){ this.nextLeft = left; } public DoubleLinkedNode getNextRight(){ return this.nextRight; } public DoubleLinkedNode getNextLeft(){ return this.nextLeft; }
Double Linked Node Class public void setInfo (String newInfo){ this.info = newInfo; } public String getInfo(){ return this.info; } }
Double Ended Queue Class import java.io.*; public class DoubleEndedQueue{ private DoubleLinkedNode leftmost; private DoubleLinkedNode rightmost; public DoubleEndedQueue (){ leftmost = null; rightmost = null; }
Double Ended Queue Class public void addNodeLeft(DoubleLinkedNode DLNode){ if(leftmost == null){ leftmost = DLNode; rightmost = DLNode; } else{ leftmost.setNextLeft(DLNode); DLNode.setNextRight(leftmost); leftmost = DLNode; } }
Double Ended Queue Class public void addNodeRight(DoubleLinkedNode DLNode){ if(rightmost == null){ rightmost = DLNode; leftmost = DLNode; } else{ rightmost.setNextRight(DLNode); DLNode.setNextLeft(rightmost); rightmost = DLNode; } }
Double Ended Queue Class public void removeNodeLeft(){ if(leftmost!=null){ leftmost = leftmost.getNextRight(); leftmost.setNextLeft(null); } } public void removeNodeRight(){ if(rightmost!=null){ rightmost = rightmost.getNextLeft(); rightmost.setNextLeft(null); } }
Double Ended Queue Class public String writeListRightLeft(){ String s = ""; DoubleLinkedNode cursor = rightmost; while(cursor != null){ s = (s+"\n "+(cursor.getInfo())); cursor = cursor.getNextLeft(); } return s; }
Double Ended Queue Manager Class public class DEQmanager{ public static void main (String arg[ ]){ DoubleEndedQueue deq = new DoubleEndedQueue( ); int i = 0; for (i = 1; i<=5; i++){ deq.addNodeRight(new DoubleLinkedNode(""+ i)); } ///for System.out.println("printing left to right" ); System.out.println(deq.writeListLeftRight( )); System.out.println("printing right to left" ); System.out.println(deq.writeListRightLeft( )); }//end of main method } //end of class