Assignment 3
Assignment 3
Deletion:
Delete from the beginning.
Delete from the end.
Delete a specific element by value.
Delete a specific element by position.
Concatenation:
Duplicate Removal:
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
Node head;
public LinkedList() {
head = null;
}
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;
}
}
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;
}
}
while(current!=null){
next = current.next;
current.next = previous;
previous = current;
current = next;
}
head = previous;
display();
}
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;
}
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: