- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkList.java
More file actions
Latest commit
161 lines (122 loc) · 4.26 KB
/
Copy pathLinkList.java
File metadata and controls
161 lines (122 loc) · 4.26 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
//PROPERTIES OF LINKLIST
//1. variable sixe
//2. non-contiguous memory
//3. insert in 0(1)
//4. search in 0(n)
//NODE
// NODE store 2 information
//1. data...2. reference
//HEAD means ll ka pehta point amd tail means last elements
//type pf LL
//1. SINGULAR....2.DOUBLE ENDED...3.CIRCULAR ...
//BASICALLI INSERTION KE LITE LINKLIST USE KRNA CHAHIYE
publicclassLinkList {
nodehead;
privateintsize; //private size ka int banaya
LinkList(){
this.size =0; //size ko initialize 0 se kr diya ;;;
}
classnode { // node cclass bana li hamne
Stringdata;
nodenext;
node(Stringdata) { // CONSTRUCTOR bana liya
this.data = data;
this.next = null;
size++;
}
}
// ADD--1.FIRST...2.LAST
//add first
publicvoidaddfirst(Stringdata) {
nodenewnode = newnode(data); //ek newnode banaya
if (head == null) {
head = newnode;
return;
}
newnode.next = head; //head ko new node ke next me dal dila;;;matlab head ek kadam aage gaya
head = newnode; //badme new node ko hi head bana diya;;
}
// ADD LAST
publicvoidaddlast(Stringdata) {
nodenewnode = newnode(data);
if (head == null) {
head = newnode;
return;
}
nodecurrnode = head; //pehle currnode ko head banaya;;
while (currnode.next != null) { //jabtak currnode ka head null nahi hota
currnode = currnode.next; // currnode ka jo next hai usko currnode me dalneka
}
currnode.next = newnode; //currnpde ka next jo hai usko newnode bananeka;;
}
// print
publicvoidprintlist() {
if (head == null) {
System.out.println(" LL is empty");
return;
}
nodecurrnode = head;
while (currnode != null) {
System.out.print(currnode.data + "-->");
currnode = currnode.next;
}
System.out.println("null");
}
// DELETE
// first ko delete karna hai to first ke next ko head banana hoga
// last ko agr delete karna hai ..to next-next karna hai aur jaise hi second
// last node tak pohochenge .
// to second last node ko null bana lenge
// delete first
publicvoiddeletefirst() {
if (head == null) {
System.out.println("LL is empty");
return;
}
size--; //delete hone ke bad size ko kam karna padega
head = head.next; // head.next ko head banaya to head carbej ho gya
}
publicvoiddeletelast() {
if (head == null) {
System.out.println("LL is empty");
return;
}
size --; ////delete hone ke bad size ko kam karna padega
nodeseclast = head; // head ko sechead banaya
nodelastnode = head.next; // head next ko last node banaaya
if (head.next == null) { // jisme LL me ek hi node hoga //next karte wakt null ka next null nahi ho sakta
head = null;
return;
}
while (lastnode.next != null) { // jabtak last node null nahi ho jata
lastnode = lastnode.next; // lastnode next ko last node me dalneka
seclast = seclast.next; // aur seclast node ke next ko sec last me dalneka jo ki head banaya hai
}
seclast.next = null;
// lastnode = null;
}
publicintgetsize(){
returnsize;
}
publicstaticvoidmain(String[] args) {
LinkListlist = newLinkList();
list.addfirst("is");
list.addfirst("varad");
list.addfirst("a");
list.printlist();
list.addlast("good");
list.printlist();
list.addlast("but");
list.addlast("not");
list.addlast("not");
list.deletelast();
list.deletefirst();
list.deletelast();
list.deletelast();
list.printlist();
list.addfirst("a");
list.addlast("so");
list.printlist();
System.out.println(list.size);
}
}