DnodeList1:构造DoubleList
insert及remove的代码:
public void insertFront(int i) { if(size==0){ head=new DListNode1(i); tail=head; size=1; } else if(size==1){ head=new DListNode1(i); head.next=tail; tail.prev=head; size=2; } else{ DListNode1 node=new DListNode1(i); node.next=head; head.prev=node; head=node; size++; } } public void removeFront() { if(size==0) return; else{ head=head.next; size--; } }
运行结果:
DNodeList2: 构造circularList,这个在lab3中写过,重写一遍就当复习了吧:
public void insertFront(int i) { DListNode2 node=new DListNode2(i); node.next=head.next; node.prev=head; head.next=node; if(size==0) head.prev=node; size++; } public void removeFront() { if(size==0) return; else if(size==1){ head.next=head; head.prev=head; size=0; } else{ head.next.next.prev=head; head.next=head.next.next; size--; } }
运行结果:
时间: 2024-12-29 22:25:50