Question

Write an efficient java private int countodds (node t) method that returns the number of odd...

Write an efficient java private int countodds (node t) method that returns the number of odd integers in a singly linked list headed by node t. (

Assume class node is defined as:       private class node {int data; node rlink;}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

private int countodds(node t) {
    int count = 0;
    while (t != null) {
        if (t.data % 2 == 1)
            ++count;
        t = t.rlink;
    }
    return count;
}
Add a comment
Know the answer?
Add Answer to:
Write an efficient java private int countodds (node t) method that returns the number of odd...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java The following Java implementation of a class Node is given: private class Node<Object> {...

    In Java The following Java implementation of a class Node is given: private class Node<Object> { Node() { this(null, null); } Node(Object d) { this(d, null); } Node(Object d, Node n) { data = d; next = n; } Object data; Node next; } Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a reference to the header node. Using the class Node described above, write a MySingleLinkedList...

  • Language: Java Topic: Doubly Linked Lists Write a method that removes and returns the last copy...

    Language: Java Topic: Doubly Linked Lists Write a method that removes and returns the last copy of the given data from the list. Do not return the same data that was passed in. Return the data that was stored in the list. Must be O(1) if data is in the tail and O(n) for all other cases.    * @param data the data to be removed from the list * @return the data that was removed * @throws java.lang.IllegalArgumentException if...

  • You are given a Node class and a List class: Write a java method insertNodeBefore() which...

    You are given a Node class and a List class: Write a java method insertNodeBefore() which takes two integers (int ref which is the data of the node that we need to insert a new node before, and int d which is the data of the new node we want to insert), for the list mylist.

  • Java Write the function void insertAtTail (int v). Don’t add any class variables to the List...

    Java Write the function void insertAtTail (int v). Don’t add any class variables to the List class. Here are the class definitions of Node and List that implement a linked list. class Node {private Node next; private int key; Node (Node nxt, int keyValue);//constructor Node getNext(); int getKey(); void putNext(Node nxt);} class List {//assume the class does not use a dummy Node private Node head; List ();//constructor boolean exists (int ky);//returns true if v is in the list void insertAtHead(int...

  • Using Java You are given a Node class and a List class: public class Node {...

    Using Java You are given a Node class and a List class: public class Node {    int   data;     Node next; } public class List {     Node first; } You are also given a Stack class. The following functions are available for use: public class Stack { public boolean isEmpty(){}; public void push(int n){}; public int pop(){};} Write a Java method snglyRevToStck that pushes the data found in a linked list t in reverse order into the stack...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • Linked List in Java The data node should be modeled somewhat like this: class node{ int...

    Linked List in Java The data node should be modeled somewhat like this: class node{ int node iNum; node next; } Write a program that creates a linked list and loads it with the numbers 0 to 9. Start with an empty list and then use a "for loop" to fill it. Create a linked list class, a node class, etc. Routines like makeNode and findTail should be methods in the linked list class. Create a showList function to display...

  • Write a method max() that takes a reference to the first node in a linked list...

    Write a method max() that takes a reference to the first node in a linked list as argument and returns the value of the maximum key in the list. Assume that all keys are positive integers, and return 0 if the list is empty. In java

  • Write a java code : Student ID at University is composed of year of admission and...

    Write a java code : Student ID at University is composed of year of admission and students number. we aim to implement a structure that improves operations of inserting and searching for a student. To enhance these operations, we will build a tree of linked lists (TreeOfLists) to combine advantages of both structures. Each node in this tree contains a linked list of all students who were admitted in that year. Each node in the linked list represents a student(id,...

  • 9) Write out the following method as a static method for the IntNode class (similar to...

    9) Write out the following method as a static method for the IntNode class (similar to the book: use data & link) public static int count10s (IntNode head) I Precondition: Head is a reference to a linked list of integers, Il condition of the list is unknown (empty or not) // Postcondition: Method returns number of 10s found in the linked list. / list is unchanged. 10) Write out the following method as a static method for the IntNode class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT