-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked_List_operation.java
193 lines (155 loc) · 4.2 KB
/
Linked_List_operation.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
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
186
187
188
189
190
191
192
193
// This is a list
// ArrayList v/s Linked List
// ArrayList--->Dynamic array increase/decrease array size insert at any position
// Insert-->O(n) Search--->O(1)
// 2.LinkedList----->Non-Contiguous link with one two another temp,head,tail,null
// insert at any position--->using temp,head etc......,
// Insert----> O(1) Search--->O(n)
// 1.Variable Size---> Not Fixed
// Non-Contiguous Memory
// Insert----> O(1)
// Search--->O(n)
// NODE------> {[(data|next)]},
// Eg: {[(1|100)]} --------> {[(2|200)]} --------> {[(3|300)]}
// 1.Single_Linked-List 2.Double_Linked-List 3.Circular_Linked-List
// 1 OPERATIONS
//import java.util.LinkedList;
/*
class LiNKED_LiST{
Node head;
private int size;
// size
LiNKED_LiST(){
this.size = 0;
}
class Node{
String data;
Node next;
Node(String data){
this.data = data;
this.next = null;
size++;
}
}
//add------> first
public void addFirst(String data){
Node newNode = new Node(data);
if(head == null){
head = newNode;
return;
}
newNode.next = head;
head = newNode;
}
//add------> last
public void addLast(String data){
Node newNode = new Node(data);
if(head == null){
head = newNode;
return;
}
Node currNode = head;
while (currNode.next != null){
currNode = currNode.next;
}
currNode.next = newNode;
}
// del first
public void deleteFirst(){
if(head==null){
System.out.println("Empty");
return;
}
size--;
head = head.next;
}
// del last
public void deleteLast() {
if (head == null) {
System.out.println("Empty");
return;
}
size--;
if (head.next == null) {
head = null;
return;
}
// more than 2 recursion
Node secondLast = head;
Node lastNode = head.next;
while (lastNode.next != null) {
lastNode = lastNode.next;
secondLast = secondLast.next;
}
secondLast.next = null;
}
// print
public void printList(){
if(head==null){
System.out.println("List is empty");
return;
}
Node currNode = head;
while (currNode != null){
System.out.print(currNode.data+"-->");
currNode = currNode.next;
}
System.out.println("NULL");
}
//size
public int getSize(){
return size;
}
public static void main(String[] args) {
LiNKED_LiST list = new LiNKED_LiST();
//add
list.addFirst("LONI");
list.addFirst("NS");
list.printList();
System.out.println(list.getSize());
list.addFirst("NS "); // 2
// delete
list.deleteFirst();
list.printList(); //LONI-->NULL
list.deleteFirst();
list.printList(); // List is empty
//size
System.out.println(list.getSize());
list.addFirst("This is ");
}
}*/
//add
// OUTPUT: NS-->LONI-->NULL
//del
//OUTPUT:
//Frame_Work
// 2
import java.util.*;
class LiNKED_LiST{
public static void main(String[] args) {
LinkedList<String > list = new LinkedList<String>();
// 1.add
list.addFirst("AS");
list.addFirst("IS");
System.out.println(list); // [IS, AS]
list.addLast("LAST");
System.out.println(list); // [IS, AS, LAST]
// 2.size
System.out.println(list.size()); // 3
// 3.for
for(int i=0; i< list.size(); i++){
System.out.print(list.get(i)+"-->"); // IS-->AS-->LAST-->null
}
System.out.println("null");
// 4. remove
list.removeFirst();
System.out.println(list); // [AS, LAST]
list.removeLast();
System.out.println(list); // [AS]
/*list.removeLast();
System.out.println(list);*/ // []
// 5. add and remove at IDX
list.remove(1);
System.out.println(list);
}
}