北工大咸鱼 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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分