top of page
Search
Writer's pictureAbhinaw Tripathi

Double-Ended Lists in core-java


Double-Ended List A double-ended lists is similar to an ordinary linked list but it has one additional feature that a reference to the last link as well as the first. The reference to the last link permits you to insert a new link directly at the end of the list as well as at the beginning. Demonstration on Double-Ended Linked List: /** * */ /** * @author Abhinaw.Tripathi * */ class Link { public long dData; public Link next; public Link(long d) { this.dData=d; } public void displayLink() { System.out.println(dData+ " "); } } class FirstLastList { private Link first; private Link last; public FirstLastList() { first=null; last=null; } public boolean isEmpty() { return first== null; } public void insertFirst(long dd) { Link newLink=new Link(dd); if(isEmpty()) first=newLink; else last=newLink; last=newLink; } public void insertLast(long dd) { Link newLink=new Link(dd); if(isEmpty()) first=newLink; else last.next =newLink; last=newLink; } public long deleteFirst() { long temp=first.dData; if(first.next == null) last=null; first=first.next; return temp; } public void displayList() { System.out.println("First----->Last : "); Link current=first; while(current!=null) { current.displayLink(); current=current.next; } System.out.println(" "); } } public class DoubleEndedTestApp { public static void main(String[] args) { FirstLastList theList=new FirstLastList(); theList.insertFirst(22); theList.insertFirst(44); theList.insertFirst(66); theList.insertLast(11); theList.insertLast(33); theList.displayList(); theList.deleteFirst(); theList.displayList(); } }

2 views0 comments

Recent Posts

See All
bottom of page