Skip to content

Commit f8a236b

Browse files
authored
Java files for singly Linked List
I have hardcoded the insertion and deletion operations, instead of giving the user choice feature. The main aim of this repository is the core algorithm, not user interaction. If needed, switch case or other similar structures can be used to let the user decide what methods to call at what time. Also, there is only singly LL here. Implementing doubly LL and circular LL is very much similar once you understand singly LL. Maybe later I will add them too. Feel free to ask me for them in case I don't.
1 parent 31f8583 commit f8a236b

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

LinkedList.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
class LinkedList {
2+
private static ListNode head = null;
3+
private static ListNode tail = null;
4+
LinkedList(int data) {
5+
ListNode node = new ListNode(data);
6+
head = node;
7+
tail = node;
8+
}
9+
10+
public void printList(){
11+
System.out.print("START -> ");
12+
ListNode ptr = head;
13+
while(ptr != null){
14+
System.out.print(ptr.getData()+" -> ");
15+
ptr = ptr.getNext();
16+
}
17+
System.out.println("END");
18+
}
19+
20+
public void insertHead(int data){
21+
ListNode node = new ListNode(data);
22+
if(head == null)
23+
head = tail = node;
24+
else{
25+
node.setNext(head);
26+
head = node;
27+
}
28+
}
29+
30+
public void insertTail(int data){
31+
ListNode node = new ListNode(data);
32+
if(head == null)
33+
head = tail = node;
34+
else{
35+
tail.setNext(node);
36+
tail = node;
37+
}
38+
}
39+
40+
public void insertAfterNodeData(int afterThisData, int data){
41+
ListNode node = new ListNode(data);
42+
if(head == null)
43+
head = tail = node;
44+
else{
45+
ListNode ptr = head;
46+
while(ptr.getData() != afterThisData)
47+
ptr = ptr.getNext();
48+
node.setNext(ptr.getNext());
49+
ptr.setNext(node);
50+
}
51+
}
52+
53+
public void deleteHead(){
54+
if(head == null){
55+
System.out.print("List Empty !");
56+
return;
57+
}
58+
else{
59+
ListNode ptr = head;
60+
head = ptr.getNext();
61+
ptr = null;
62+
}
63+
}
64+
65+
public void deleteTail(){
66+
if(head == null){
67+
System.out.print("List Empty !");
68+
return;
69+
}
70+
else{
71+
ListNode ptr = head;
72+
while(ptr.getNext().getNext() != null)
73+
ptr = ptr.getNext();
74+
ptr.setNext(null);
75+
tail = ptr;
76+
tail.setNext(null);
77+
}
78+
}
79+
80+
public void deleteNodeWithData(int toBeDeleted){
81+
if(head == null){
82+
System.out.print("List Empty !");
83+
return;
84+
}
85+
else{
86+
ListNode ptr = head;
87+
while(ptr.getNext().getData() != toBeDeleted)
88+
ptr = ptr.getNext();
89+
ptr.setNext(ptr.getNext().getNext());
90+
ptr = ptr.getNext();
91+
ptr = null;
92+
}
93+
}
94+
95+
public static void main(String[] args) {
96+
// TODO Auto-generated method stub
97+
LinkedList L = new LinkedList(21);
98+
L.insertHead(15);
99+
L.insertTail(36);
100+
L.insertTail(50);
101+
L.insertTail(45);
102+
L.insertAfterNodeData(36, 10);
103+
L.printList();
104+
L.deleteNodeWithData(10);
105+
L.printList();
106+
}
107+
}

ListNode.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class ListNode{
2+
private int data;
3+
private ListNode next = null;
4+
5+
ListNode(int data){
6+
this.data = data;
7+
}
8+
9+
public int getData(){ return this.data; }
10+
public ListNode getNext(){ return this.next; }
11+
12+
public void setData(int data){ this.data = data; }
13+
public void setNext(ListNode next){ this.next = next; }
14+
}

0 commit comments

Comments
 (0)