Sunteți pe pagina 1din 6

import java.io.

*; Insertion program

class Insertion

class Node

int m;

Node n;

public Node(int m)

this.m=m;

this.n=null;

int m;

Node n;

/*public Insertion(int m)

this.m=m;

this.n=null;

}*/

public Node head=null;


public Node tail=null;

public void adNod(int m)

Node newNode=new Node(m);

if(head==null)

head=newNode;

tail=newNode;

else

tail.n=newNode;

tail=newNode;

public void instart(int s)

Node newNode=new Node(s);

if(head==null)

head=newNode;

tail=newNode;

else
{

newNode.n=head;

head=newNode;

public void inmid(int s,int p)

Node newNode=new Node(s);

newNode.m=s;

Node ptr=new Node(s);

ptr=head;

int c=1;

while(c<=p)

ptr=ptr.n;

c++;

ptr.n= newNode;

newNode.n=ptr;

public void inend(int s)

Node newNode=new Node(s);

newNode.m=s;

Node ptr=new Node(s);


ptr=head;

while(ptr.n!=null)

ptr=ptr.n;

newNode.n=null;

ptr.n= newNode;

public void display()

Node Current=head;

int c=1;

if(head==null)

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

return;

System.out.println("Integers in Linked List:");

while(Current!=null)

System.out.print(Current.m+" ");

Current=Current.n;

System.out.println();
}

public static void main()throws IOException

System.out.println("Create the list");

Insertion obj=new Insertion();

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the no of nodes");

int q=Integer.parseInt(br.readLine());

for(int i=1;i<=q;i++)

System.out.println("Enter the nodes");

int f=Integer.parseInt(br.readLine());

obj.adNod(f);

obj.display();

System.out.println("Enter your choice:");

int ch=Integer.parseInt(br.readLine());

System.out.println("Enter the node you want to add:");

int r=Integer.parseInt(br.readLine());

switch(ch)

case 1:

obj.instart(r);

obj.display();

break;
case 2:

obj.inmid(r,q);

obj.display();

break;

case 3:

obj.inend(r);

obj.display();

break;

default:

System.out.println("Wrong choice");

S-ar putea să vă placă și