Basic operations on Singly linked list
package Linked_List;
class Node
{
int data;
Node next;
public Node(int a)
{
this.data = a;
this.next = null;
}
public Node()
{}
}
public class LinkedList
{
Node head;
int size=0;
public void push(int a)
{
Node node = new Node(a);
if(head == null)
head = node;
else
{
Node temp = head;
for(temp=head; temp.next != null; temp = temp.next)
{}
temp.next = node;
}
size++;
}
public int size()
{
Node temp = this.head.next;
int i = 1;
while(temp != head && temp != null)
{
temp = temp.next;
i++;
}
return i;
}
}
Comments
Post a Comment