You are on page 1of 12

Singly linked list implementation

Singly Linked Lists are a type of data structure. It is a type of list. In a singly linked
list each node in the list stores the contents of the node and a pointer or reference to
the next node in the list. It does not store any pointer or reference to the previous
node. It is called a singly linked list because each node only has a single link to
another node. To store a single linked list, you only need to store a reference or
pointer to the first node in that list. The last node has a pointer to nothingness to
indicate that it is the last node.

Here is the pictorial view of singly linked list:

Here is the pictorial view of inserting an element in the middle of a singly linked list:

Here is the pictorial view of deleting an element in the middle of a singly linked list:

Below shows the java implementation of singly linked list:


?
1 package com.java2novice.ds.linkedlist;
2
3 public class SinglyLinkedListImpl<T> {
4
private Node<T> head;
5 private Node<T> tail;
6
7 public void add(T element){
8
9 Node<T> nd = new Node<T>();
10 nd.setValue(element);
11 System.out.println("Adding: "+element);
/**
12 * check if the list is empty
13 */
14 if(head == null){
15 //since there is only one element, both head and
//tail points to the same object.
16
head = nd;
17 tail = nd;
18 } else {
19 //set current tail next link to new node
20 tail.setNextRef(nd);
//set tail as newly created node
21 tail = nd;
22 }
23 }
24
25 public void addAfter(T element, T after){
26
27 Node<T> tmp = head;
Node<T> refNode = null;
28 System.out.println("Traversing to all nodes..");
29 /**
30 * Traverse till given element
31 */
32 while(true){
if(tmp == null){
33 break;
34 }
35 if(tmp.compareTo(after) == 0){
36 //found the target node, add after this node
refNode = tmp;
37 break;
38 }
39 tmp = tmp.getNextRef();
40 }
41 if(refNode != null){
//add element after the target node
42 Node<T> nd = new Node<T>();
43 nd.setValue(element);
44 nd.setNextRef(tmp.getNextRef());
45 if(tmp == tail){
46 tail = nd;
}
47 tmp.setNextRef(nd);
48
49 } else {
50 System.out.println("Unable to find the given element...");
51 }
}
52
53
public void deleteFront(){
54
55 if(head == null){
56 System.out.println("Underflow...");
57 }
58 Node<T> tmp = head;
head = tmp.getNextRef();
59 if(head == null){
60 tail = null;
61 }
62 System.out.println("Deleted: "+tmp.getValue());
63 }
64
public void deleteAfter(T after){
65
66 Node<T> tmp = head;
67 Node<T> refNode = null;
68 System.out.println("Traversing to all nodes..");
69 /**
70 * Traverse till given element
*/
71 while(true){
72 if(tmp == null){
73 break;
74 }
if(tmp.compareTo(after) == 0){
75 //found the target node, add after this node
76 refNode = tmp;
77 break;
78 }
79 tmp = tmp.getNextRef();
}
80 if(refNode != null){
81 tmp = refNode.getNextRef();
82 refNode.setNextRef(tmp.getNextRef());
83 if(refNode.getNextRef() == null){
84 tail = refNode;
}
85 System.out.println("Deleted: "+tmp.getValue());
86 } else {
87 System.out.println("Unable to find the given element...");
88 }
}
89
90
91 public void traverse(){
92
93 Node<T> tmp = head;
while(true){
94 if(tmp == null){
95 break;
96 }
97 System.out.println(tmp.getValue());
tmp = tmp.getNextRef();
98 }
99 }
100
101 public static void main(String a[]){
102 SinglyLinkedListImpl<Integer> sl = new SinglyLinkedListImpl<Integer>();
103 sl.add(3);
sl.add(32);
104 sl.add(54);
105 sl.add(89);
106 sl.addAfter(76, 54);
107 sl.deleteFront();
108 sl.deleteAfter(76);
sl.traverse();
109
110 }
111 }
112
113 class Node<T> implements Comparable<T> {
114
115 private T value;
116 private Node<T> nextRef;
117
public T getValue() {
118 return value;
119 }
120 public void setValue(T value) {
121 this.value = value;
}
122 public Node<T> getNextRef() {
123 return nextRef;
124 }
125 public void setNextRef(Node<T> ref) {
126 this.nextRef = ref;
}
127 @Override
128 public int compareTo(T arg) {
129 if(arg == this.value){
130 return 0;
131 } else {
return 1;
132 }
133 }
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

Output:
Adding: 3
Adding: 32
Adding: 54
Adding: 89
Traversing to all nodes..
Deleted: 3
Traversing to all nodes..
Deleted: 89
32
54
76
public class DoublyLinkList<T> {

private static class Node<T> {


private T data;
private Node next;
private Node prev;

public Node(T data) {


this.data = data;
}

public void displayNode() {


System.out.print(data + " ");
}
@Override
public String toString() {
return data.toString();
}
}

public Node first = null;


public Node last = null;

public void addFirst(T data) {


Node newNode = new Node(data);

if (isEmpty()) {
newNode.next = null;
newNode.prev = null;
first = newNode;
last = newNode;

} else {
first.prev = newNode;
newNode.next = first;
newNode.prev = null;
first = newNode;
}
}

public boolean isEmpty() {


return (first == null);
}

public void displayList() {


Node current = first;
while (current != null) {
current.displayNode();
current = current.next;
}
System.out.println();
}

public void removeFirst() {


if (!isEmpty()) {
Node temp = first;

if (first.next == null) {
first = null;
last = null;
} else {
first = first.next;
first.prev = null;
}
System.out.println(temp.toString() + " is popped from the list");
}
}
public void removeLast() {
Node temp = last;

if (!isEmpty()) {

if (first.next == null) {
first = null;
last = null;
} else {
last = last.prev;
last.next = null;
}
}
System.out.println(temp.toString() + " is popped from the list");
}
}
DoublyLinkListDemo.java
public class DoublyLinkListDemo {

public static void main(String[] args) {


DoublyLinkList newList = new DoublyLinkList();
newList.addFirst("arun");
newList.addFirst("prakash");
newList.addFirst(70);
newList.addFirst(80);
newList.addFirst(30);
newList.displayList();
newList.removeFirst();
newList.removeFirst();
newList.removeFirst();
newList.removeLast();

newList.displayList();
}
}

Java Program to Implement a Binary Search Tree using Linked


Lists
This is a Java Program to implement Binary Search Tree using Linked Lists. A binary search tree
(BST), sometimes also called an ordered or sorted binary tree, is a node-based binary tree data
structure which has the following properties:
i) The left subtree of a node contains only nodes with keys less than the nodes key.
ii) The right subtree of a node contains only nodes with keys greater than the nodes key.
iii) The left and right subtree must each also be a binary search tree.
iv) There must be no duplicate nodes.
Here BST is implemented using Linked List.
Here is the source code of the Java program to implement Binary Search Tree using Linked
Lists. The Java program is successfully compiled and run on a Windows system. The
program output is also shown below.
1. /*
2. * Java Program to Implement a Binary Search Tree using Linked Lists
3. */
4.
5. import java.util.Scanner;
6.
7. /* Class Node */
8. class Node
9. {
10. Node left, right;
11. int data;
12.
13. /* Constructor */
14. public Node(int n)
15. {
16. left = null;
17. right = null;
18. data = n;
19. }
20. }
21.
22. /* Class BST */
23. class BST
24. {
25. private Node root;
26.
27. /* Constructor */
28. public BST()
29. {
30. root = null;
31. }
32. /* Functions to insert data */
33. public void insert(int data)
34. {
35. root = insert(root, data);
36. }
37. /* Function to insert data recursively */
38. private Node insert(Node node, int data)
39. {
40. if (node == null)
41. node = new Node(data);
42. else
43. {
44. if (data <= node.data)
45. node.left = insert(node.left, data);
46. else
47. node.right = insert(node.right, data);
48. }
49. return node;
50. }
51. /* Function for inorder traversal */
52. public void inorder()
53. {
54. inorder(root);
55. }
56. private void inorder(Node r)
57. {
58. if (r != null)
59. {
60. inorder(r.left);
61. System.out.print(r.data +" ");
62. inorder(r.right);
63. }
64. }
65. /* Function for preorder traversal */
66. public void preorder()
67. {
68. preorder(root);
69. }
70. private void preorder(Node r)
71. {
72. if (r != null)
73. {
74. System.out.print(r.data +" ");
75. preorder(r.left);
76. preorder(r.right);
77. }
78. }
79. /* Function for postorder traversal */
80. public void postorder()
81. {
82. postorder(root);
83. }
84. private void postorder(Node r)
85. {
86. if (r != null)
87. {
88. postorder(r.left);
89. postorder(r.right);
90. System.out.print(r.data +" ");
91. }
92. }
93. }
94.
95. /* Class LinkedListBST */
96. public class LinkedListBST
97. {
98. public static void main(String[] args)
99. {
100. Scanner scan = new Scanner(System.in);
101. /* Creating object of BST */
102. BST bst = new BST();
103. System.out.println("Linked List Binary Search Tree Test\n");
104. char ch;
105. /* Accept input */
106. do
107. {
108. System.out.println("Enter integer element to insert");
109. bst.insert( scan.nextInt() );
110.
111. /* Display tree */
112. System.out.print("\nPost order : ");
113. bst.postorder();
114. System.out.print("\nPre order : ");
115. bst.preorder();
116. System.out.print("\nIn order : ");
117. bst.inorder();
118.
119. System.out.println("\nDo you want to continue (Type y or n)
\n");
120. ch = scan.next().charAt(0);
121. } while (ch == 'Y'|| ch == 'y');
122. }
123. }
advertisements
Linked List Binary Search Tree Test

Enter integer element to insert


45

Post order : 45
Pre order : 45
In order : 45
Do you want to continue (Type y or n)

y
Enter integer element to insert
12

Post order : 12 45
Pre order : 45 12
In order : 12 45
Do you want to continue (Type y or n)

y
Enter integer element to insert
67

Post order : 12 67 45
Pre order : 45 12 67
In order : 12 45 67
Do you want to continue (Type y or n)

y
Enter integer element to insert
23

Post order : 23 12 67 45
Pre order : 45 12 23 67
In order : 12 23 45 67
Do you want to continue (Type y or n)

y
Enter integer element to insert
96

Post order : 23 12 96 67 45
Pre order : 45 12 23 67 96
In order : 12 23 45 67 96
Do you want to continue (Type y or n)

y
Enter integer element to insert
32

Post order : 32 23 12 96 67 45
Pre order : 45 12 23 32 67 96
In order : 12 23 32 45 67 96
Do you want to continue (Type y or n)

y
Enter integer element to insert
24

Post order : 24 32 23 12 96 67 45
Pre order : 45 12 23 32 24 67 96
In order : 12 23 24 32 45 67 96
Do you want to continue (Type y or n)

You might also like