2-Linked List
2-Linked List
class Link
{
public int iData; // data
public double dData; // data
public Link next; // reference to next link
}
This kind of class definition is sometimes called self-referential because it contains a
field—called next in this case—of the same type as itself.
Each link has two references to other links instead of one. The first is to
the next link, as in ordinary lists. The second is to the previous link.
So we can:
Insert a node in any position
Delete a node from any position
Traverse the list forward and backward
if( isEmpty() ) {
last = newLink;
first = newLink;
}
Else {
first.previous = newLink;
newLink.next = first;
first = newLink; }
if(current==last) {
newLink.next = null;
last = newLink;
newLink.previous = current;
current.next = newLink;
}
else {
newLink.next = current.next;
current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;