Created
May 3, 2018 05:29
-
-
Save ershad1/97760c5f4851fa77e4cb16cafd2a39f2 to your computer and use it in GitHub Desktop.
SinglyLinkList
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SinglyLinkList { | |
private Node start; | |
private Node end; | |
private int size; | |
// constructor | |
public SinglyLinkList() { | |
start = null; | |
end = null; | |
size = 0; | |
} | |
// append an element at beginning | |
public void insertAtStart(int val) { | |
Node nptr = new Node(val, null); | |
size++; | |
if (start == null) { | |
start = nptr; | |
end = start; | |
} else { | |
nptr.setNext(start); | |
start = nptr; | |
} | |
} | |
// append an element at end | |
public void insertAtEnd(int val) { | |
Node nptr = new Node(val, null); | |
size++; | |
if (start == null) { | |
start = nptr; | |
end = start; | |
} else { | |
end.setNext(nptr); | |
end = nptr; | |
} | |
} | |
// append an element at position | |
public void insertAtPos(int val, int pos) { | |
Node nptr = new Node(val, null); | |
Node ptr = start; | |
pos = pos - 1; | |
for (int i = 1; i < size; i++) { | |
if (i == pos) { | |
Node tmp = ptr.getNext(); | |
ptr.setNext(nptr); | |
nptr.setNext(tmp); | |
break; | |
} | |
ptr = ptr.getNext(); | |
} | |
size++; | |
} | |
// remove an element at position | |
public void deleteAtPos(int pos) { | |
if (pos == 1) { | |
start = start.getNext(); | |
size--; | |
return; | |
} | |
if (pos == size) { | |
Node s = start; | |
Node t = start; | |
while (s != end) { | |
t = s; | |
s = s.getNext(); | |
} | |
end = t; | |
end.setNext(null); | |
size--; | |
return; | |
} | |
Node ptr = start; | |
pos = pos - 1; | |
for (int i = 1; i < size - 1; i++) { | |
if (i == pos) { | |
Node tmp = ptr.getNext(); | |
tmp = tmp.getNext(); | |
ptr.setNext(tmp); | |
break; | |
} | |
ptr = ptr.getNext(); | |
} | |
size--; | |
} | |
// Remove the tail element | |
public void removeLast() { | |
if (end == null) | |
return; | |
else { | |
if (start == end) { | |
start = null; | |
end = null; | |
} else { | |
Node previousToTail = start; | |
while (previousToTail.getNext() != end) { | |
previousToTail = previousToTail.getNext(); | |
} | |
end = previousToTail; | |
end.setNext(null); | |
} | |
} | |
} | |
// Remove all element in the linked list that is great than a target value | |
public int removeAllBasedOnInputValue(int val) { | |
int counter = 0; | |
Node current = start; | |
//here we will go to the last node | |
while (current.getNext() != null) { | |
if (current.getData() > val) { | |
/* Here, we need to verify 3 things: | |
* 1 - If it is the start; | |
* 2 - If it is the end; and | |
* 3 - If it is the body. | |
*/ | |
/*1st verification - | |
If the start is bigger than your value, | |
then you just make your next node as "start", | |
and make its previous as NULL.*/ | |
if (current == start) { | |
start = current.getNext(); | |
}/*2nd verification - | |
If it is the last element, | |
then you just make the next node of your previous be NULL.*/ else if (current.getNext() == null) { | |
end = null; | |
}/*3rd verification - | |
You will make the next of the previous as your current next; | |
and the previous of the next as your current previous. | |
In that way you will lose all the ways of reaching the current | |
(which is greater than the value)*/ else { | |
current.next.next = current.next; | |
} | |
counter++; | |
} | |
current.setNext(current.getNext()); | |
} | |
return counter; | |
} | |
} | |
class Node { | |
protected int data; | |
protected Node next; | |
// Constructor | |
public Node() { | |
next = null; | |
data = 0; | |
} | |
// Constructor | |
public Node(int d, Node n) { | |
data = d; | |
next = n; | |
} | |
// set link to next Node | |
public void setNext(Node n) { | |
next = n; | |
} | |
// set data to current Node | |
public void setData(int d) { | |
data = d; | |
} | |
// get link to next node | |
public Node getNext() { | |
return next; | |
} | |
// get data from current Node | |
public int getData() { | |
return data; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment