-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinked.java
More file actions
185 lines (168 loc) · 3.7 KB
/
SinglyLinked.java
File metadata and controls
185 lines (168 loc) · 3.7 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
class Node {
Node next;
int data;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class Linked {
Node head;
public Linked() {
this.head = null;
}
public void insertFirst(int data) {
Node newnode = new Node(data);
if(this.head == null){
head = newnode;
return;
}
newnode.next = head;
head = newnode;
}
public void insertPos(int data, int pos) {
Node newnode = new Node(data);
Node tempo;
int i;
for(i = 0, tempo = this.head; tempo != null && i < pos-2; tempo = tempo.next, i++);
newnode.next = tempo.next;
tempo.next = newnode;
}
public void insertLast(int data) {
Node newnode = new Node(data);
if(this.head == null){
head = newnode;
return;
}
Node current;
for(current = this.head; current.next != null; current = current.next);
current.next = newnode;
}
public boolean isEmpty() {
if(this.head == null)
return true;
else
return false;
}
public void isLastElement(int d) {
Node curren;
for(curren = this.head; curren.next != null; curren = curren.next);
if (curren.data == d)
System.out.println("YES");
else
System.out.println("NO");
}
public void deleteFront() {
Node t = head;
head = head.next;
t.next = null;
}
public void deleteLast() {
Node tem;
for(tem = this.head; tem.next.next != null; tem = tem.next);
tem.next = null;
}
public void deletePos (int p) {
Node curr;
int i;
for (i = 0, curr = this.head; curr.next.next != null && i < p-1; curr = curr.next, i++);
curr.next = curr.next.next;
}
public void print() {
Node temp;
for(temp = this.head; temp != null; temp = temp.next)
System.out.println(temp.data);
}
public void sortedInsert(int data)
{
Node newnode = new Node(data);
Node current;
if (head == null || head.data >= newnode.data)
{
newnode.next = head;
head = newnode;
}
else {
current = head;
while (current.next != null &&
current.next.data < newnode.data)
current = current.next;
newnode.next = current.next;
current.next = newnode;
}
}
public void delData(int d) {
Node crnt;
int i = 0;
for(crnt = this.head; crnt != null; crnt = crnt.next) {
if(crnt.data == d) {
i++;
deletePos(i);
}
}
}
public void printEven() {
Node temp = this.head.next;
System.out.println(temp.data);
while(temp.next != null) {
temp = temp.next.next;
System.out.println(temp.data);
}
}
}
class SinglyLinked {
public static void main(String[] args) {
Linked l=new Linked();
l.insertFirst(1);
l.insertFirst(2);
l.insertFirst(30);
l.insertFirst(60);
l.insertFirst(40);
l.insertFirst(100);
l.print();
System.out.println("Insert at position");
l.insertPos(20,2);
l.print();
System.out.println("Insert at last");
l.insertLast(18);
l.insertLast(10);
l.print();
boolean status =l.isEmpty();
if (status==true)
System.out.println("Empty");
else
System.out.println("Not Empty");
System.out.println("Delete at last");
l.deleteLast();
l.print();
System.out.println("Delete at front");
l.deleteFront();
l.print();
l.isLastElement(2);
System.out.println("Delete at pos");
l.deletePos(2);
l.print();
System.out.println("Deleting last 4 elements");
l.deleteLast();
l.deleteLast();
l.deleteLast();
l.deleteLast();
l.print();
System.out.println("Insert");
l.sortedInsert(30);
l.print();
System.out.println("Inserted 10");
l.insertPos(10,2);
l.print();
System.out.println("Delete 10");
l.delData(10);
l.print();
System.out.println("Insert");
l.insertLast(50);
l.insertLast(60);
l.insertLast(70);
l.print();
System.out.println("Print Even");
l.printEven();
}
}