forked from anku580/Java-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.java
More file actions
77 lines (69 loc) · 1.13 KB
/
SinglyLinkedList.java
File metadata and controls
77 lines (69 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public class SinglyLinkedList
{
class Node
{
int data;
Node next;
public Node(int data)
{
this.data=data;
this.next=null;
size++;
}
}
Node head;
Node tail;
int size;
public SinglyLinkedList()
{
head=null;
tail=null;
size=0;
}
public void InsertAtTail(int data)
{
Node node =new Node(data);
if(this.head==null)
{
this.head=node;
this.tail=node;
}
else
this.tail.next=node;
this.tail=node;
}
public void InstertAtHead(int data)
{
Node node=new Node(data);
node.next=head;
head=node;
if(this.tail==null)
this.tail=node;
}
Boolean isEmpty()
{
return head==null;
}
public void displayNodes()
{
if(this.isEmpty())
System.out.println("The list is empty");
Node temp=this.head;
while(temp.next!=null)
{
System.out.print(temp.data+"->");
temp=temp.next;
}
System.out.print("end");
}
public static void main(String[] args)
{
SinglyLinkedList l =new SinglyLinkedList();
l.InstertAtHead(5);
l.InsertAtTail(6);
l.InstertAtHead(4);
System.out.println("Size of Linked List is: "+l.size);
l.InsertAtTail(3);
l.displayNodes();
}
}