双端链表: 比链表多了一些新的特性。
属性:新建了一个对结尾链接点的引用
方法:1.在表尾处可以插入一个链接点;普通链表的也可以,只不过要遍历整个链表才行。
2.可以访问表尾
3.但是,如果对结尾链接点进行删除操作,需要遍历列表。
package firstlast;
public class Link {
public long dData;
public Link next;
public Link( long dData){
this.dData = dData;
}
public void displayLink(){
System.out.println( "{"+dData+ "}");
}
}
public class Link {
public long dData;
public Link next;
public Link( long dData){
this.dData = dData;
}
public void displayLink(){
System.out.println( "{"+dData+ "}");
}
}
package firstlast;
public class FirstLastLink {
private Link first;
private Link last;
public FirstLastLink(){
first = null;
last = null;
}
public boolean isEmpty(){
return first == null;
}
public void insertFirst( long dd){
Link newLink = new Link(dd);
if(isEmpty()){ //如果原来是空的,更新尾节点;如果不为空,不需要对尾节点进行操作
last = newLink;
}
newLink.next = first;
first = newLink;
}
public void insertLast( long dd){
Link newLink = new Link(dd);
if(isEmpty()){
first = newLink;
} else{
last.next = newLink; //更新最后一个链接点,指向新链接点
}
last = newLink; //更新尾节点,指向新链接点
}
public long deleteFirst(){
if(first == null){
System.out.println( "没有数据");
return 0;
}
long temp = first.dData;
if(first.next == null){
last = null;
}
first = first.next;
return temp;
}
public long deleteLast(){
long temp = last.dData;
if(first == null){
System.out.println( "没有数据");
return 0;
}
return temp;
}
public void displayList(){
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
}
public static void main(String[] args){
FirstLastLink firstlastlink = new FirstLastLink();
firstlastlink.insertFirst(1);
firstlastlink.insertFirst(2);
firstlastlink.insertLast(4);
firstlastlink.insertLast(0);
firstlastlink.deleteFirst();
firstlastlink.d();
firstlastlink.displayList();
}
}
public class FirstLastLink {
private Link first;
private Link last;
public FirstLastLink(){
first = null;
last = null;
}
public boolean isEmpty(){
return first == null;
}
public void insertFirst( long dd){
Link newLink = new Link(dd);
if(isEmpty()){ //如果原来是空的,更新尾节点;如果不为空,不需要对尾节点进行操作
last = newLink;
}
newLink.next = first;
first = newLink;
}
public void insertLast( long dd){
Link newLink = new Link(dd);
if(isEmpty()){
first = newLink;
} else{
last.next = newLink; //更新最后一个链接点,指向新链接点
}
last = newLink; //更新尾节点,指向新链接点
}
public long deleteFirst(){
if(first == null){
System.out.println( "没有数据");
return 0;
}
long temp = first.dData;
if(first.next == null){
last = null;
}
first = first.next;
return temp;
}
public long deleteLast(){
long temp = last.dData;
if(first == null){
System.out.println( "没有数据");
return 0;
}
return temp;
}
public void displayList(){
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
}
public static void main(String[] args){
FirstLastLink firstlastlink = new FirstLastLink();
firstlastlink.insertFirst(1);
firstlastlink.insertFirst(2);
firstlastlink.insertLast(4);
firstlastlink.insertLast(0);
firstlastlink.deleteFirst();
firstlastlink.d();
firstlastlink.displayList();
}
}
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/229154,如需转载请自行联系原作者