Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

DS-java Implementation

The document discusses the implementation of insertion and deletion operations in different types of linked lists - singly linked list (SLL), circular linked list (CLL), and doubly linked list (DLL). It includes the code for Node and LinkedList classes with methods to insert and delete nodes from various positions in each type of linked list, and a main method to test the operations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

DS-java Implementation

The document discusses the implementation of insertion and deletion operations in different types of linked lists - singly linked list (SLL), circular linked list (CLL), and doubly linked list (DLL). It includes the code for Node and LinkedList classes with methods to insert and delete nodes from various positions in each type of linked list, and a main method to test the operations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

SLL:INSERTION:

class Node

int no;

Node next;

Node(int n)

no=n;

next=null;

public class Linklist1 {

Node head,temp;

public void insertnode(int n)

Node newnode=new Node(n);

newnode.next=head;

head=newnode;

public void insertatfront(int n)

Node newnode=new Node(n);

if(head==null)

head=newnode;

else{

newnode.next=head;

head=newnode;

}
public void insertatend(int n)

temp=head;

Node newnode=new Node(n);

while(temp.next!=null)

temp=temp.next;

temp.next=newnode;

public void insertatmiddle(int n,int after)

temp=head;

Node newnode=new Node(n);

while(temp.no!=after)

temp=temp.next;

newnode.next=temp.next;

temp.next=newnode;

public void display()

temp=head;

while(temp!=null)

System.out.print(temp.no+" ");

temp=temp.next;

System.out.println("\n");

}
public static void main(String args[])

Linklist1 obj=new Linklist1();

obj.insertnode(5);

obj.insertnode(10);

obj.insertnode(15);

obj.display();

obj.insertatfront(20);

obj.insertatfront(30);

obj.display();

obj.insertatend(35);

obj.display();

obj.insertatmiddle(25, 20);

obj.display();

SLL:DELETION

class Node

int no;

Node next;

Node(int n)

no=n;

next=null;

public class Linklist1 {

Node head,temp;
public void insertnode(int n)

Node newnode=new Node(n);

newnode.next=head;

head=newnode;

public void deleteatfront()

Node curr;

curr=head;

head=head.next;

curr.next=null;

public void deleteatend()

temp=head;

Node p=head;

while(temp.next!=null)

p=temp;

temp=temp.next;

p.next=null;

public void deleteatmiddle(int after)

temp=head;

Node p=head;

while(temp.no!=after)

p=temp;
temp=temp.next;

p.next=temp.next;

public void display()

temp=head;

while(temp!=null)

System.out.print(temp.no+" ");

temp=temp.next;

System.out.println("\n");

public static void main(String args[])

Linklist1 obj=new Linklist1();

obj.insertnode(5);

obj.insertnode(10);

obj.insertnode(15);

obj.insertnode(20);

obj.display();

obj.deleteatfront();

obj.display();

obj.deleteatend();

obj.display();

obj.deleteatmiddle(15);

obj.display();

}
}

CLL:INSERTION

class cNode

int no;

cNode next;

cNode(int n)

no=n;

next=null;

