Skip to content

Commit 63cb144

Browse files
committed
Created ILinkedList interface
Fixed SingleLinkedList - inherit the interface - add remove at end, beginning, remove data. Fix insert at position
1 parent 92382aa commit 63cb144

File tree

2 files changed

+196
-70
lines changed

2 files changed

+196
-70
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package linkedList;
2+
3+
public interface ILinkedList {
4+
5+
/**
6+
* InsertNodeAtEnd
7+
* @param data - Data to be inserted
8+
* @return headNode or NULL if any error
9+
*/
10+
public ListNode InsertNodeAtEnd(int data) ;
11+
12+
/**
13+
* InsertAtTheBegining
14+
* @param data - Data to be inserted
15+
* @return headNode or NULL if error
16+
*/
17+
public ListNode InsertAtTheBegining(int data);
18+
19+
/**
20+
* InsertNodeAt
21+
* @param data - Data to be inserted
22+
* @param pos - Position in the Linked List
23+
* @return headNode or NULL if error
24+
*/
25+
public ListNode InsertNodeAt(int data, int pos);
26+
27+
/**
28+
* RemoveNode
29+
* @param data - Data to be Removed
30+
* @return headNode or NULL if error
31+
*/
32+
public ListNode RemoveNode(int data);
33+
34+
/**
35+
* RemoveNodeAtEnd
36+
* @param
37+
* @return headNode or NULL if error
38+
*/
39+
public ListNode RemoveNodeAtEnd();
40+
41+
/**
42+
* RemoveNodeAtBeginning
43+
* @param
44+
* @return headNode or NULL if error
45+
*/
46+
public ListNode RemoveNodeAtBeginning();
47+
48+
/**
49+
* RemoveNodeAt
50+
* @param pos - Position in the Linked List
51+
* @return headNode or NULL if error
52+
*/
53+
public ListNode RemoveNodeAt(int pos);
54+
55+
56+
/**
57+
* CreateNewNode
58+
* @param data -Data for the new node
59+
* @return
60+
*/
61+
public ListNode CreateNewNode(int data);
62+
63+
/**
64+
* length
65+
* @return length of linked list
66+
*/
67+
public int length();
68+
69+
/**
70+
* NodeAt
71+
* @param pos - Position
72+
* @return Node at requested Position
73+
*/
74+
public ListNode NodeAt(int pos);
75+
76+
/**
77+
* PrintLinkedList
78+
* Print the Linked List
79+
*/
80+
public void PrintLinkedList() ;
81+
82+
/**
83+
* IsEmpty
84+
* @return TRUE or FALSE
85+
*/
86+
public Boolean IsEmpty() ;
87+
88+
}
Lines changed: 108 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,66 @@
11
package linkedList;
22

