Let’s Talk About Encapsulation—SortedLinkedList

Encapsulation is a process of wraping data and functions into single unit.Our Node class remains as it is and there is no change from previous post. Here, I have created a class SortedLinkedList with its functions. First step to define two properties :public class SortedLinkedList{
private Node head = null;
private int size = 0;
}There are two constructors,
1. Empty constructor
2. Parameterized constructor to initialize with a array.public SortedLinkedList() {}
public SortedLinkedList(int[] elements) {
for (int i = 0; i <elements.length; i++) {
insertData(elements[i]);
}
}Second step to insertData()public void insertData(int data) {
Node newnode = createNode(data);
if (head == null) {
head = newnode;
} else {
Node node = head;
do {
if (node.getData() >= newnode.getData() && node == head) {
newnode.setNext(head);
head = newnode;
break;
} else if (node.getNext() == null
|| (node.getNext()).getData() >= newnode.getData()) {
newnode.setNext(node.getNext());
node.setNext(newnode);
break;
} else {
node = node.getNext();
}
} while (node != null);
System.out.println(newnode);
}
size++;
}Passing array to insertData()?—?Function overloading.public void insertData(int elements[]) {
for (int i = 0; i < elements.length; i++) {
insertData(elements[i]);
}
}To find out first index (position) of an element.public int indexOf(int ele) {
int position = -1;
if (head == null) {
System.out.println(“Linked list is empty”);
} else {
int i = 0;
Node node = head;
do {
i++;
if (node.getData() == ele) {
position = i;
break;
}
node = node.getNext();
} while (node != null);
}
return position;
}To find out last index (position) of an element.public int lastIndexOf(int o) {
int position = -1;
if (head == null) {
System.out.println(“Linked list is empty”);
} else {
int i = 0;
Node node = head;
do {
i++;
if (node.getData() == o) {
position = i;
}
node = node.getNext();
} while (node != null);
}
return position;
}Remove element from Linked List, if list contains duplicate elements it will remove first occurrence of an element. The function also returns true/false indicating whether element is removed or not.public boolean remove(int ele) {
if (head != null) {
Node node = head;
do {
if (head.getData() == ele) {
head = head.getNext();
return true;
}
if (node.getNext().getData() == ele) {
node.setNext(node.getNext().getNext());
size–;
return true;
}
node = node.getNext();
} while (node != null);
}
return false;
}Remove element from Linked List, if list contains duplicate elements it will remove all occurrence of an element.public void removeAll(int ele) {
if (head != null) {
int i = 0;
Node node = head;
do {
if (head.getData() == ele){
head = head.getNext();
size–;
i=1;
} else if (node.getNext().getData() == ele) {
node.setNext(node.getNext().getNext());
size–;
i = 1;
}
if (i == 1) {
node = node;
i = 0;
} else {
node = node.getNext();
}
} while (node.getNext() != null);
}
}Remove all the elements from the list and empty it.public void clear() {
head = null;
size=0;
}
To find out the size of list.

public int size()
{
return size;
}
To print all the elements.

public void traversal() {
if (head == null) {
System.out.println(“Linked list is empty”);
} else {
Node node = head;
do {
System.out.print(“ “ + node.getData() + “ ->”);
node = node.getNext();
} while (node != null);
System.out.print(“ NULL”);
}
}Last step to access SortedLinkedList classpublic static void main(String[] args) {
SortedLinkedList list = new SortedLinkedList();
list.insertData(4);
list.traversal();
int arr[] = { 5, 6, 8, 3, 2 };
SortedLinkedList list1 = new SortedLinkedList(arr);
list1.insertData(30);
list1.insertData(23);
list1.insertData(8);
list1.traversal();
System.out.println(“First Index of 80: “ + list1.indexOf(80));
System.out.println(“First Index of 8 : “ + list1.indexOf(8));
System.out.println(“Last Index of 8 : “+list1.lastIndexOf(8));
list1.remove(5);
list1.traversal();
list1.removeAll(8);
list1.traversal();
System.out.println(“Size : “+list1.size());
list1.clear();
}The benefits of our new encapsulated data structureRe-usability—we can create multiple sorted linked list and each of them is independent.Total control over data structure. The structure can be accessed by only using its functions which defines access patterns. No one else can disturb this data.It can now be called as adata structure.

News Reporter

Leave a Reply

Your email address will not be published. Required fields are marked *