0% found this document useful (0 votes)
34 views12 pages

Import Java - Io. Import Java - Util.scanner Class Node (Int Value Node Link Node (Value 0 Link Null ) )

This document contains the code for a Java class that implements a single linked list data structure. The class includes methods for inserting and removing nodes from the list, getting a node's value, finding the index of a value, checking if the list is empty, and displaying the list. The main method creates an instance of the class and calls the execute method, which uses a menu driven interface to test the linked list methods.
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)
34 views12 pages

Import Java - Io. Import Java - Util.scanner Class Node (Int Value Node Link Node (Value 0 Link Null ) )

This document contains the code for a Java class that implements a single linked list data structure. The class includes methods for inserting and removing nodes from the list, getting a node's value, finding the index of a value, checking if the list is empty, and displaying the list. The main method creates an instance of the class and calls the execute method, which uses a menu driven interface to test the linked list methods.
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/ 12

import java.io.

*;
import java.util.Scanner;
class Node
{
int value;
Node link;
Node()
{
value=0;
link=null;
}

class sll

int size,x, index;

Node header;
sll()

Scanner sc=new Scanner(System.in);

header=new Node();

System.out.println("Enter header value:");

header.value=sc.nextInt();

header.link=null;

size=1;

void insert(int index, int value)

if(index<0||index>=size)

throw new IllegalArgumentException("Enter


valid index");
}

if(index==0)

Node p= new Node();

p.value=value;

p.link=header;

header=p;

else

Node q= header;

for(int i=0;i<index-1;i++)

q=q.link;

p.link=q.link;

q.link=p;

}
size++;

int remove(int index)

if(header == null)

System.out.println("Linked list is empty");

if(index<0||index>=size)

throw new IllegalArgumentException("Enter


valid index");

if(index==0)

x=header.value;
p=header;

header=header.link;

delete p;

else

p=header;

for(int i=0;i<index-1;i++)

p=p.link;

Node q=p.link;

x=q.value;

p.link=q.link;

delete q;

return x;

}
int get(int index)

if(header==null)

System.out.println("Single Linked List Empty");

if(index<0||index>=size)

throw new IllegalArgumentException("Enter


valid index");

Node p=header;

for(int i=0;i<index;i++)

p=p.link;

return p.value;

}
int indexOf(int value)

if(header==null)

System.out.println("Single Linked List Empty");

if(index<0||index>=size)

throw new IllegalArgumentException("Enter


valid index");

int index=1;

Node p=header;

while(p!=null)

index++;

if(p.value==value)

return index;
p=p.link;

System.out.println("Value Not Found...");

boolean isempty()

if(header==null)

return true;

else

return false; }

void display()

if(header==null)

System.out.println("Single Linked List Empty");

Node p=header;

while(p!=null)
{

System.out.println(p.value+"\t");

p=p.link;

void execute()

int index, value, ch;

Scanner sc=new Scanner(System.in);

do

System.out.println("\n 1. Insert\n 2.
Remove\n 3. Get\n 4. IndexOf\n 5. IsEmpty\n
6. Display");

System.out.println("Enter Your Choice:");

ch=sc.nextInt();
switch(ch)

case 1: System.out.println("Enter index and


value: ");

index=sc.nextInt();

value=sc.nextInt();

insert(index, value);

break;

case 2: System.out.println("Enter index to be


Removed: ");

index=sc.nextInt();

System.out.println("Removed Element
is:"+remove(index));

break;

case 3: System.out.println("Enter Index:");

index=sc.nextInt();
System.out.println("Value at
Index:"+index+"is"+get(index));

break;

case 4: System.out.println("Enter value:");

value=sc.nextInt();

System.out.println("Index of Value
is:"+indexOf(value));

break;

case 5:

if(isempty())

System.out.println("Single LinkedList is
Empty");

else

System.out.println("Single LinkedList is Not


Empty");

break;

case 6: display();
break;

}while(ch>0&&ch<7);

public static void main(String args[])

sll s=new sll();

s.execute();

You might also like