北工大咸鱼 2022-03-14 20:28 采纳率: 100%
浏览 10
已结题

h.Insert(4, f),Delete(2)为什么会报??

问题遇到的现象和发生背景
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 ;
    }
    
        
            
        
}
           
  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2022-03-14 21:08
    关注

    报错信息发一下。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月22日
  • 已采纳回答 3月14日
  • 创建了问题 3月14日

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示