In: Computer Science
Count all nonzero elements, odd numbers and even numbers in a linked list.
Code needed in java through single linked list.
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;
}
}