☻According to our previous topic Double-ended linked list, it was an addition reference that point to the last linked list and first linked list.
B. Illustration:
D. Reference:
[WIKI] http://www.rosettacode.org/wiki/Doubly-Linked_List_(element_insertion)
C. java Code:
{
public int iData;
public double dData:
public Link next;
public Link(int id,double dd)
{
iData = id;
dData=dd;}
public void displayLink()
{
System.out.print("{"+iData+","dData+"}");}}
class FirstLastList
{
private Link first;
private Link last;
public FirstLastList()
{
first = null;
last = null;
}
public boolean isEmpty()
{
return (first == null);
}
public void insertFirst(int id,double dd)
{
Link newLink = new Link(id,dd);
if (isEmpty ())
last = newLink;
newLink.next = first;
first = newLink;
}
public void insertLast(int id,double dd)
{
Link newLink = new Link(id,dd);
if (isEmpty())first = newLink;
elselast.next = newLink;
last = newLink;
}
public int deleteFirst()
{
int temp = first.iData;
if (first.next == null)
last = null;
first = first.next;
return temp;
}
public void displayList()
{
System.out.print("List(first-->Last);");
Link current=first;
while(current!=null)
{
current.displayLink();
current=current.next;
}
}
System.out.println(" ");
}
}
public class FirstLastApp
{
public static void main(String[] args)
{
FirstLastList theList = new FirstLastList();
theList.insertFirst(22,2.99);
theList.insertFirst(44,4.99);
theList.insertFirst(66,6.99);
theList.insertLast(11,1.99);
theList.insertLast(33,3.99);
theList.insertLast(55,5.99);
System.out.println(theList);
theList.deleteFirst();
theList.deleteFirst();
System.out.println(theList);
}
}
D. Reference:
[WIKI] http://www.rosettacode.org/wiki/Doubly-Linked_List_(element_insertion)
No comments:
Post a Comment