以下循环双链表类的深拷贝构造方法有什么错误??如何改正? public CirDoublyLinkedList(CirDoublyLinkedList<T> list)
{
this();
DLinkNode<T> p=list.head.next;
DLinkNode<T> rear=this.head;
While(p!=list.head){
rear.next=new DLinkNode<T>(p.data,rear,this.head);
rear=rea.next;
p=p.next; }}
代码 看不懂啥意思,麻烦大虾们指点!谢!
{
this();
DLinkNode<T> p=list.head.next;
DLinkNode<T> rear=this.head;
While(p!=list.head){
rear.next=new DLinkNode<T>(p.data,rear,this.head);
rear=rea.next;
p=p.next; }}
代码 看不懂啥意思,麻烦大虾们指点!谢!
循环链表的话
你传进来的list也许是循环链表,但是你拷贝没有做到循环
在while循环外面应该还要加一句rear.next = this.head
暂时还没看出其他问题
public CirDoublyLinkedList(CirDoublyLinkedList<T> list)
{
this();
DLinkNode<T> p=list.head.next; //获得待拷贝链表的首个元素
DLinkNode<T> rear=this.head; //将rear指向当前对象的头,将待拷贝的元素一个个复制到当前对象上
While(p!=list.head){ //判断待拷贝对象是否遍历完毕
rear.next=new DLinkNode<T>(p.data,rear,this.head); //每次new一个和p指向的一样的对象,也就是深克隆给rear
rear=rear.next; //rear后移
p=p.next; //p后移}
rear.next = this.head; //这里应该是少了这句话,让当前链表的末元素连接到头上,形成循环链表
}
你的DLinkNode这个构造函数里面是不是已经将rear指向了this.head
rear.next=new DLinkNode<T>(p.data,rear,this.head);
While(p!=list.head){ //判断待拷贝对象是否遍历完毕
这一点不是很明白,上面的p=list.head.next不是说获得待拷贝元素的首个元素么?那为什么while循环条件是p!=list.head呢?这样的话不就是不能循环了么? 嘿嘿,刚学不是很明白
head->a->b->c->head 大致是这样 后面一个head就是前面的head形成一个循环,这里不好画就这样表示
首先p指向a,然后rear指向新链表的head
拷贝a到head后面
一直持续下去
p指向c时拷贝完成,会后移p,指向head也就结束了,head不存在值 不需要拷贝
rear.next=new DLinkNode<T>(p.data,rear,this.head);
新的结点构造应该是这样new DLinkNode<T>(data, preNode, nextNode),即第二个参数是此结点的上一个结点,第三个参数是此结点的下一个结点。
而这句代码里preNode是对的,但nextNode一直指向this.head,当然形成不了双向循环列表了,形成的是一个反向循环,正向都指向head的列表
public CirDoublyLinkedList(CirDoublyLinkedList<T> list)
{
this();
DLinkNode<T> p=list.head.next;
DLinkNode<T> rear=this.head;
While(p!=list.head){
rear.pre.next = rear;rear.next=new DLinkNode<T>(p.data,rear,this.head);
rear=rea.next;
p=p.next; }}
public CirDoublyLinkedList(CirDoublyLinkedList<T> list)
{
this();
DLinkNode<T> p=list.head.next;
DLinkNode<T> rear=this.head;
While(p!=list.head){
rear.pre.next = rear;
rear.next=new DLinkNode<T>(p.data,rear,this.head);
rear=rea.next;
p=p.next; }}
public CirDoublyLinkedList(CirDoublyLinkedList<T> list)
{
this();
DLinkNode<T> p=list.head.next;
DLinkNode<T> rear=this.head;
While(p!=list.head){
rear.pre.next = rear;
rear.next=new DLinkNode<T>(p.data,rear,this.head);
rear=rea.next;
p=p.next;
}
rear.pre.next = rear;
rear = this.head;
}写了下整个代码,请参考:public class CirDoublyLinkedList<T> {
DLinkNode<T> head = new DLinkNode<T>(null, null, null);
DLinkNode<T> end; public CirDoublyLinkedList() {
head.pre = head;
head.next = head;
end = head;
} public CirDoublyLinkedList(CirDoublyLinkedList<T> list) {
this();
DLinkNode<T> p = list.head.next;
DLinkNode<T> rear = this.head;
while (p != list.head) {
rear.pre.next = rear; rear.next = new DLinkNode<T>(p.data, rear, this.head);
rear = rear.next;
p = p.next;
}
rear.pre.next = rear;
rear = this.head;
} public void addData(T data) {
DLinkNode<T> newNode = new DLinkNode<T>(data, end, head);
end.next = newNode;
head.pre = newNode;
end = newNode;
} public void printThis() {
DLinkNode<T> p = head.next;
System.out.println("-------CirDoublyLinkedList Begin--------");
while (p != head) {
System.out.println("data:" + p.data + ", " + "pre:" + p.pre + ", "
+ "next:" + p.next);
p = p.next;
} System.out.println("-------CirDoublyLinkedList end--------");
} public class DLinkNode<T> {
T data;
DLinkNode<T> pre;
DLinkNode<T> next; public DLinkNode(T data, DLinkNode<T> pre, DLinkNode<T> next) {
super();
this.data = data;
this.pre = pre;
this.next = next;
} @Override
public String toString() {
if (null == data) {
return "head";
} else {
return this.data.toString();
}
} } public static void main(String[] args) {
CirDoublyLinkedList<Integer> CirDoublyLinkedList1 = new CirDoublyLinkedList<Integer>();
CirDoublyLinkedList1.addData(1);
CirDoublyLinkedList1.addData(2);
CirDoublyLinkedList1.addData(3);
CirDoublyLinkedList1.addData(4);
CirDoublyLinkedList1.addData(5); CirDoublyLinkedList<Integer> CirDoublyLinkedList2 = new CirDoublyLinkedList<Integer>(
CirDoublyLinkedList1); CirDoublyLinkedList1.printThis();
CirDoublyLinkedList2.printThis();
}
}
测试结果:
-------CirDoublyLinkedList Begin--------
data:1, pre:head, next:2
data:2, pre:1, next:3
data:3, pre:2, next:4
data:4, pre:3, next:5
data:5, pre:4, next:head
-------CirDoublyLinkedList end--------
-------CirDoublyLinkedList Begin--------
data:1, pre:head, next:2
data:2, pre:1, next:3
data:3, pre:2, next:4
data:4, pre:3, next:5
data:5, pre:4, next:head
-------CirDoublyLinkedList end--------
我仔细看了下,但是还是想不通为啥while循环之后还要加上
rear.pre.next=rear
rear=this.head
rear.pre.next=rear
rear.next.next=this.head