In: Computer Science
Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive method addOddNodes for the LinkedList class which represents singly linked lists.
public class LNode
{
private int m_info;
private LNode m_link;
public LNode(int info){
m_info = info;
m_link = null;
}
public void setLink(LNode link){
m_link = link;
}
public LNode getLink(){
return m_link;
}
public void setInfo(int info){
m_info = info;
}
public int getInfo(){
return m_info;
}
}
public class LinkedList
{
private LNode head; // a reference to the first
node
// in the list
public LinkedList()
{
head = null;
}
// This non-recursive method traverses the list and
// count the total number of nodes and then save it
// in the last node. You can assume that the list has
// at least one node.
// You will receive zero points if recursion is
used.
// For example, the following linked list on the left
// has 3 nodes, so after this method is executed, the
// list will be updated to the one on the right.
public void saveCountinLastNode()
{
// TODO: implement this method
}
// This recursive method computes the sum of all the nodes
// with odd values.
// You will receive zero points if any looping
statements
// are used.
public int addOddNodes(LNode node)
{
// TODO: implement this method
}
}
- I have included
a main method just for testing. Please remove it.
Kindly upvote if this helped
Sample output
CODE:
class LNode {
private int m_info;
private LNode m_link;
public LNode(int info) {
m_info = info;
m_link = null;
}
public void setLink(LNode link) {
m_link = link;
}
public LNode getLink() {
return m_link;
}
public void setInfo(int info) {
m_info = info;
}
public int getInfo() {
return m_info;
}
}
public class LinkedList {
static int sum = 0;
private LNode head; // a reference to the first node
// in the list
public LinkedList() {
head = null;
}
// This non-recursive method traverses the list and
// count the total number of nodes and then save it
// in the last node. You can assume that the list has
// at least one node.
// You will receive zero points if recursion is used.
// For example, the following linked list on the left
// has 3 nodes, so after this method is executed, the
// list will be updated to the one on the right.
public void saveCountinLastNode() {
LNode current = head;
LNode temp;
int count = 0;
while (current != null) {
count++;
if (current.getLink() == null) {
LNode newNode = new LNode(count);
current.setLink(newNode);
break;
}
current = current.getLink();
}
}
// This recursive method computes the sum of all the nodes
// with odd values.
// You will receive zero points if any looping statements
// are used.
public int addOddNodes(LNode node) {
if (node != null) {
if (node.getInfo() % 2 != 0) {
//it is odd value
sum += node.getInfo(); // have to take a static variable sum as in recursion it will get initialized to default value if we do not take static
}
addOddNodes(node.getLink());
}
return sum;
}
public void dis() {
LNode current = head;
while (current != null) {
System.out.print(current.getInfo() + " ");
current = current.getLink();
}
}
public static void main(String[] args) {
//LNode ll = new LNode(3);
LinkedList l = new LinkedList();
l.head = new LNode(3);
l.saveCountinLastNode();
l.dis();
int sum = l.addOddNodes(l.head);
System.out.println("Sum is " + sum);
}
}