Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

DSA-LAB EVALUATION

A.SAI VARUN
AM.EN.U4AIE21120

public class List {

static class Node {


public int data;
public Node nextNode;

public Node(int data) {


this.data = data;

}
}

static Node NodeGet(int data) {


return new Node(data);
}

static Node InsertAtPos(Node headNode, int data, int position) {


Node head = headNode;
if (position < 1)
System.out.println("Invalid position");

if (position == 1)
{
Node newNode = new Node(data);
newNode.nextNode = headNode;
head = newNode;
}

else
{
while (position != 0) {
if (position == 1) {
Node newNode = NodeGet(data);
newNode.nextNode = headNode.nextNode;

break;
}
headNode = headNode.nextNode;
}
if (position != 1)
System.out.println("Position is out of the given range");
}
return head;
}

static void CopyList(Node node) {


while (node != null) {
System.out.print(node.data);
node = node.nextNode;
if (node != null)
System.out.print(",");
}
System.out.println();
}

//MAIN Method
public static void main(String[] args)
{

Node head = NodeGet(13);


head.nextNode = NodeGet(18);
head.nextNode.nextNode = NodeGet(25);
head.nextNode.nextNode.nextNode = NodeGet(55);

System.out.println(" This is Linked list before insertion: ");


CopyList(head);

int data = 10, pos = 3;


data = 1;
pos = 1;
head = InsertAtPos(head, pos, data);
System.out.println("Linked list after" + "insertion of 1 at position 1: ");
CopyList(head);

OUTPUT:

You might also like