Single Linked List Programs in Java
- Categories Java, Data Structures
To understand more about Linked List watch our both videos. To watch videos on Single Linked List click on Single Linked List Programs in Java and single Linked List Programs Part 2
Q. What is linked list?
Ans. A linked list is a collection of elements, but the elements are not stored in a consecutive location.
Q. What is the difference between array and link list.
Ans.
array | link list |
---|---|
Arrays are stored at contiguous memory location. | Linked lists are stored at different locations in the memory. |
fixed size | dynamic size |
insert and delete an element is difficult | insert and delete an element is easy |
Array elements can be accessed randomly using the array index. | Random accessing is not possible in linked lists. The elements will have to be accessed sequentially. |
Q. What are the different types of Linked list?
Ans. Linked list are of three types:
– Single Linked List
– Double Linked List
– Circular Linked list
java program print, count Node, count if Node, sum of Node, sum If Node, product of Node, product If Node, min Node, max Node, search any Node operation on link list.
import java.util.Scanner;
class Node {
int data;
Node next;
Node(int d){
data = d;
next = null;
}
}
public class LinkedList
{
Node insertNode(Node start, int data, int pos)
{
Node n1 = new Node(data);
if (pos == 0 || start == null)
{
n1.next=start; return n1;
}
else if (pos < 0)
{
Node copy = start;
while (copy.next != null)
{
copy = copy.next;
}
copy.next = n1;
return start;
}
else
{
int step = 1;
Node copy = start;
while (copy.next != null)
{
if (step == pos) {
n1.next = copy.next;
copy.next = n1;
return start;
}
step++;
copy = copy.next;
}
System.out.println("Reached end of list");
return start;
}
}
void printNode(Node start) {
while (start != null) {
System.out.println(start.data);
start = start.next;
}
}
void countNode(Node start) {
int count = 0;
while (start != null) {
count++;
start = start.next;
}
System.out.println ("Count = " + count);
}
void countIfNode(Node start) {
int count = 0;
while (start != null) {
if(start.data >= 18)
count++;
start = start.next;
}
System.out.println ("Count = " + count);
}
void sumNode(Node start) {
int sum = 0;
while (start != null) {
sum = sum + start.data;
start = start.next;
}
System.out.println ("Sum = " + sum);
}
void sumIfNode(Node start) {
int sum = 0;
while (start != null) {
if (start.data%2 == 0)
sum = sum + start.data;
start = start.next;
}
System.out.println ("Sum = " + sum);
}
void productNode(Node start) {
int prod = 1;
while (start != null) {
prod = prod * start.data;
start = start.next;
}
System.out.println ("Product = " + prod);
}
void productIfNode(Node start) {
int prod = 1;
while (start != null) {
if(start.data%2 == 1)
prod = prod * start.data;
start = start.next;
}
System.out.println ("Product of odd = " + prod);
}
void minNode(Node start) {
int min = start.data;
while (start != null) {
if(start.data < min)
min = start.data;
start = start.next;
}
System.out.println ("Min Value = " + min);
}
void maxNode(Node start) {
int max = start.data;
while (start != null) {
if(start.data > max)
max = start.data;
start = start.next;
}
System.out.println ("Min Value = " + max);
}
void searchNode(Node start, int val) {
while (start != null) {
if (start.data == val){
System.out.println("found");
return;
}
start = start.next;
}
System.out.println("not found");
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int opt = 0;
Node start = null;
LinkedList llist = new LinkedList();
do {
System.out.println("Enter the option: 1 insert 2 print 3 countNode 4 countIfNode 5 sumNode 6 sumIfNode 7 productNode 8 productIfNode 9 minNode 10 maxNode 11 searchNode 12 exit ");
opt = input.nextInt();
switch (opt ) {
case 1:
System.out.print("Enter data");
int data = input.nextInt();
System.out.print("Enter position");
int pos = input.nextInt();
start = llist.insertNode(start,data,pos);
break;
case 2:
llist.printNode(start);
break;
case 3:
llist.countNode(start);
break;
case 4:
llist.countIfNode(start);
break;
case 5:
llist.sumNode(start);
break;
case 6:
llist.sumIfNode(start);
break;
case 7:
llist.productNode(start);
break;
case 8:
llist.productIfNode(start);
break;
case 9:
llist.minNode(start);
break;
case 10:
llist.maxNode(start);
break;
case 11:
System.out.print("Enter search Node");
int p = input.nextInt();
llist.searchNode(start,p );
break;
}
} while (opt != 12);
}
}
Link list insertion, deletion, reverse and rotate operation.
import java.util.Scanner; class Node { int data; Node next; Node(int d){ data = d; next = null; } } public class LinkedList { Node insertNode(Node start, int data, int pos) { Node n1 = new Node(data); if (pos == 0 || start == null) { n1.next=start; return n1; } else if (pos < 0) { Node copy = start; while (copy.next != null) { copy = copy.next; } copy.next = n1; return start; } else { int step = 1; Node copy = start; while (copy.next != null) { if (step == pos) { n1.next = copy.next; copy.next = n1; return start; } step++; copy = copy.next; } System.out.println("Reached end of list"); return start; } }
void printNode(Node start)
{
while (start != null)
{
System.out.println(start.data);
start = start.next;
}
} Node rotateList(Node start, int n) { int pos = 0; Node newStart = start, lastNode=start; Node copy = start; while (copy.next != null) { if (++pos == n) { lastNode = copy; newStart = copy.next; } copy = copy.next; } copy.next = start; lastNode.next = null; return newStart; }
Node reverseList(Node start)
{
Node newStart = null;
while (start!= null)
{
Node n1 = new Node(start.data);
n1.next = newStart;
newStart = n1;
start = start.next;
}
return newStart;
} Node deleteNode(Node start, int pos) { if (pos == 0 ) { return start.next; } else if (pos < 0) { Node copy = start; while (copy.next.next != null) { copy = copy.next; } copy.next = null; } else { Node copy = start; int step = 1; while (copy.next != null) { if (step == pos) { copy.next = copy.next.next; return start; } copy = copy.next; step++; } System.out.println("Reached end of list"); } return start; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int opt = 0; Node start = null; LinkedList llist = new LinkedList(); do { System.out.print("Enter the option: 1 insert 2 print 3 rotate 4 reverse 5 delete 6 quit"); opt = input.nextInt(); switch (opt ) { case 1: System.out.print("Enter data"); int data = input.nextInt(); System.out.print("Enter position"); int pos = input.nextInt(); start = llist.insertNode(start,data,pos); break; case 2: llist.printNode(start); break; case 3: System.out.print("Enter no of items to rotate"); int i = input.nextInt(); start = llist.rotateList(start,i); llist.printNode(start); break; case 4: Node n1 = llist.reverseList(start); llist.printNode(n1); break; case 5: System.out.print("Enter position"); pos = input.nextInt(); start = llist.deleteNode(start,pos); break; } } while (opt != 6); } }