Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b7c4613

Browse files
authoredJul 26, 2018
SingleLinkedList Implementation
SingleLinkedList Implementation with add , remove and traverse method
1 parent 2f9910c commit b7c4613

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
 

‎LinkedList.java

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package collection;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class LinkedList<T> {
8+
9+
private Node<T> head;
10+
11+
public LinkedList() {
12+
13+
}
14+
15+
public String toString() {
16+
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());
23+
24+
}
25+
}
26+
toReturn.append("]");
27+
return toReturn.toString();
28+
}
29+
30+
public Node<T> getHead() {
31+
return head;
32+
}
33+
34+
public void setHead(Node<T> head) {
35+
this.head = head;
36+
}
37+
38+
public T add(T data) {
39+
if(data==null) {
40+
return null;
41+
}
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+
52+
tempHead.setNext(n);
53+
}
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+
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+
}
74+
}
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+
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;
108+
}
109+
110+
public void setElement(T element) {
111+
this.element = element;
112+
}
113+
114+
public Node<T> getNext() {
115+
return next;
116+
}
117+
118+
public void setNext(Node<T> next) {
119+
this.next = next;
120+
}
121+
122+
public Node(T element, Node<T> next) {
123+
this.element = element;
124+
this.next = next;
125+
}
126+
127+
}
128+
129+
}

0 commit comments

Comments
 (0)
Please sign in to comment.