Question

In: Computer Science

Count all nonzero elements, odd numbers and even numbers in a linked list. Code needed in...

Count all nonzero elements, odd numbers and even numbers in a linked list.

Code needed in java through single linked list.

Solutions

Expert Solution

I have implemented SingleLinkedList class to count the required statistics.
PLEASE FIND THE FOLLOWING CODE SCREENSHOT, OUTPUT, AND CODE.

ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT

1.CODE SCREENSHOT:

2.OUTPUT:

3.CODE :

SingleLinkedList.java

public class SingleLinkedList {    
        
     
    //Represent the head and tail of the singly linked list    
    public Node head ;    
    public Node tail ;    
    public SingleLinkedList (){
                head = null;
                tail=null;
        }
        //Insert the element at a spefied position
        //If the number of element is less than pos then
        //we insert at tail
        public void addAt(int pos,int data){
                //Create a new node    
        Node newNode = new Node(data); 
                
                //Checks if the list is empty    
        if(head == null) {    
            //If list is empty, both head and tail will point to new node    
            head = newNode;    
            tail = newNode;    
        }    
        else {    
                        Node temp=head,p=head;
                        while(pos>0&&temp.next!=null){
                                
                                p=temp;
                                temp=temp.next;         
                                pos--;
                        }               
                        
                        if(temp.next==null){
                                tail.next = newNode;    
            //newNode will become new tail of the list    
            tail = newNode;   
                        }
                        else{
                        newNode.next=temp;
                        p.next=newNode;
                        } 
                
        }    
                
        }
        //Insert the element at the begining of the list
        public void addFirst(int data) {    
        //Create a new node    
        Node newNode = new Node(data);    
            
        //Checks if the list is empty    
        if(head == null) {    
            //If list is empty, both head and tail will point to new node    
            head = newNode;    
            tail = newNode;    
        }    
        else {    
            //newNode will be added after head such that head will point by newNode    
            newNode.next=head;    
            //newNode will become new head of the list    
            head = newNode;    
        }    
    }
    //insert the element at end of the list 
    public void addLast(int data) {    
        //Create a new node    
        Node newNode = new Node(data);    
            
        //Checks if the list is empty    
        if(head == null) {    
            //If list is empty, both head and tail will point to new node    
            head = newNode;    
            tail = newNode;    
        }    
        else {    
            //newNode will be added after tail such that tail's next will point to newNode    
            tail.next = newNode;    
            //newNode will become new tail of the list    
            tail = newNode;    
        }    
    }    
        
    //display() will display all the nodes present in the list    
    public void display() {    
        //Node current will point to head    
        Node current = head;    
            
        if(head == null) {    
            System.out.println("List is empty");    
            return;    
        }    
        System.out.println("Nodes of singly linked list: ");    
        while(current != null) {    
            //Prints each node by incrementing pointer    
            System.out.print(current.data + " ");    
            current = current.next;    
        }    
        System.out.println();    
    }    
    //
        public void printStats(){
                int zeros=0,nonzeros=0,even=0,odd=0;
                //Node current will point to head    
        Node current = head;       
        if(head == null) {    
            System.out.println("List is empty");    
            return;    
        }    
        System.out.println("");    
        while(current != null) {    
                        if(current.data==0)
                                zeros++;
                        else{
                                nonzeros++;
            if(current.data%2==0)
                                even++;
                        else
                                odd++;
                        }
            current = current.next;    
        }    
        System.out.println("Number of Zeros : "+zeros);
                System.out.println("Number of Non Zero Elements : "+nonzeros);
                System.out.println("Number of Even Numbers : "+even);
                System.out.println("Number of Odd Numbers : "+odd);
                
                
        }
    public static void main(String[] args) {    
            
        SingleLinkedList sList = new SingleLinkedList();    
            
        //Add nodes to the list   
                
        sList.addLast(1);    
        sList.addFirst(2);    
        sList.addFirst(3);    
                sList.addAt(2,6);
                sList.addAt(2,7);
        sList.addLast(4);    
        sList.addAt(10,9);
        //Displays the nodes present in the list    
        sList.display();    
                sList.printStats();
    }    
}    

Node.java


 public class Node{    
        int data;    
        Node next;    
            
        public Node(int data) {    
            this.data = data;    
            this.next = null;    
        }    
    }



Related Solutions

Count all nonzero elements, odd numbers and even numbers in a linked list. Code needed in...
Count all nonzero elements, odd numbers and even numbers in a linked list. Code needed in java.
Write a Python function which finds the smallest even and odd numbers in a given list....
Write a Python function which finds the smallest even and odd numbers in a given list. (Use for loop and if conditions for this problem)
C++ CODE TO FIND ALL Odd numbers between 1 and 70000.
C++ CODE TO FIND ALL Odd numbers between 1 and 70000.
I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
method to remove all elements frrom my linked list (java)
method to remove all elements frrom my linked list (java)
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Find the largest and smallest element of a linked list, print total of all elements and...
Find the largest and smallest element of a linked list, print total of all elements and find out the average. Code needed in java
Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
In Python, Given a list of numbers, return a list where all adjacent == elements have...
In Python, Given a list of numbers, return a list where all adjacent == elements have been reduced to a single element, so [1,2,2,3,3,2,2,4] returns [1,2,3,2,4]. You may create a new list or modify the passed in list (set function does not work in this case).
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT