微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

一种递归算法,用于添加非虚拟标题单链接线性列表的所有元素仅列表的开头将作为参数给出

如何解决一种递归算法,用于添加非虚拟标题单链接线性列表的所有元素仅列表的开头将作为参数给出

添加单链列表的元素时,我遇到了运行时错误。我已经enter image description here安装了错误图片。我在这里附上我的代码

    public class Node{
      Node next;
      int a=0;
    
        public Node (int e,Node n){
          a =e;
          next =n;
      }}
    
    
    public class Question6{
      static int sum=0;
      public static void main(String[]args){
    
        Node head=null;
        
        Node n5= new Node(5,null);
        Node n4= new Node(4,n5);
        Node n3= new Node(3,n4);
        Node n2= new Node(2,n3);
        Node n1= new Node(1,n2);
        
        head=n1;
        sum =add(head);   
      }
      public static int add(Node head){
        if(head==null){
          return sum;
        }
        else{
          sum=sum + head.a;
          head=head.next;
          add(head);
        }
        return sum;
      }
    }

解决方法

您的编译器似乎有问题。图像中的错误表明它无法在类Node中找到init方法。 尝试在类Node中添加空的init方法(花括号):

class Node{
        Node next;
        int a = 0;

        {}

        public Node(int e,Node n) {
            a = e;
            next = n;
        }
}
,

首先,由于您使用class SizeWidget extends StatefulWidget { @override _SizeWidgetState createState() => _SizeWidgetState(); } class _SizeWidgetState extends State<SizeWidget> { final GlobalKey _textKey = GlobalKey(); Size textSize; @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) => getSizeAndPosition()); } getSizeAndPosition() { RenderBox _cardBox = _textKey.currentContext.findRenderObject(); textSize = _cardBox.size; setState(() {}); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Size Position"),),body: Column( mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.stretch,children: <Widget>[ Text( "Currern Size of Text",key: _textKey,textAlign: TextAlign.center,style: TextStyle(fontSize: 22,fontWeight: FontWeight.bold),SizedBox( height: 20,Text( "Size - $textSize",],); } } ,因此您的代码不是real递归。

external static variable

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。