0% found this document useful (0 votes)
4 views2 pages

Java Single Linked List Implementation

This document contains a Java implementation of a singly linked list with basic operations such as adding, inserting, and deleting nodes. The class 'SingleLL' manages the linked list, while the 'Node' class represents individual elements. The main method demonstrates the functionality of the linked list by performing various operations and printing the list at each step.

Uploaded by

mahendrag0528
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)
4 views2 pages

Java Single Linked List Implementation

This document contains a Java implementation of a singly linked list with basic operations such as adding, inserting, and deleting nodes. The class 'SingleLL' manages the linked list, while the 'Node' class represents individual elements. The main method demonstrates the functionality of the linked list by performing various operations and printing the list at each step.

Uploaded by

mahendrag0528
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

/***** Linked list Implemntation ****/

import [Link].*;
import [Link].*;
class Node{
int data;
Node next;
Node(int data){
[Link]=data;
[Link]=null;
}
}
public class SingleLL{
Node head;
Node tail;
SingleLL(){
[Link]=null;
[Link]=null;
}
void add(int data){
Node x=new Node(data);
if(head==null){
head=x;
tail=x;
}
else{
[Link]=x;
tail=x;
}
}
void print(){
Node x=head;
while(x!=null){
[Link]([Link]+"-->");
x=[Link];
}
[Link]("null");
}

//insert at index start from 1


void insert(int data,int index){
Node x=new Node(data);
int c=1;
if(c==index){
[Link]=head;
head=x;
}
else{
Node cur=head;
while([Link]!=null && c<(index-1)){
cur=[Link];
c++;
}
[Link]=[Link];
[Link]=x;
}
}

void delete(int index){


int c=1;
Node x=head;
if(index==1)
head=[Link];
while([Link]!=null && c<(index-1)){
x=[Link];
c++;
}
if([Link]==null)return;
Node r=[Link];
[Link]=[Link];
}

public static void main(String[] args){


SingleLL obj=new SingleLL();
[Link]();
[Link](4);
[Link](69);
[Link](6);
[Link]();
[Link](3,5);
[Link]();
[Link](4);
[Link]();
}
}

You might also like