Write the programs for Linked List (Insertion and Deletion)
operations

Answer Posted / vikram_tp

import java.util.*;
class Node
{
int n;
Node nx;
Node()
{
nx=null;
}
}

class Linklist
{
Node f,l;
Linklist()
{
f=l=null;
}

void inserte(int x)
{
Node nr=new Node();
nr.nx=null;
nr.n=x;
if(f==null)
f=nr;
else
l.nx=nr;
l=nr;
}

void insertf(int x)
{
Node nr=new Node();
nr.nx=f;
nr.n=x;
f=nr;
}

void insertb(int x, int c)
{
Node nr=new Node();
nr.n=x;
Node p;
p=f;
while(p.n!=c)
p=p.nx;
nr.nx=p.nx;
p.nx=nr;
}

void delete(int x)
{
Node p,q;
p=q=f;
while(p!=null&&p.n!=x)
{
q=p;
p=p.nx;
}
if(p!=null)
{
if(p==f)
f=f.nx;
else
if(p==l)
{
q.nx=null;
l=q;
}
else
q.nx=p.nx;
}
else
System.out.println("element not present ");
}

void search(int x)
{
Node p;
p=f;
while(p!=null&&p.n!=x)
p=p.nx;
if(p==null)
System.out.println("element not found");
else
System.out.println("element found");
}

void display()
{
Node p;
p=f;
if(p==null)
{
System.out.println("Link list is empty ");
return;
}
while(p!=null)
{
System.out.println(p.n);
p=p.nx;
}
}
}

public class Link
{
public static void main(String args[])
{
int ch1,ch2,n;
Linklist L=new Linklist();
Scanner bv=new Scanner(System.in);
do
{
System.out.println(" menu\n1. insert a node\n2. Delete a node\n3. Display\n4. Search\n5. Exit");
System.out.print("enter ur choice : ");
ch1=bv.nextInt();
switch(ch1)
{
case 1:
System.out.print("enter the data : ");
n=bv.nextInt();
System.out.println(" menu\n1. insert at start\n2. insert at end\n3. insert in between");
System.out.print("enter ur choice : ");
ch2=bv.nextInt();
switch(ch2)
{
case 1: L.insertf(n);
break;
case 2: L.inserte(n);
break;
case 3:
System.out.print("enter the data after which you want to insert : ");
int c=bv.nextInt();
L.insertb(n,c);
break;
default:
System.out.println("wrong choice");
break;
}
break;
case 2:
System.out.print("enter the data : ");
n=bv.nextInt();
L.delete(n);
break;
case 3:
L.display();
break;
case 4:
System.out.print("enter the data : ");
n=bv.nextInt();
L.search(n);
break;
case 5 :break;
default :
System.out.println("wrong choice entered");
break;
}
}while(ch1!=5);
}
}

Is This Answer Correct ?    10 Yes 22 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is list an array?

621


How memory is reserved using a declaration statement ?

904


What is the difference between Array and LinkedList?

779


Sorting is not possible by using which of the methods?

695


What is a b+ tree? Explain its uses.

735


What is a data structure? What are the types of data structures?

771


what is Singly Linked list?

787


How do you declare An array of three pointers to chars

676


Is heap sort faster than quicksort?

647


Why null is not allowed in treemap?

703


Mention the advantages of representing stacks using linked lists than arrays?

699


What can be stored in an arraylist?

685


Write a Program for Insert in a sorted list

704


What are the advantages of bubble sort?

720


Which type of memory allocation is referred for linked list?

995