From 650848c2f97e9c380904a9ff3a632cd8477368e0 Mon Sep 17 00:00:00 2001 From: Dinki Yaduwanshi <91734640+Dinki-Yaduwanshi@users.noreply.github.com> Date: Wed, 26 Oct 2022 09:12:31 +0530 Subject: [PATCH 1/3] NodeNumber.java A program to add nodes to a linked list and count the total number. --- Ds/LinkedList/NodeNumber.java | 69 +++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Ds/LinkedList/NodeNumber.java diff --git a/Ds/LinkedList/NodeNumber.java b/Ds/LinkedList/NodeNumber.java new file mode 100644 index 000000000..ebf9db1fc --- /dev/null +++ b/Ds/LinkedList/NodeNumber.java @@ -0,0 +1,69 @@ +public class NodeNumber { + class Node{ + Node next; + int data; + + Node(int d){ + this.data=d; + this.next=null; + } + } + + public Node head=null; + public Node tail=null; + + public void addNode(int data) { + Node newNode= new Node(data); + + if(head==null) { + head=newNode; + tail=newNode; + } + else { + tail.next=newNode; + tail=newNode;} + } + + +public int NodeNumber() { +int count=0; +//NOde current will point to head + +Node current=head; + +while(current!=null) { + //increment count by 1 +count++; +current=current.next; +} + +return count; + +} + +public void Display() { + Node current=head; + if(head==null) { + System.out.print("List is empty"); + return ; + } + System.out.print("Nodes in Linked LIst are as follows:"); + while(current!=null) { + //prints each node by incrementing pointer + System.out.print(current.data+" "); + current=current.next; + } + } + +public static void main(String args[]) { + NodeNumber list=new NodeNumber(); + list.addNode(12); + list.addNode(34); + list.addNode(45); + list.addNode(23); + + list.Display(); + System.out.println(); + System.out.print("Number of nodes are "+ list.NodeNumber()); +} +} From a51dcbd6c4e18a242f34ed39e3034f47ecbb1cbf Mon Sep 17 00:00:00 2001 From: Dinki Yaduwanshi <91734640+Dinki-Yaduwanshi@users.noreply.github.com> Date: Wed, 26 Oct 2022 09:14:11 +0530 Subject: [PATCH 2/3] Delete NodeNumber.java --- Ds/LinkedList/NodeNumber.java | 69 ----------------------------------- 1 file changed, 69 deletions(-) delete mode 100644 Ds/LinkedList/NodeNumber.java diff --git a/Ds/LinkedList/NodeNumber.java b/Ds/LinkedList/NodeNumber.java deleted file mode 100644 index ebf9db1fc..000000000 --- a/Ds/LinkedList/NodeNumber.java +++ /dev/null @@ -1,69 +0,0 @@ -public class NodeNumber { - class Node{ - Node next; - int data; - - Node(int d){ - this.data=d; - this.next=null; - } - } - - public Node head=null; - public Node tail=null; - - public void addNode(int data) { - Node newNode= new Node(data); - - if(head==null) { - head=newNode; - tail=newNode; - } - else { - tail.next=newNode; - tail=newNode;} - } - - -public int NodeNumber() { -int count=0; -//NOde current will point to head - -Node current=head; - -while(current!=null) { - //increment count by 1 -count++; -current=current.next; -} - -return count; - -} - -public void Display() { - Node current=head; - if(head==null) { - System.out.print("List is empty"); - return ; - } - System.out.print("Nodes in Linked LIst are as follows:"); - while(current!=null) { - //prints each node by incrementing pointer - System.out.print(current.data+" "); - current=current.next; - } - } - -public static void main(String args[]) { - NodeNumber list=new NodeNumber(); - list.addNode(12); - list.addNode(34); - list.addNode(45); - list.addNode(23); - - list.Display(); - System.out.println(); - System.out.print("Number of nodes are "+ list.NodeNumber()); -} -} From 2b44113a24853cdce9c30c0bad2f77c6ec26e68d Mon Sep 17 00:00:00 2001 From: Dinki Yaduwanshi <91734640+Dinki-Yaduwanshi@users.noreply.github.com> Date: Wed, 26 Oct 2022 09:15:28 +0530 Subject: [PATCH 3/3] NodeNumber.java Add new nodes and count total number in a linked list. --- Ds/LinkedList/NodeNumber.java | 69 +++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Ds/LinkedList/NodeNumber.java diff --git a/Ds/LinkedList/NodeNumber.java b/Ds/LinkedList/NodeNumber.java new file mode 100644 index 000000000..ebf9db1fc --- /dev/null +++ b/Ds/LinkedList/NodeNumber.java @@ -0,0 +1,69 @@ +public class NodeNumber { + class Node{ + Node next; + int data; + + Node(int d){ + this.data=d; + this.next=null; + } + } + + public Node head=null; + public Node tail=null; + + public void addNode(int data) { + Node newNode= new Node(data); + + if(head==null) { + head=newNode; + tail=newNode; + } + else { + tail.next=newNode; + tail=newNode;} + } + + +public int NodeNumber() { +int count=0; +//NOde current will point to head + +Node current=head; + +while(current!=null) { + //increment count by 1 +count++; +current=current.next; +} + +return count; + +} + +public void Display() { + Node current=head; + if(head==null) { + System.out.print("List is empty"); + return ; + } + System.out.print("Nodes in Linked LIst are as follows:"); + while(current!=null) { + //prints each node by incrementing pointer + System.out.print(current.data+" "); + current=current.next; + } + } + +public static void main(String args[]) { + NodeNumber list=new NodeNumber(); + list.addNode(12); + list.addNode(34); + list.addNode(45); + list.addNode(23); + + list.Display(); + System.out.println(); + System.out.print("Number of nodes are "+ list.NodeNumber()); +} +}