0% found this document useful (0 votes)
11 views3 pages

Sorted Singly List

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Sorted Singly List

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

public interface SortedList {

void insert(int element);


void delete(int element);
void deleteAll(int element);
boolean search(int element);
void print();
}

package com.acts;

public class SortedLinkedList implements SortedList {


class Node {
int data;
Node next;

public Node() {
data = 0;
next = null;
}

public Node(int data) {


this.data = data;
next = null;
}
}

Node head;

public SortedLinkedList() {
head = null;
}

@Override
public void insert(int ele) {
Node newNode = new Node(ele); // Create a new node

if (head == null) { // List is empty


head = newNode;
return;
}

Node current = head;


Node previous = null;

// Find the correct insertion point


while (current != null && current.data <= newNode.data) {
previous = current;
current = current.next;
}

if (previous == null) { // Insert at the beginning


newNode.next = head;
head = newNode;
} else { // Insert between previous and current
previous.next = newNode;
newNode.next = current;
}
}
@Override
public void delete(int ele) {
Node current = head;
Node previous = null;

// Special case: deleting the head node


if (current != null && current.data == ele) {
head = head.next; // Move head to the next node
return;
}

// Traverse to find the element to delete


while (current != null) {
if (current.data == ele) {
previous.next = current.next; // Bypass the current node
return;
}
previous = current;
current = current.next;
}

System.out.println("Element is not present"); // Element not found


}

@Override
public void deleteAll(int ele) {
if (head == null) {
System.out.println("List is empty");
return;
}

// Remove occurrences at the head


while (head != null && head.data == ele) {
head = head.next; // Update head to skip nodes with the value 'ele'
}

Node current = head;


Node previous = null;

// Traverse the list to delete all occurrences


while (current != null) {
if (current.data == ele) {
previous.next = current.next; // Bypass the current node
} else {
previous = current; // Move previous only if not deleted
}
current = current.next; // Move to the next node
}
}

@Override
public boolean search(int ele) {
if (head == null) {
System.out.println("List is empty");
return false;
}

Node current = head;


// Traverse the list to search for the element
while (current != null) {
if (current.data == ele) {
return true; // Element found
}
current = current.next; // Move to the next node
}
return false; // Element not found
}

@Override
public void print() {
Node current = head;
while (current != null) {
System.out.println(current.data); // Print current node's data
current = current.next; // Move to the next node
}
}
}

public class tester {


public static void main(String[] args) {
SortedLinkedList list = new SortedLinkedList();
list.insert(0);
list.insert(44);
list.insert(55);
list.insert(66);
list.insert(0);
list.insert(44);
list.insert(77);
list.insert(44);
//list.deleteAll(0);
list.delete(44);
//list.deleteAll(44);
list.print();
System.out.println( list.search(12));
}
}

You might also like