问题遇到的现象和发生背景
import java.util.Scanner;
public class shiyan3 {
public static void main(String[] args) {
need<String> h = new need<String>();
System.out.println("初始化循环双链表h");
System.out.println("依次采用尾插法插入a,b,c,d,e元素");
h.appendLast("a");
h.appendLast("b");
h.appendLast("c");
h.appendLast("d");
h.appendLast("e");
System.out.println("输出循环双链表h"+h);
System.out.println("循环双链表的长度为"+h.GetSize());
System.out.println("循环双链表h为"+h.IsEmpty());
System.out.println("循环双链表h的第3个元素为"+h.GetElem(3));
System.out.println("元素a的位置"+h.GetElemPos("a"));
System.out.println("在第四个元素上插入f元素");
System.out.println("输出循环双链表h"+h.Insert(4, f));
System.out.println("删除循环链表的第3个元素");
System.out.println("输出循环链表h"+h.Delete(2));
}
}
public class need<T> {
public DNode<T> head;
private int Count;
class DNode<T>{
private DNode pre;
private DNode next;
private T value;
public DNode(T value, DNode pre, DNode next) {
this.next = next;
this.pre = pre;
this.value = value;
}
}
public need() {
head = new DNode<T>(null, null, null);
head.next = head.pre = head;
Count = 0;
}
public void DLinkListClass() {
}
public void appendLast(T t) {
DNode<T> node = new DNode<T>(t, head.pre,head);
head.pre.next = node;
head.pre = node;
Count++;
}
public void CreateListR(String[] b) {
System.out.println("");
}
public void show() {
for(int i = 1; i <= Count; i++) {
System.out.println(getNode(i).value);
}
}
public int GetSize() {
return Count;
}
public boolean IsEmpty() {
return Count == 0;
}
public String GetElem(int i) {
return (String) getNode(i).value;
}
public void Delete(int index) {
DNode<T> node = getNode(index);
node.next.pre = node.pre;
node.pre.next = node.next;
node = null;
Count--;
}
public need<T>.DNode<T> getNode(int i2) {
DNode<T> node = head.next;
for(int i = 1; i < i2; i++) {
node = node.next;
}
return node;
}
public need<T>.DNode<T> getnode(String a) {
DNode<T> node = head.next;
for(int i = 1; i < a.length(); i++) {
node = node.next;
}
return node;
}
public int GetElemPos(String a) {
return (int) getnode(a).value;
}
public void Insert(int index,T t) {
if( index== 1) {
DNode<T> tnode = new DNode<T>(t, head, head.next);
head.next.pre = tnode;
head.next = tnode;
Count++;
return;
}
DNode<T> inode = getNode(index);
DNode<T> tnode = new DNode<T>(t, inode.pre, inode);
inode.pre.next = tnode;
inode.pre = tnode;
Count++;
return ;
}
}