Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NodeNumber.java #653

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Ds/LinkedList/NodeNumber.java
Original file line number Diff line number Diff line change
@@ -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());
}
}