linkedlistnode java_java linkedlist node 问题

a)createamethodcalledsuminclassNode(anyoftheNodeclassesthatwehavecreated),thatwillrecursivelycalculatethesumofalloftheintdatavaluesfromthatnodetothelastnodeinthelist.(HIN...

a) create a method called sum in class Node (any of the Node classes that we have created), that will recursively calculate the sum of all of the int data values from that node to the last node in the list. (HINT: using the next pointer) This method should return (NOT PRINT OUT) the sum that was calculated.

b) IN CLASS LINKED LIST, create a method called sum that will take an int, n, as a parameter. It will call method sum (of class Node) on the nth node in the list. (the head is node 1, the node after head is 2, etc.) And then print out to a file the sum that was calculated. If the parameter passed is negative or zero, then the sum should be zero. If n is greater than the number of nodes in the list, then throw an appropriate exception.

我在class Node里面怎么怎么写sum method,才能在class linkedlist,里面用啊?因为Node里面的sum是计算全部的,而linkedlist里面是计算到第n个的,下面是Node 和linkedlist class,求各位学长大神教教我

publicclass Node {

privateintdata;

private Node next;

public Node(intd){

data = d;

}

public Node(intd, Node n){

data = d;

next = n;

}

publicint getData() {returndata; }

public Node getNext() {returnnext; }

publicvoid setNext(Node n) {next = n;}

}

publicclass LinkedList {

private Node head;

public LinkedList(Node n){

head =n;

}

public LinkedList(intx){

head = new Node(x);

}

publicvoid append(intx) {

Node current = head;

while (current.getNext()!=null){

current = current.getNext();

}

current.setNext(new Node(x));

}

publicvoid insert(intx) {

if(head.getData()>x){ //case 1: insert before head

head = new Node(x, head);

} else {

Node current = head;

Node previous = head;

while (current!=null){

if (current.getData()>x){ // case 2: insert into the middleof the list

previous.setNext(new Node(x, current));

// Node temp= new Node(x, current);

// previous.setNext(temp);

break;

} else {

previous = current;

current = current.getNext();

} // else

} // end ofwhile

if(current == null){ // case 3: insert after list

previous.setNext(new Node(x));

} // if

} // else

} // insertmethod

publicboolean delete(intx){

if(x==head.getData()){

head = head.getNext();

returntrue;

}

Node current = head;

Node previous = head;

while (current!=null){

if (current.getData()==x){ // case 2: delete from the middle or from the end

previous.setNext(current.getNext());

returntrue;

} elseif(current.getData()>x) {

returnfalse;

} else {

previous = current;

current = current.getNext();

}

}// while

returnfalse;

} // deletemethod

}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值