0% found this document useful (0 votes)
11 views5 pages

PG 9 Node

The document outlines a Java program designed to insert a node into a sorted linked list. It includes an algorithm detailing the steps for node insertion and provides the complete code implementation for the linked list and node classes. The program allows user input for node values and displays the resulting linked list after insertions.

Uploaded by

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

PG 9 Node

The document outlines a Java program designed to insert a node into a sorted linked list. It includes an algorithm detailing the steps for node insertion and provides the complete code implementation for the linked list and node classes. The program allows user input for node values and displays the resulting linked list after insertions.

Uploaded by

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

PROGRAM 11:

AIM:
To write the java program to insert a node in a sorted linked list.
ALGORITHM:
1. Start the program.
2. Import the header file.
import java.io.*;
3. Declare the class, object and main function.
class node
4. Declare the variables.
int data;
5. Ptr=start
6. NEWPTR=new Node
7. if NEWPTR=NULL
8. else
9. NEWPTR.INFO=ITEM
10.NEWPTR.LINK=NULL
11.IF START=NULL then
12.START=NEWPTR
13.else if ITEM<START.INFO then
14.Save=start
15.START=NEWPTR
16.NEWPTR.LINK=Save
17.Save=Start
18.Repeat steps 16 through 22 until ptr=NULL
19.if NEWPTR.INFO<ptr.INFO then
20.Save=ptr
21.Ptr=ptr.LINK
22.else
23.Save.LINK=NEWPTR
24.NEWPTR.LINK=ptr
25.Break
26.if ptr=NULL then
27.Save.LINK=NEWPTR
28.NEWPTR.LINK=NULL
29.NEWPTR.LINK=NULL
30.End the program.
COADING:

import java.io.*;
class Node
{
protected int data;
protected Node link;
public Node()
{
link=null;
data=0;
}
public Node(int d,Node n)
{
data=d;
link=n;
}
public void setlink(Node n)
{
link=n;
}
public void setData(int d)
{
data=d;
}
public Node getlink()
{
return link;
}
public int getData()
{
return data;
}
}
class linkedList
{
protected Node start;
public linkedList()
{
start=null;
}
public boolean isEmpty()
{
return start==null;
}
public void Insert(int val)
{
Node nptr,ptr,save=null;
nptr=new Node(val,null);
Boolean ins=false;
if(start==null)
{
start=nptr;
}
else if
{
(val<=start.getData());
nptr.setlink(start);
start=nptr;
}
else
{
save=start;
ptr=start.getlink();
while(ptr!=null)
{
if(val>=save.getData()&&val<=ptr.getData())
{
save.setlink(nptr);
nptr.setlink(ptr);
ins=true;
break;
}
else
{
save=ptr;
ptr=ptr.getlink();
}
}
if(ins==false)
{
save.setlink(nptr);
}
}
}
public void display()
{
Node ptr=start;
System.out.print(start.getData()+"-->");
ptr=start.getlink();
while(ptr.getlink()!=null)
{
System.out.print(ptr.getData()="-->");
ptr=ptr.getlink();
}
System.out.print(ptr.getData()+"!!!!");
System.out.println();
}
}
class linListTest
{
protected static linkedList S;
public static void main(String[]args)
{
int num;
S=new linkedList();
BufferedReaderbr=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("……Starting List Test for INSERTION……\n");
for(int a=0;a<5;++a)
{
System.out.print("Enter a number :");
try
{
num=Integer.parseInt(br.readLine());
S.Insert(num);
System.out.println("Inserted : "+num);
}
catch(Exception e)
{
System.out.println(e);
}
}
System.out.println("\n created List is :");
S.display();
System.out.println("\n---List test over---");
}
}

You might also like