/**
* 双端链表
*/
class MyLinkList {
public Link first;
public Link last;
public MyLinkList(){
first = null;
last = null;
}
//判空
public boolean isEmpty(){
return first == null;
}
//从头插入
public void insertFirst(int dd){
Link link = new Link(dd);
if(isEmpty()) {
last = link;
}
link.next = first;
first = link;
}
//从尾部插入
public void insertLast(int dd){
Link link = new Link(dd);
if(isEmpty()){
first = link;
}else {
last.next = link;
}
last = link;
}
//从开始位置删除
public int deleteFirst(){
int temp = first.dData;
if(first.next == null){
last = null;
}
first = first.next;
return temp;
}
//打印
public void displayList(){
System.out.println("List(first -> last):");
Link current = first;
while (current != null){
current.displayLink();
current = current.next;
}
System.out.println();
}
public static void main(String[] args){
MyLinkList myLinkList = new MyLinkList();
myLinkList.insertFirst(22);
myLinkList.insertFirst(44);
myLinkList.insertFirst(66);
myLinkList.insertLast(11);
myLinkList.insertLast(33);
myLinkList.insertLast(55);
myLinkList.displayList();
myLinkList.deleteFirst();
myLinkList.deleteFirst();
myLinkList.displayList();
}
}
class Link{
public int dData; //数据
public Link next; //下一个节点的引用
public Link(int d){
dData = d;
}
public void displayLink(){
System.out.print(dData + " ");
}
}
java版数据结构与算法—双端链表
猜你喜欢
转载自blog.csdn.net/weixin_38799368/article/details/84206176
今日推荐
周排行