0% found this document useful (0 votes)
5 views

ll

The document contains Java code for a singly linked list implementation, including classes for the linked list (sl), nodes (Node), and a main program (Main) to demonstrate functionality. It includes methods for inserting nodes, displaying the list, and deleting a node from a specified position. The main program creates a linked list, adds several integers, displays the list, deletes a node, and displays the updated list again.

Uploaded by

ASK 011
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)
5 views

ll

The document contains Java code for a singly linked list implementation, including classes for the linked list (sl), nodes (Node), and a main program (Main) to demonstrate functionality. It includes methods for inserting nodes, displaying the list, and deleting a node from a specified position. The main program creates a linked list, adds several integers, displays the list, deletes a node, and displays the updated list again.

Uploaded by

ASK 011
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

package Linklist

1 sl.java
package Linklist;

public class sl {
public Node head = null;
public Node tail = null;

public void insert(int data) {

Node newNode = new Node(data);


// checking of the list is empty
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.setNext(newNode);
tail = newNode;
}
}

public void displaylist() {


Node current = head;
if (head == null) {
System.out.println("The given list is empty");
return;
}
System.out.println("The data in the given list are: ");

while (current != null) {


System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}

public void deletionfrompos(int position) {


System.out.println("Deleting a node from the specified position "
+ position + "\n");
if (this.head == null) {
System.out.println("The given list is empty.\n");
}

else if (position == 0) {
head = head.next;
} else {
Node current1 = head;
for (int i = 0; current1 != null && i < position - 1; i++)
{
current1 = current1.next;

if (current1 == null || current1.next == null) {


System.out.println("The element is not present
at the specified position.\n");
}

Node temp = current1.next.next;


current1.next = temp;

}
}
}

2 Main.java
package Linklist;

public class Main {


public static void main(String[] args) {

// creating a new list


sl newList = new sl();
// Adding data to the list by calling the insert function
newList.insert(10);
newList.insert(30);
newList.insert(50);
newList.insert(70);
newList.insert(100);
newList.insert(120);

// Displaying the data in the list by calling displaylist() function


newList.displaylist();
newList.deletionfrompos(3);
newList.displaylist();
}

3Node.java
package Linklist;

public class Node {


int data;
Node next;

public Node(int data) {


this.data = data;
next = null;
}

public int getData() {


return data;
}

public void setData(int data) {


this.data = data;
}

public Node getNext() {


return next;
}

public void setNext(Node next) {


this.next = next;
}

You might also like