public class Linklist1 {

cNode head,temp;

public void insertnode(int n)

temp=head;

cNode newnode=new cNode(n);

if(head==null)

head=newnode;

head.next=head;

else{

while(temp.next!=head)

temp=temp.next;

temp.next=newnode;
temp=newnode;

temp.next=head;

public void insertatfront(int n)

cNode newnode=new cNode(n);

if(head==null)

head=newnode;

head.next=head;

else

temp=head;

newnode.next=head;

while(temp.next!=head)

temp=temp.next;

temp.next=newnode;

head=newnode;

public void insertatend(int n)

cNode newnode=new cNode(n);

temp=head;

while(temp.next!=head)

temp=temp.next;
}

newnode.next=head;

temp.next=newnode;

public void insertatmiddle(int n,int after)

temp=head;

cNode newnode=new cNode(n);

while(temp.no!=after)

temp=temp.next;

newnode.next=temp.next;

temp.next=newnode;

public void display()

temp=head;

while(temp.next!=head)

System.out.print(temp.no+" ");

temp=temp.next;

System.out.println(temp.no);

System.out.println("\n");

public static void main(String args[])

Linklist1 obj=new Linklist1();

obj.insertnode(5);
obj.insertnode(10);

obj.insertnode(15);

obj.insertnode(20);

obj.display();

obj.insertatfront(1);

obj.display();

obj.insertatend(25);

obj.display();

obj.insertatmiddle(17, 15);

obj.display();

CLL:DELETION:

class cNode

int no;

cNode next;

cNode(int n)

no=n;

next=null;

public class Linklist1 {

cNode head,temp;

public void insertnode(int n)

temp=head;

cNode newnode=new cNode(n);


if(head==null)

head=newnode;

head.next=head;

else{

while(temp.next!=head)

temp=temp.next;

temp.next=newnode;

temp=newnode;

temp.next=head;

public void deleteatfront()

temp=head;

cNode curr;

while(temp.next!=head)

temp=temp.next;

curr=head;

head=head.next;

temp.next=head;

curr.next=null;

public void deleteatend()

temp=head;
cNode pnode=null;

while(temp.next!=head)

pnode=temp;

temp=temp.next;

pnode.next=head;

public void deleteatmiddle(int n)

cNode pnode=null;

temp=head;

while(temp.no!=n)

pnode=temp;

temp=temp.next;

pnode.next=temp.next;

public void display()

temp=head;

while(temp.next!=head)

System.out.print(temp.no+" ");

temp=temp.next;

System.out.println(temp.no);

System.out.println("\n");

}
public static void main(String args[])

Linklist1 obj=new Linklist1();

obj.insertnode(5);

obj.insertnode(10);

obj.insertnode(15);

obj.insertnode(20);

obj.deleteatmiddle(10);

obj.display();

obj.display();

obj.deleteatfront();

obj.display();

obj.deleteatend();

obj.display();

DLL:INSERTION

class dNode

int no;

dNode next;

dNode prev;

dNode(int n)

no=n;

next=null;

prev=null;
}

public class Linklist1 {

dNode temp,head=null;

public void insertnode(int n)

dNode newnode=new dNode(n);

if(head==null)

newnode.next=head;

newnode.prev=null;

head=newnode;

else

temp=head;

while(temp.next!=null)

temp=temp.next;

newnode.prev=temp;

temp.next=newnode;

newnode.next=null;

public void insertatfront(int n)

dNode newnode=new dNode(n);

if(head==null)
{

newnode.next=head;

newnode.prev=null;

head=newnode;

else

newnode.next=head;

head.prev=newnode;

head=newnode;

public void insertatmiddle(int n,int after)

dNode newnode=new dNode(n);

temp=head;

dNode pnode;

while(temp.no!=after)

temp=temp.next;

pnode=temp.next;

newnode.next=pnode;

pnode.prev=newnode;

temp.next=newnode;

newnode.prev=temp;

public void insertatend(int n)

{
dNode newnode =new dNode(n);

temp=head;

while(temp.next!=null)

temp=temp.next;

temp.next=newnode;

newnode.prev=temp;

newnode.next=null;

public void display()

temp=head;

while(temp!=null)

System.out.print(temp.no+"-->");

temp=temp.next;

System.out.println("\n");

public static void main(String args[])

Linklist1 obj=new Linklist1();

obj.insertnode(10);

obj.insertnode(20);

obj.insertnode(30);

obj.insertnode(40);

obj.display();

obj.insertatfront(5);

obj.display();
obj.insertatmiddle(25, 20);

obj.display();

obj.insertatend(45);

obj.display();

DLL:DELETION

class dNode

int no;

dNode next;

dNode prev;

dNode(int n)

no=n;

next=null;

prev=null;

public class Linklist1 {

dNode temp,head=null;

public void insertnode(int n)

dNode newnode=new dNode(n);

if(head==null)

newnode.next=head;

newnode.prev=null;
head=newnode;

else

temp=head;

while(temp.next!=null)

temp=temp.next;

newnode.prev=temp;

temp.next=newnode;

newnode.next=null;

public void deleteatfront()

head=head.next;

head.prev=null;

public void deleteatmiddle(int after)

temp=head;

dNode pnode;

dNode nnode;

while(temp.no!=after)

temp=temp.next;

pnode=temp.next;

nnode=pnode.next;
temp.next=nnode;

nnode.prev=temp;

public void deleteatend()

temp=head;

dNode pnode=null;

while(temp.next!=null)

pnode=temp;

temp=temp.next;

pnode.next=null;

public void display()

temp=head;

while(temp!=null)

System.out.print(temp.no+"-->");

temp=temp.next;

System.out.println("\n");

public static void main(String args[])

Linklist1 obj=new Linklist1();

obj.insertnode(10);

obj.insertnode(20);
obj.insertnode(30);

obj.insertnode(40);

obj.insertnode(50);

obj.display();

obj.deleteatfront();

obj.display();

obj.deleteatmiddle(30);

obj.display();

obj.deleteatend();

obj.display();

QUEUE IMPLEMENTATION:

import java.util.*;

class Node{

int data;

Node next;

Node(int data)

this.data=data;

next=null;

class A

static Node rear,front=null;

static void insert(int data)

{
Node newnode=new Node(data);

if(rear==null)

rear=front=newnode;

else {

rear.next=newnode;

rear=newnode;

static void display()

Node temp=front;

while(temp!=null)

System.out.print(temp.data+" ");

temp=temp.next;

System.out.println();

static void delete()

if(front==null)

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

else

front=front.next;

if(front==null)

front=rear=null;
}

public static void main(String args[])

insert(10);

insert(20);

insert(30);

display();

delete();

display();

CIRCULAR QUEUE USING LINKEDLIST:

import java.util.*;

class Node{

int data;

Node next;

Node(int data)

this.data=data;

next=null;

class A

static Node rear,front=null;

static void insert(int data)

{
Node newnode=new Node(data);

if(rear==null)

rear=front=newnode;

rear.next=front;

else {

rear.next=newnode;

rear=newnode;

rear.next=front;

static void display()

Node temp=front;

while(temp.next!=front)

System.out.print(temp.data+" ");

temp=temp.next;

System.out.print(temp.data);

System.out.println();

static void delete()

if(front==null)

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

else if(front==rear)

{
front=rear=null;

else{

front=front.next;

rear.next=front;

public static void main(String args[])

insert(10);

insert(20);

insert(30);

display();

insert(40);

delete();

display();

CIRCULAR QUEUE USING ARRAY:

import java.util.*;

class A

static int rear,front=-1;

static int[] insert(int d,int a[],int n)

if( front==-1)

{
rear=0;

front=0;

a[rear]=d;

else if((rear+1)%n==front)

System.out.println("Queue is full");

else

rear=(rear+1)%n;

a[rear]=d;

return a;

static void display(int a[],int n)

int i=front;

if(front==-1 && rear==-1)

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

else

while(i!=rear)

System.out.print(a[i]+" ");

i=(i+1)%n;

System.out.print(a[rear]);

}
}

static void delete(int a[],int n)

if(front==rear)

front=rear=-1;

else if(front==-1 && rear==-1)

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

else

front=(front+1)%n;

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int a[]=new int[n];

a=insert(10,a,n);

a=insert(12,a,n);

a=insert(20,a,n);

a=insert(18,a,n);

display(a,n);

System.out.println();

delete(a,n);

a=insert(30,a,n);

display(a,n);
}

TREE INPUT:

import java.util.*;

class Node{

int data;

Node left,right;

Node(int n)

data=n;

left=right=null;

class Main{

static Node root;

public static Node create(String s)

if(s.length()==0 || s.charAt(0)=='N')

return null;

String a[]=s.split(" ");

Node root=new Node(Integer.parseInt(a[0]));

Queue<Node> n=new LinkedList<Node>();

n.offer(root);

int i=1;

while(n.size()>0 && i<a.length)

Node c=n.poll();

String cv=a[i];

if(!cv.equals("N"))

{
c.left=new Node(Integer.parseInt(cv));

n.offer(c.left);

i++;

if(i>=a.length)

break;

cv=a[i];

if(!cv.equals("N"))

c.right=new Node(Integer.parseInt(cv));

n.offer(c.right);

i++;

return root;

public static void printInorder(Node root) {

if (root == null)

return;

printInorder(root.left);

System.out.print(root.data + " ");

printInorder(root.right);

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int t=sc.nextInt();

sc.nextLine();

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

String s=sc.nextLine();
root=create(s);

printInorder(root);

SORTING ALGORITHMS:

BUBBLE SORT:

import java.util.*;

class Main{

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int a[]=new int[n];

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

a[i]=sc.nextInt();

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

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

if(a[j]>a[j+1])

int temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

for(int i=0;i<n;i++)
{

System.out.print(a[i]+" ");

MERGE SORT:

import java.util.*;

class Main{

public static void mergesort(int a[],int beg,int mid,int end)

int i, j, k;

int n1 = mid - beg + 1;

int n2 = end - mid;

int LeftArray[] = new int[n1];

int RightArray[] = new int[n2];

for (i = 0; i < n1; i++)

LeftArray[i] = a[beg + i];

for (j = 0; j < n2; j++)

RightArray[j] = a[mid + 1 + j];

i = 0;

j = 0;

k = beg;

while (i < n1 && j < n2)

if(LeftArray[i] <= RightArray[j])

a[k] = LeftArray[i];

i++;

else
{

a[k] = RightArray[j];

j++;

k++;

while (i<n1)

a[k] = LeftArray[i];

i++;

k++;

while (j<n2)

a[k] = RightArray[j];

j++;

k++;

public static void merge(int a[],int beg,int end)

if(beg<end)

int mid=(beg+end)/2;

merge(a,beg,mid);

merge(a,mid+1,end);

mergesort(a,beg,mid,end);

public static void main(String args[])


{

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int a[]=new int[n];

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

a[i]=sc.nextInt();

System.out.println("Before sorting: ");

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

System.out.print(a[i]+" ");

System.out.println();

merge(a,0,n-1);

System.out.println("After sorting: ");

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

System.out.print(a[i]+" ");

SORTING ALGORITHMS  BUBBLE, MERGE, COUTING, HEAP,INSERTION,SELECTION,QUICK

LINKEDLIST SORTING  SLL,DLL,CLL

You might also like