1
1
package collection ;
2
2
3
- import static org .junit .jupiter .api .Assertions .assertTrue ;
4
-
5
- import org .junit .jupiter .api .Test ;
6
-
7
3
public class LinkedList <T > {
8
4
9
5
private Node <T > head ;
6
+ private Node <T > tail ;
10
7
11
8
public LinkedList () {
9
+ head = null ;
10
+ tail = null ;
11
+ }
12
12
13
+ public void add (T data ) {
14
+ Node <T > node = new Node <T >(data , null , null );
15
+ Node <T > current =null ;
16
+ if (head == null && tail == null ) {
17
+ head = node ;
18
+ tail = node ;
19
+ head .setPrevious (null );
20
+ head .setNext (null );
21
+ tail .setNext (null );
22
+ tail .setPrevious (null );
23
+
24
+ } else {
25
+ tail =node ;
26
+ current =head ;
27
+ while (current .getNext ()!=null ) {
28
+ current =current .getNext ();
29
+ }
30
+ current .setNext (tail );
31
+ tail .setPrevious (current );
32
+ }
13
33
}
14
34
15
35
public String toString () {
16
36
StringBuffer toReturn = new StringBuffer ("[" );
17
- Node <T > tempHead =head ;
18
- if (tempHead != null ) {
19
- toReturn .append (tempHead .getElement ());
20
- while (tempHead .getNext () != null ) {
21
- tempHead = tempHead .getNext ();
22
- toReturn .append (", " ).append (tempHead .getElement ());
37
+ Node <T > curr = head ;
38
+ while (curr !=null ) {
39
+ toReturn .append (curr .getData ()).append (" , " );
40
+ curr =curr .getNext ();
23
41
24
- }
25
42
}
26
43
toReturn .append ("]" );
27
44
return toReturn .toString ();
28
45
}
29
46
30
- public Node <T > getHead () {
31
- return head ;
32
- }
47
+ private class Node <T > {
33
48
34
- public void setHead ( Node < T > head ) {
35
- this . head = head ;
36
- }
49
+ private T data ;
50
+ private Node < T > previous ;
51
+ private Node < T > next ;
37
52
38
- public T add (T data ) {
39
- if (data ==null ) {
40
- return null ;
53
+ public Node (T data , Node <T > previous , Node <T > next ) {
54
+ super ();
55
+ this .data = data ;
56
+ this .previous = previous ;
57
+ this .next = next ;
41
58
}
42
- Node <T > n = new Node <T >(data , null );
43
- Node <T > tempHead = head ;
44
- if (tempHead == null ) {
45
- tempHead = n ;
46
- head = tempHead ;
47
- } else {
48
- while (tempHead .getNext () != null ) {
49
- tempHead = tempHead .getNext ();
50
- }
51
59
52
- tempHead .setNext (n );
60
+ public T getData () {
61
+ return data ;
53
62
}
54
- return data ;
55
- }
56
-
57
- public T remove (T data ) {
58
-
59
- Node <T > tempHead = head ;
60
- Node <T > previous = head ;
61
-
62
- if (tempHead == null ) {
63
- return null ;
64
- } else {
65
63
66
- while (tempHead != null ) {
67
- if (tempHead .getElement ().equals (data )) {
68
- previous .setNext (tempHead .getNext ());
69
- break ;
70
- }
71
- previous = tempHead ;
72
- tempHead = tempHead .next ;
73
- }
64
+ public void setData (T data ) {
65
+ this .data = data ;
74
66
}
75
- return data ;
76
- }
77
-
78
- @ Test
79
- public void test () {
80
- LinkedList <String > list = new LinkedList <>();
81
- System .out .println (list );
82
-
83
- System .out .println ("-----------------------------" );
84
67
85
- assertTrue ("Hello" .equalsIgnoreCase (list .add ("Hello" )));
86
- assertTrue ("Saty" .equalsIgnoreCase (list .add ("Saty" )));
87
- assertTrue ("vir" .equalsIgnoreCase (list .add ("vir" )));
88
- assertTrue (null ==list .add (null ));
89
-
90
- assertTrue ("Man" .equalsIgnoreCase (list .add ("Man" )));
91
-
92
-
93
-
94
- System .out .println (list );
95
-
96
- System .out .println ("-----------------------------" );
97
- assertTrue ("vir" .equalsIgnoreCase (list .remove (("vir" ))));
98
- System .out .println (list );
99
-
100
- }
101
-
102
- private class Node <T > {
103
- private T element ;
104
- private Node <T > next ;
105
-
106
- public T getElement () {
107
- return element ;
68
+ public Node <T > getPrevious () {
69
+ return previous ;
108
70
}
109
71
110
- public void setElement ( T element ) {
111
- this .element = element ;
72
+ public void setPrevious ( Node < T > previous ) {
73
+ this .previous = previous ;
112
74
}
113
75
114
76
public Node <T > getNext () {
@@ -119,11 +81,20 @@ public void setNext(Node<T> next) {
119
81
this .next = next ;
120
82
}
121
83
122
- public Node (T element , Node <T > next ) {
123
- this .element = element ;
124
- this .next = next ;
125
- }
126
-
84
+
85
+ }
86
+
87
+ public static void main (String [] args ) {
88
+ LinkedList <String > list =new LinkedList <>();
89
+ System .out .println (list );
90
+ list .add ("Hello" );
91
+ list .add ("ajay" );
92
+ list .add ("jay" );
93
+ list .add ("vijay" );
94
+ list .add ("Sanjay" );
95
+ System .out .println ("========================" );
96
+ System .out .println (list );
97
+
98
+
127
99
}
128
-
129
100
}
0 commit comments