一个双端链表解决方案
一个双端链表
------解决方案--------------------
程序构造了一个链表,链表的每个单元是Link对象。Link由两部分组成,存值的dData属性和指向下一个单元的next属性。链表是单向的。
public void insertFirst(long dd)
从头插入单元,新的单元作为链表头(first)。这里要注意的是,第一单元被插入时,程序同时指定这个单元是链表的尾(last)。
public void insertlast(long dd)
从尾插入单元,新的单元作为链表尾(last)。这里要注意的是,第一单元被插入时,程序同时指定这个单元是链表的头(first)。
public void displayList()
如果清楚了first,last指向同一个链的头尾两个单元,这个方法就好玩解了。
建议在纸上画出程序运行过程中对链的操作。
------解决方案--------------------
你所说的:“但是看代码,这里的值都是插入的成员变量last里 ”是不正确的,值是插入到last的next里
public void insertlast(long dd) {
Link newlink = new Link(dd);
if (isEmpty()) {
first = newlink; //双端链表为空,第一次插入结点,把该结点做为第一个结点
} else {
last.next = newlink; //双端链表不为空,则把该结点插入到最后一个结点的后面
}
last = newlink; //设置插入的结点为最后一个结点
}
下面这段代码是递归打印list里边的所有值,而不是打印first。
public void displayList() {
System.out.print("List(first-->last):");
Link current = first;
while(current != null){
current.displayLink();
current = current.next; //current后移
}
System.out.println("");
}
- Java code
/** * * 双端链表 */ class Link { public long dData; public Link next; public Link(long d) { dData = d; } public void displayLink() { System.out.print(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()) { last = newlink; } newlink.next = first; first = newlink; } //这里看不懂,不知道为什么插入last,然后他插入的值会在first里面,displayList()方法可以打印出first的值,first和last好像都不是同一个Link 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.print("List(first-->last):"); Link current = first; while(current != null){ current.displayLink(); current = current.next; } System.out.println(""); } } public class Test4 { 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.insertlast(55); thelist.displayList(); thelist.deleteFirst(); thelist.deleteFirst(); thelist.displayList(); } }
------解决方案--------------------
程序构造了一个链表,链表的每个单元是Link对象。Link由两部分组成,存值的dData属性和指向下一个单元的next属性。链表是单向的。
public void insertFirst(long dd)
从头插入单元,新的单元作为链表头(first)。这里要注意的是,第一单元被插入时,程序同时指定这个单元是链表的尾(last)。
public void insertlast(long dd)
从尾插入单元,新的单元作为链表尾(last)。这里要注意的是,第一单元被插入时,程序同时指定这个单元是链表的头(first)。
public void displayList()
如果清楚了first,last指向同一个链的头尾两个单元,这个方法就好玩解了。
建议在纸上画出程序运行过程中对链的操作。
------解决方案--------------------
你所说的:“但是看代码,这里的值都是插入的成员变量last里 ”是不正确的,值是插入到last的next里
public void insertlast(long dd) {
Link newlink = new Link(dd);
if (isEmpty()) {
first = newlink; //双端链表为空,第一次插入结点,把该结点做为第一个结点
} else {
last.next = newlink; //双端链表不为空,则把该结点插入到最后一个结点的后面
}
last = newlink; //设置插入的结点为最后一个结点
}
下面这段代码是递归打印list里边的所有值,而不是打印first。
public void displayList() {
System.out.print("List(first-->last):");
Link current = first;
while(current != null){
current.displayLink();
current = current.next; //current后移
}
System.out.println("");
}