0% found this document useful (0 votes)
47 views7 pages

Lab Task by Sahil 031

The document contains Java code for a linked list data structure. It defines Node and LinkedList classes, with methods to insert nodes at the beginning of the list, after or before a specified node number, and after or before a specified data value. The main method creates a linked list, inserts some initial nodes, then demonstrates calling the insertion methods and printing the modified list.

Uploaded by

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

Lab Task by Sahil 031

The document contains Java code for a linked list data structure. It defines Node and LinkedList classes, with methods to insert nodes at the beginning of the list, after or before a specified node number, and after or before a specified data value. The main method creates a linked list, inserts some initial nodes, then demonstrates calling the insertion methods and printing the modified list.

Uploaded by

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

Name : sahil ali

Reg no : FA22-BAI-031
LAB : DS
SUBMITTED TO : MAAM SAIDA RUBAB

/*
* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package javaapplication92;

class Node {

int data;

Node next;

public Node(int data) {

this.data = data;

this.next = null;

class LinkedList {

private Node head;

public LinkedList() {

this.head = null;

// Method to insert a node at the beginning of the list

public void insertAtBeginning(int data) {

Node newNode = new Node(data);

newNode.next = head;

head = newNode;
}

// Method to insert a node after a specific node number

public void insertAfterNodeNumber(int nodeNumber, int data) {

Node newNode = new Node(data);

Node current = head;

int count = 1;

while (current != null && count < nodeNumber) {

current = current.next;

count++;

if (current == null) {

System.out.println("Node number not found.");

return;

newNode.next = current.next;

current.next = newNode;

// Method to insert a node before a specific node number

public void insertBeforeNodeNumber(int nodeNumber, int data) {

if (nodeNumber == 1) {

insertAtBeginning(data);

return;

}
Node newNode = new Node(data);

Node current = head;

int count = 1;

while (current != null && count < nodeNumber - 1) {

current = current.next;

count++;

if (current == null) {

System.out.println("Node number not found.");

return;

newNode.next = current.next;

current.next = newNode;

// Method to insert a node after a specific data value

public void insertAfterData(int targetData, int data) {

Node newNode = new Node(data);

Node current = head;

while (current != null && current.data != targetData) {

current = current.next;

if (current == null) {

System.out.println("Target data not found.");


return;

newNode.next = current.next;

current.next = newNode;

// Method to insert a node before a specific data value

public void insertBeforeData(int targetData, int data) {

if (head == null) {

System.out.println("List is empty.");

return;

if (head.data == targetData) {

insertAtBeginning(data);

return;

Node newNode = new Node(data);

Node current = head;

while (current.next != null && current.next.data != targetData) {

current = current.next;

if (current.next == null) {

System.out.println("Target data not found.");

return;
}

newNode.next = current.next;

current.next = newNode;

public void SHOWDATA() {

Node current = head;

while (current != null) {

System.out.print(current.data + " ");

current = current.next;

System.out.println();

public class Main {

public static void main(String[] args) {

LinkedList myList = new LinkedList();

myList.insertAtBeginning(1);

myList.insertAtBeginning(2);

myList.insertAtBeginning(3);

myList.insertAtBeginning(4);

myList.insertAtBeginning(5);

System.out.println("Original List:");

myList. SHOWDATA();

myList.insertAfterNodeNumber(3, 6);

myList.insertBeforeNodeNumber(4, 7);

myList.insertAfterData(5, 8);

myList.insertBeforeData(2, 9);
System.out.println("Modified List:");

myList. SHOWDATA();

You might also like