33
/**
4-
* @author karri_000
4+
* @author ajkarri
5+
* @version 1.0.1
56
*
67
*/
7-
public class SingleLinkedList {
8+
public class SingleLinkedList implements ILinkedList {
89
ListNode headNode;
910
private int noOfNodes;
1011

1112
public SingleLinkedList(int data) {
1213
headNode = new ListNode(data);
13-
noOfNodes = 0;
14+
noOfNodes = 1;
1415
}
1516

16-
/**
17-
* InsertNodeAtEnd
18-
* @param data - Data to be inserted
19-
* @return headNode or NULL if any error
20-
*/
17+
2118
public ListNode InsertNodeAtEnd(int data) {
22-
if (headNode == null) {
23-
headNode = CreateNewNode(data);
24-
} else {
25-
ListNode currentNode = headNode;
26-
while (currentNode.getNext() != null){
27-
currentNode = currentNode.getNext();
28-
}
29-
currentNode.setNext(CreateNewNode(data));
30-
}
31-
32-
noOfNodes++;
33-
34-
return headNode;
19+
return InsertNodeAt(data,noOfNodes+1);
3520
}
3621

3722

38-
/**
39-
* InsertAtTheBegining
40-
* @param data - Data to be inserted
41-
* @return headNode or NULL if error
42-
*/
4323
public ListNode InsertAtTheBegining(int data) {
4424
return InsertNodeAt(data, 1);
4525
}
4626

47-
48-
49-
/**
50-
* InsertNodeAt
51-
* @param data - Data to be inserted
52-
* @param pos - Position in the Linked List
53-
* @return headNode or NULL if error
54-
*/
27+
5528
public ListNode InsertNodeAt(int data, int pos) {
5629

57-
if ((pos > noOfNodes) || (pos < 1)){
30+
if ((pos > noOfNodes+1) || (pos < 1)){
5831
System.out.println("Enter a valid position");
5932
return null;
6033
}
61-
if (pos == 1) {
34+
if (pos == 1) { // insert at beginning
6235
ListNode newNode = CreateNewNode(data);
6336
newNode.setNext(headNode);
6437
headNode = newNode;
65-
} else {
38+
} else { // insert in middle or end
6639
int currentPos = 1;
67-
ListNode currentNode = headNode;
68-
while ((currentNode != null)){
69-
if (currentPos == pos) {
70-
ListNode tempNode = currentNode.getNext();
71-
currentNode.setNext(CreateNewNode(data));
72-
currentNode.getNext().setNext(tempNode);
73-
break;
74-
}
75-
currentNode = currentNode.getNext();
40+
ListNode previousNode = headNode;
41+
while (currentPos < pos-1){
42+
43+
previousNode = previousNode.getNext();
7644
currentPos++;
7745
}
78-
}
46+
//System.out.println("CurrentNode Node " +previousNode.getData() + " ,Current Positon" +currentPos);
47+
ListNode tempNode = previousNode.getNext();
48+
previousNode.setNext(CreateNewNode(data));
49+
previousNode.getNext().setNext(tempNode);
7950

51+
}
52+
8053
noOfNodes++;
8154

8255
return headNode;
8356
}
8457

85-
/**
86-
* CreateNewNode
87-
* @param data -Data for the new ndoe
88-
* @return
89-
*/
90-
private ListNode CreateNewNode(int data) {
58+
59+
public ListNode CreateNewNode(int data) {
9160
return new ListNode(data);
9261

9362
}
94-
/**
95-
* length
96-
* @return length of linked list
97-
*/
63+
9864
public int length() {
9965
int length = 0;
10066
ListNode currentNode = headNode;
@@ -105,11 +71,6 @@ public int length() {
10571
return length;
10672
}
10773

108-
/**
109-
* NodeAt
110-
* @param pos - Position
111-
* @return Node at requested Position
112-
*/
11374
public ListNode NodeAt(int pos) {
11475
ListNode currentNode = headNode;
11576
if ((headNode == null)) {
@@ -132,10 +93,7 @@ public ListNode NodeAt(int pos) {
13293
return currentNode;
13394
}
13495

135-
/**
136-
* PrintLinkedList
137-
* Print the Linked List
138-
*/
96+
13997
public void PrintLinkedList() {
14098
ListNode currentNode = headNode;
14199
System.out.print(" Linked List:: ");
@@ -146,15 +104,95 @@ public void PrintLinkedList() {
146104
System.out.println();
147105
}
148106

149-
/**
150-
* IsEmpty
151-
* @return TRUE or FALSE
152-
*/
107+
153108
public Boolean IsEmpty() {
154109
if( length() == 0) {
155110
return true;
156111
}
157112

158113
return false;
159114
}
115+
116+
117+
@Override
118+
public ListNode RemoveNode(int data) {
119+
ListNode previousNode = headNode;
120+
Boolean found = false;
121+
122+
if ((headNode!=null) && (data == headNode.getData())) {
123+
headNode = headNode.getNext();
124+
found = true;
125+
previousNode = null;
126+
127+
} else {
128+
while ((previousNode !=null) && (previousNode.getNext() !=null)){
129+
//System.out.println("CurrentNode Node " +previousNode.getData() );
130+
131+
previousNode = previousNode.getNext();
132+
if (data == previousNode.getNext().getData()) {
133+
found = true;
134+
break;
135+
}
136+
}
137+
//System.out.println("CurrentNode Node " +previousNode.getData() );
138+
if (found) {
139+
ListNode tempNode = previousNode.getNext();
140+
previousNode.setNext(previousNode.getNext().getNext());
141+
tempNode = null;
142+
noOfNodes--;
143+
return headNode;
144+
}
145+
}
146+
147+
if(!found) {
148+
System.out.println("Didn't find data " + data + " in the linked list ... bool ");
149+
}
150+
return null;
151+
}
152+
153+
154+
@Override
155+
public ListNode RemoveNodeAt(int pos ) {
156+
if ((pos > noOfNodes+1) || (pos < 1)){
157+
System.out.println("Enter a valid position");
158+
return null;
159+
}
160+
if (pos == 1) { // insert at beginning
161+
ListNode currentNode = headNode;
162+
headNode = headNode.getNext();
163+
currentNode = null;
164+
} else { // insert in middle or end
165+
int currentPos = 1;
166+
ListNode previousNode = headNode;
167+
while (currentPos < pos-1){
168+
//System.out.println("CurrentNode Node " +previousNode.getData() + " ,Current Positon" +currentPos);
169+
previousNode = previousNode.getNext();
170+
currentPos++;
171+
}
172+
//System.out.println("CurrentNode Node " +previousNode.getData() + " ,Current Positon" +currentPos);
173+
ListNode tempNode = previousNode.getNext();
174+
previousNode.setNext(previousNode.getNext().getNext());
175+
tempNode = null;
176+
177+
}
178+
179+
noOfNodes--;
180+
181+
return headNode;
182+
183+
}
184+
185+
186+
@Override
187+
public ListNode RemoveNodeAtEnd() {
188+
// TODO Auto-generated method stub
189+
return RemoveNodeAt(noOfNodes);
190+
}
191+
192+
193+
@Override
194+
public ListNode RemoveNodeAtBeginning() {
195+
// TODO Auto-generated method stub
196+
return RemoveNodeAt(1);
197+
}
160198
}

0 commit comments

Comments
 (0)