-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList42.java
139 lines (114 loc) · 2.48 KB
/
LinkedList42.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class node
{
public int data;
public node next;
}
/*
struct node
{
int data;
struct node *next;
};
*/
class SinglyLL
{
public node First;
public int iCount;
public SinglyLL()
{
System.out.println("Object of SinglyLL gets created Successfully");
First = null;
iCount = 0;
}
public void InsertFirst(int iNo)
{
node newn = null;
newn = new node();
newn.data = iNo;
newn.next = null;
if(First == null)
{
First = newn;
}
else
{
newn.next = First;
First = newn;
}
iCount++;
}
public void Display()
{
System.out.println("Elements of the linked List are");
node temp = First;
while(temp != null)
{
System.out.print("| "+temp.data+" |->");
temp = temp.next;
}
System.out.println("null ");
}
public void InserLast(int iNo)
{
node newn = null;
newn = new node();
node temp = null;
newn.data = iNo;
newn.next = null;
if(First == null)
{
First = newn;
}
else
{
temp = First;
while (temp.next != null)
{
temp = temp.next;
}
temp.next = newn;
}
iCount++;
}
public int Count()
{
return iCount;
}
public void DeleteFirst()
{
if(First == null)
{
System.out.println("The Linked list is empty");
}
else
{
First = First.next;
}
iCount--;
}
}
class LinkedList42
{
public static void main(String Google[])
{
int iRet = 0;
SinglyLL obj = new SinglyLL();
obj.InsertFirst(101);
obj.InsertFirst(51);
obj.InsertFirst(21);
obj.InsertFirst(11);
obj.Display();
iRet = obj.Count();
System.out.println("The Number of elements in the Linked List are : " + iRet);
obj.InserLast(151);
obj.InserLast(111);
obj.InserLast(201);
obj.Display();
iRet = obj.Count();
System.out.println("The Number of elements in the Linked List are : " + iRet);
obj.DeleteFirst();
obj.Display();
iRet = obj.Count();
System.out.println("The Number of elements in the Linked List are : " + iRet);
}
}