Linked Lists Implementation in Java
Course Name: CS6308 Java Programming
Course Instructor: Jayachitra V P
NAME: ASHWATH B REG NO:2022503023
Date: 28/08/2024
1. Write a Java program to implement the below listed task
Insertion:
Insert at the beginning.
Insert at the end.
Insert at a specific position.
Insert after a specific node.
Insert before a specific node.
Deletion:
Delete from the beginning.
Delete from the end.
Delete a specific element by value.
Delete a specific element by position.
Traversal and Display:
Traverse and print the elements in the linked list.
Reverse and print the elements in the linked list.
Search and Access:
Search for an element by value.
Access an element by position.
Length and Counting:
Find the length (number of nodes) of the linked list.
Count the occurrences of a specific value in the list.
Sorting :
Sort the linked list (best sort).
Concatenation:
Concatenate (combine) two linked lists together.
Duplicate Removal:
Remove duplicate elements from a linked list.
Note: Code snippet for your reference
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
Node head;
public LinkedList() {
head = null;
}
public void insertAtBeginning(int data) {}
public void insertAtEnd(int data) {}
public void insertAtPosition(int data, int position) {}
…}
CODE:
import java.util.Scanner;
class Node{
int data;
Node next;
public Node(int data){
this.data=data;
this.next=null;
}
}
public class LinkedList {
Node head;
public LinkedList(){
this.head = null;
}
public void insertAtBegin(int data){
Node newnode = new Node(data);
if(head==null){
head = newnode;
}
else {
newnode.next = head;
head = newnode;
}
}
public void insertAtEnd(int data){
Node newnode = new Node(data);
if(head == null){
head = newnode;
}
else {
Node current = head;
while(current.next != null){
current = current.next;
}
current.next = newnode;
}
}
public void insertAtPosition(int position,int
data){
Node newnode = new Node(data);
if (position == 0){
newnode.next = head;
head = newnode;
return;
}
Node current = head;
int count = 0;
while(current !=null && count<position-1){
current = current.next;
count++;
}
if (current == null){
System.out.println("OUT OF BOUNDARY");
}
else {
newnode.next = current.next;
current.next = newnode;
}
}
public void insertAfterValue(int pos,int data){
Node newnode = new Node(data);
Node current = head;
while(current != null && current.data!=pos){
current = current.next;
}
if(current==null){
System.out.println("TARGET VALUE NOT
FOUND");
}
else {
newnode.next = current.next;
current.next = newnode;
}
}
public void insertBeforeValue(int eleme,int
data){
Node newnode = new Node(data);
if(head == null){
System.out.println("LIST IS EMPTY!!");
}
if (head.data == eleme){
newnode.next = head;
head = newnode;
return;
}
Node current = head;
Node previous = null;
while(current!=null && current.data!=eleme){
previous = current;
current=current.next;
}
if (current==null){
System.out.println("Target node not
found!!");
}
else {
previous.next=newnode;
newnode.next = current;
}
}
public void deleteBegin(){
if(head == null){
System.out.println("List is Empty");
return;
}
head = head.next;
}
public void deleteEnd(){
if(head==null){
System.out.println("List is empty");
return;
}
if (head.next == null){
head=null;
}
Node current = head;
while (current.next.next!=null){
current=current.next;
}
current.next = null;
}
public void deleteValues(int data){
if(head == null){
System.out.println("LIST is Empty!!");
return;
}
if (head.data == data){
head = head.next;
return;
}
Node current = head;
Node previous = null;
while (current!=null && current.data != data){
previous = current;
current = current.next;
}
if (current == null){
System.out.println("Value not find");
}
else {
previous.next = current.next;
}
}
public void deletePosition(int value){
Node current = head;
if(head == null){
System.out.println("No elements in the
List");
return;
}
if(value == 0){
head = head.next;
}
int count =0;
while (current != null && count < value-1){
current = current.next;
count++;
}
if (current == null){
System.out.println("Out of Boundary");
}
else {
current.next = current.next.next;
}
}
public void reverseList(){
if(head == null){
System.out.println("LIST is Empty");
return;
}
Node next = null;
Node previous = null;
Node current = head;
while(current!=null){
next = current.next;
current.next = previous;
previous = current;
current = next;
}
head = previous;
display();
}
public void searchValue(int val){
if(head == null) {
System.out.println("List is empty!");
return;
}
Node current = head;
int position = 0;
while(current!=null) {
if (current.data == val) {
System.out.println("Value: " +
current.data + " is in the position " + position);
return;
}
current = current.next;
position++;
}
System.out.println("Position "+position+" is
out of boundary");
}
public void searchposition(int data){
if(head == null){
System.out.println("List is empty");
return;
}
Node current = head;
int count =0;
while(current!=null){
if(count == data){
System.out.println("The value is
"+current.data+" from position "+data);
return;
}
current = current.next;
count++;
}
System.out.println("Invalid Position!!");
}
public void LengthOfList(){
int length =0;
if(head == null){
System.out.println("Length of the List is
"+length);
return;
}
Node current = head;
while (current!=null){
current = current.next;
length++;
}
System.out.println("length of the linked list is
"+ length);
}
public void occurance(int occur){
if(head == null){
System.out.println("List is empty");
return;
}
int count =0;
Node current= head;
while (current != null){
if (current.data == occur){
count++;
}
current = current.next;
}
System.out.println(count+" times the value "+
occur+" oocured");
}
public void bubblesort(){
if(head == null && head.next == null){
return;
}
boolean swap;
do {
Node current = head;
swap = false;
while(current!=null && current.next!=null){
if(current.data>current.next.data){
int temp = current.data;
current.data = current.next.data;
current.next.data = temp;
swap = true;
}
current = current.next;
}
}while (swap);
}
public void concatinate(LinkedList other){
if(head == null){
head = other.head;
}
else {
Node current = head;
while (current.next!=null){
current = current.next;
}
current.next = other.head;
}
}
public void removeDuplicates(){
Node current = head;
while (current!=null){
Node runner = current;
while (runner.next!=null){
if(runner.next.data == current.data){
runner.next = runner.next.next;
}
else {
runner = runner.next;
}
}
current = current.next;
}
display();
}
public void display(){
Node current = head;
if(current == null){
System.out.println("No Elements in the
List");
return;
}
while(current.next!=null)
{
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println(current.data);
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
Scanner sc = new Scanner(System.in);
System.out.println("------ASHWATH B
(3023)------");
System.out.println("------LINKED LIST------");
while(true){
System.out.println("Enter the choic ");
System.out.println("1.Insert at begining");
System.out.println("2.Insert at the end");
System.out.println("3.Insert at a specific
position.");
System.out.println("4.Insert after a specific
node.");
System.out.println("5.Insert before a
specific node.");
System.out.println("6.Delete from the
beginning.");
System.out.println("7.Delete from the
end.");
System.out.println("8.Delete a specific
element by value.");
System.out.println("9.Delete a specific
element by position.");
System.out.println("10.Reverse and print
the elements in the linked list.");
System.out.println("11.Search for an
element by value.");
System.out.println("12.Access an element
by position.");
System.out.println("13.Find the length
(number of nodes) of the linked list.");
System.out.println("14.Count the
occurrences of a specific value in the list.");
System.out.println("15.Sort the linked list
(best sort).");
System.out.println("16.Concatenate
(combine) two linked lists together.");
System.out.println("17.Remove duplicate
elements from a linked list.");
System.out.println("18.Dispaly");
System.out.println("19.Exit");
int choice = sc.nextInt();
switch(choice){
case 1:
System.out.print("Enter the element to
insert at first: ");
int first = sc.nextInt();
list.insertAtBegin(first);
break;
case 2:
System.out.print("Enter the element to
insert at end: ");
int end = sc.nextInt();
list.insertAtEnd(end);
break;
case 3:
System.out.print("Enter the position to
insert:");
int position = sc.nextInt();
System.out.println();
System.out.print("Enter the element to
be inserted:");
int ele = sc.nextInt();
list.insertAtPosition(position,ele);
break;
case 4:
System.out.print("Enter the data:");
int pos = sc.nextInt();
System.out.println();
System.out.print("Enter the element to
be inserted:");
int elem = sc.nextInt();
list.insertAfterValue(pos,elem);
break;
case 5:
System.out.print("Enter the data:");
int data = sc.nextInt();
System.out.println();
System.out.print("Enter the element to
be inserted:");
int eleme = sc.nextInt();
list.insertBeforeValue(data,eleme);
break;
case 6:
list.deleteBegin();
break;
case 7:
list.deleteEnd();
break;
case 8:
System.out.print("Enter the element to
be deleted:");
int elemen = sc.nextInt();
list.deleteValues(elemen);
break;
case 9:
System.out.print("Enter the position to
be deleted:");
int element = sc.nextInt();
list.deletePosition(element);
break;
case 10:
list.reverseList();
break;
case 11:
System.out.print("Enter the element to
searched:");
int val = sc.nextInt();
list.searchValue(val);
break;
case 12:
System.out.print("Enter the Position of
the List:");
int value = sc.nextInt();
list.searchposition(value);
break;
case 13:
list.LengthOfList();
break;
case 14:
System.out.print("Enter the value to
count the numberb of occurance:");
int occur = sc.nextInt();
list.occurance(occur);
break;
case 15:
System.out.println("Sorting the List");
list.bubblesort();
break;
case 16:
LinkedList newList = new LinkedList();
System.out.println("Creating new
Linked List");
System.out.print("Enter the number of
length of linked list:");
int n = sc.nextInt();
for(int i=1;i<=n;i++){
int newelem = sc.nextInt();
newList.insertAtEnd(newelem);
System.out.println("|");
System.out.println();
}
list.concatinate(newList);
list.display();
break;
case 17:
list.removeDuplicates();
break;
case 18:
list.display();
break;
case 19:
System.out.println("Exited");
sc.close();
return;
default:
System.out.println("Invalid choice!!");
}
}
}
}
OUTPUT:
2. Implement Stack using LinkedList
CODE:
import java.util.Scanner;
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
class Stack {
private Node top;
Stack() {
this.top = null;
}
public void push(int data) {
Node newNode = new Node(data);
newNode.next = top;
top = newNode;
System.out.println(data + " pushed to stack.");
}
public int pop() {
if (top == null) {
System.out.println("Stack is empty.");
return -1;
}
int popped = top.data;
top = top.next;
return popped;
}
public int peek() {
if (top == null) {
System.out.println("Stack is empty.");
return -1;
}
return top.data;
}
public boolean isEmpty() {
return top == null;
}
}
public class StackUsingLinkedList {
public static void main(String[] args) {
Stack stack = new Stack();
System.out.println("--------ASHWATH B (3023)--------");
Scanner scanner = new Scanner(System.in);
int choice, value;
while (true) {
System.out.println("\nStack Operations:");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Check if Empty");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter value to push: ");
value = scanner.nextInt();
stack.push(value);
break;
case 2:
value = stack.pop();
if (value != -1)
System.out.println("Popped value: " + value);
break;
case 3:
value = stack.peek();
if (value != -1)
System.out.println("Top value: " + value);
break;
case 4:
if (stack.isEmpty())
System.out.println("Stack is empty.");
else
System.out.println("Stack is not empty.");
break;
case 5:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try
again.");
}
}
}
}
OUTPUT: