Vous êtes sur la page 1sur 48

Reg No: 310114405010

PROGRAM:
import java.io.*;
class BFS
{
public static void main(String args[]) throws IOException
{
int i,j,k,n,ch;
System.out.println("No of vertices:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
int q[]=new int[10];
int m[]=new int[10];
int a[][]=new int[10][10];
for(i=0;i<10;i++)
m[i]=0;
System.out.println("1. DIRECTED GRAPH\n2.UNDIRECTED GRAPH");
ch=Integer.parseInt(br.readLine());
System.out.println("\n\n enter 1 if edge is present, else 0");
if(ch==1)
{
for(i=0;i<n;i++)
{
System.out.println("\n");
for(j=0;j<n;j++)
{
if(i!=j)
{
System.out.println("Edge between"+(i+1)+" and "+(j+1)+":");
a[i][j]=Integer.parseInt(br.readLine());
}
}
a[i][i]=0;
}
}
else if(ch==2)
{
for(i=0;i<n;i++)
{
System.out.println("\n");
for(j=i;j<n;j++)
{

Reg No: 310114405010

if(i!=j)
{
System.out.println("Edge between"+(i+1)+" and "+(j+1)+":");
a[i][j]=Integer.parseInt(br.readLine());
a[j][i]=a[i][j];
}
}
a[i][i]=0;
}
}
else
System.out.println("Enter a valid choice.....");
System.out.println("\n Order of the accessed nodes:\n");
q[0]=0;m[0]=1;
int u;
int node=1;
int beg1=1,beg=0;
while(node>0)
{
u=q[beg];
beg++;
System.out.println(" "+(u+1));
node--;
for(j=0;j<n;j++)
{
if(a[u][j]==1)
{
if(m[j]==0)
{
m[j]=1;
q[beg1]=j;
node++;
beg1++;
}
}
}
}
}
}

Reg No: 310114405010

OUTPUT:
No of vertices: 5
1. DIRECTED GRAPH
2. UNDIRECTED GRAPH
2
Enter 1 if edge is present, else 0
Edge between 1 and 2: 1
Edge between 1 and 3: 1
Edge between 1 and 4: 0
Edge between 1 and 5: 0
Edge between 2 and 3: 0
Edge between 2 and 4: 1
Edge between 2 and 5: 1
Edge between 3 and 4: 0
Edge between 3 and 5: 0
Edge between 4 and 5: 0
Order of accessed nodes:
1
2
3
4
5

Reg No: 310114405010

PROGRAM:
import java.io.*;
class DFS
{
static void dfs(int a[][],int m[],int i,int n)
{
int j;
System.out.println("\t"+(i+1));
m[i]=1;
for(j=0;j<n;j++)
if(a[i][j]==1&& m[j]==0)
dfs(a,m,j,n);
}
public static void main(String args[]) throws IOException
{
int n,i,j;
System.out.println("No of vertices");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
int m[]=new int[n];
int a[][]=new int[n][n];
for(i=0;i<n;i++)
{
m[i]=0;
}
System.out.println("\n \n Enter 1 if edge is present, else 0");
for(i=0;i<n;i++)
{
System.out.println("\n");
for(j=0;j<n;j++)
{
if(i!=j)
{
System.out.println("Edge between"+(i+1)+" and"+(j+1)+":");
a[i][j]=Integer.parseInt(br.readLine());
}
}
a[i][i]=0;
}
System.out.println("\n order of accesed nodes:\n");

Reg No: 310114405010

for(i=0;i<n;i++)
if(m[i]==0)
dfs(a,m,i,n);
}
}

Reg No: 310114405010

OUTPUT:
Enter the no of vertices: 7
Enter 1 if edge is present, else 0
Edge between 1 and 2: 1
Edge between 1 and 3: 0
Edge between 1 and 4: 0
Edge between 1 and 5: 0
Edge between 1 and 6: 1
Edge between 1 and 7: 0
Edge between 2 and 1: 0
Edge between 2 and 3: 1
Edge between 2 and 4: 1
Edge between 2 and 5: 0
Edge between 2 and 6: 0
Edge between 2 and 7: 0
Edge between 3 and 1: 0
Edge between 3 and 2: 0
Edge between 3 and 4: 0
Edge between 3 and 5: 0
Edge between 3 and 6: 0
Edge between 3 and 7: 0
Edge between 4 and 1: 1
Edge between 4 and 2: 0
Edge between 4 and 3: 0
Edge between 4 and 5: 0
Edge between 4 and 6: 0
Edge between 4 and 7: 0
Edge between 5 and 1: 0
Edge between 5 and 2: 1
Edge between 5 and 3: 0
Edge between 5 and 4: 0
Edge between 5 and 6: 0
Edge between 5 and 7: 1
Edge between 6 and 1: 1
Edge between 6 and 2: 0

Reg No: 310114405010

Edge between 6 and 3: 1


Edge between 6 and 4: 0
Edge between 6 and 5: 0
Edge between 6 and 7: 0
Edge between 7 and 1: 0
Edge between 7 and 2: 0
Edge between 7 and 3: 0
Edge between 7 and 4: 1
Edge between 7 and 5: 1
Edge between 7 and 6: 0
Order of accessed nodes:
1
2
3
4
6
5
7

Reg No: 310114405010

PROGRAM:
import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class Vertex implements Comparable<Vertex>
{
public final String name;
public Edge[] adjacencies;
public double minDistance = Double.POSITIVE_INFINITY;
public Vertex previous;
public Vertex(String argName) { name = argName; }
public String toString() { return name; }
public int compareTo(Vertex other)
{
return Double.compare(minDistance, other.minDistance);
}
}
class Edge
{
public final Vertex target;
public final double weight;
public Edge(Vertex argTarget, double argWeight)
{ target = argTarget; weight = argWeight; }
}
public class Dijkstra1
{
public static void computePaths(Vertex source)
{
source.minDistance = 0.;
PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
vertexQueue.add(source);
while (!vertexQueue.isEmpty())
{
Vertex u = vertexQueue.poll();
for (Edge e : u.adjacencies)
{
Vertex v = e.target;
double weight = e.weight;
double distanceThroughU = u.minDistance + weight;

Reg No: 310114405010

if (distanceThroughU < v.minDistance)


{
vertexQueue.remove(v);
v.minDistance = distanceThroughU ;
v.previous = u;
vertexQueue.add(v);
}
}
}
}
public static List<Vertex> getShortestPathTo(Vertex target)
{
List<Vertex> path = new ArrayList<Vertex>();
for (Vertex vertex = target; vertex != null; vertex = vertex.previous)
path.add(vertex);
Collections.reverse(path);
return path;
}
public static void main(String[] args)
{
Vertex v0 = new Vertex("Redvile");
Vertex v1 = new Vertex("Blueville");
Vertex v2 = new Vertex("Greenville");
Vertex v3 = new Vertex("Orangeville");
Vertex v4 = new Vertex("Purpleville");
v0.adjacencies = new Edge[]{ new Edge(v1, 5),new Edge(v2, 10),new Edge(v3, 8) };
v1.adjacencies = new Edge[]{ new Edge(v0, 5),new Edge(v2, 3),new Edge(v4, 7) };
v2.adjacencies = new Edge[]{ new Edge(v0, 10),new Edge(v1, 3) };
v3.adjacencies = new Edge[]{ new Edge(v0, 8),new Edge(v4, 2) };
v4.adjacencies = new Edge[]{ new Edge(v1, 7),new Edge(v3, 2) };
Vertex[] vertices = { v0, v1, v2, v3, v4 };
computePaths(v0);
for (Vertex v : vertices)
{
System.out.println("Distance to " + v + ": " + v.minDistance);
List<Vertex> path = getShortestPathTo(v);
System.out.println("Path: " + path);
}
}
}

Reg No: 310114405010

OUTPUT:
Distance to Redville: 0.0
Path: [Redville]
Distance to Blueville: 0.0
Path: [Redville, Blueville]
Distance to Greenville: 0.0
Path: [Redville, Blueville, Greenville]
Distance to Orangeville: 0.0
Path: [Redville,Orangeville]
Distance to Purpleville: 0.0
Path: [Redville,Orangeville,Purpleville]

Reg No: 310114405010

PROGRAM:
import java.io.*;
public class Kruskal1
{
static int path[];
static int n,m,mincost,i,j;
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter yhe no of vertices in graph");
n=Integer.parseInt(br.readLine());
System.out.println("Enter the no of edges in graph:");
m=Integer.parseInt(br.readLine());
path=new int[n+1];
Edge e[]=new Edge[m];
Edge t=new Edge();
System.out.println("Enter 2 vertices and weight of the edge");
for(i=0;i<m;i++)
{
e[i]=new Edge();
System.out.println("First vertex:");
e[i].u=Integer.parseInt(br.readLine());
System.out.println("Second vertex:");
e[i].v=Integer.parseInt(br.readLine());
System.out.println("Weight:");
e[i].wt=Integer.parseInt(br.readLine());
}
for(i=0;i<=m-1;i++)
{
for(j=0;j<m-i-1;j++)
{
if(e[j].wt>e[j+1].wt)
{
t=e[j];
e[j]=e[j+1];
e[j+1]=t;
}
}
}
for(i=1;i<=n;i++)

Reg No: 310114405010

{
path[i]=0;
}
i=0;j=0;mincost=0;
System.out.println();
while((i!=n-1)&&(j!=m))
{
System.out.print("Edge("+e[i].u+","+e[i].v+")with weight"+e[i].wt+" ");
if(checkCycle(e[i]))
{
mincost=mincost+e[i].wt;
i++;
System.out.print("is selected");
}
else
{
System.out.print("is discared");
}
j++;
System.out.println();
}
if(i!=n-1)
{
System.out.println("Minimum spanning tree cannot be formed");
}
System.out.println("Minimum cost id:"+mincost);
}
public static boolean checkCycle(Edge e)
{
int u=e.u,v=e.v;
while(path[u]>0)
u=path[u];
while(path[v]>0)
v=path[v];
if(u!=v)
{
path[u]=v;
return true;
}
return false;
}

Reg No: 310114405010

static class Edge


{
int u,v,wt;
}
}

Reg No: 310114405010

OUTPUT:
Enter the no of vertices in graph: 4
Enter the no of edges in graph: 5
Enter 2 vertices and weight of the graph:
First vertex: 1
Second vertex: 2
Weight: 2
First vertex: 1
Second vertex: 3
Weight: 3
First vertex: 1
Second vertex: 4
Weight: 1
First vertex: 2
Second vertex: 4
Weight: 2
First vertex: 3
Second vertex: 4
Weight: 1
Edge (3,4) with weight 1 is selected
Edge (1,4) with weight 1 is selected
Edge (1,2) with weight 2 is selected
Minimum cost is: 4

Reg No: 310114405010

PROGRAM:
import java.util.*;
class Graph
{
int g[][];
int v,e;
int d[],p[],visited[];
void createGraph()
{
int a,b,w;
Scanner s=new Scanner(System.in);
System.out.println("Enter the no of vertices:");
v=s.nextInt();
System.out.println("Enter the no of edges:");
e=s.nextInt();
g=new int[v+1][v+1];
for(int i=1;i<=v;i++)
for(int j=1;j<=v;j++)
g[i][j]=0;
for(int i=1;i<=e;i++)
{
System.out.println("Enter edge information");
a=s.nextInt();
b=s.nextInt();
System.out.println("Enter the weight of this edge:");
w=s.nextInt();
g[a][b]=g[b][a]=w;
}
}
void callPrim()
{
visited=new int[v+1];
d=new int[v+1];
p=new int[v+1];
for(int i=1;i<=v;i++)
p[i]=visited[i]=0;
for(int i=1;i<=v;i++)
d[i]=32767;
prim();
}

Reg No: 310114405010

void prim()
{
int c=1,current=1,mincost=0;
visited[current]=1;
d[current]=0;
while(c!=v)
{
for(int i=1;i<=v;i++)
{
if(g[current][i]!=0&&visited[i]!=1)
if(g[current][i]<d[i])
{
d[i]=g[current][i];
p[i]=current;
}
}
int min=32767;
for(int i=1;i<=v;i++)
{
if(visited[i]!=1&&d[i]<min)
{
min=d[i];
current=i;
}
}
visited[current]=1;
c=c+1;
}
for(int i=1;i<=v;i++)
mincost+=d[i];
System.out.println("minimum cost:"+mincost);
}
}
public class Prim
{
public static void main(String args[])
{
Graph g=new Graph();
g.createGraph();
g.callPrim();
}}

Reg No: 310114405010

OUTPUT:
Enter the no of vertices: 6
Enter the no of edges: 9
Enter edge information:
1
2
Enter the weight of this edge:
1
Enter edge information:
2
4
Enter the weight of this edge:
15
Enter edge information:
4
5
Enter the weight of this edge:
6
Enter edge information:
5
3
Enter the weight of this edge:
6
Enter edge information:
3
1
Enter the weight of this edge:
10
Enter edge information:
6
1
Enter the weight of this edge:
2
Enter edge information:
6
2
Enter the weight of this edge:
2
Enter edge information:
6

Reg No: 310114405010

5
Enter the weight of this edge:
4
Enter edge information:
6
4
Enter the weight of this edge:
8
Minimum cost: 19

Reg No: 310114405010

PROGRAM:
import java.util.*;
import java.text.*;
class TSP
{
int weight[][],n,tour[],finalCost;
final int INF=1000;
public TSP()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter no. of nodes:=>");
n=s.nextInt();
weight=new int[n][n];
tour=new int[n-1];
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(i!=j)
{
System.out.print("Enter weight of "+(i+1)+" to "+(j+1)+":=>");
weight[i][j]=s.nextInt();
weight[j][i]=weight[i][j];
}
}
}
System.out.println("Starting node assumed to be node 1.");
eval();
}
public int COST(int currentNode,int inputSet[],int setSize)
{
if(setSize==0)
return weight[currentNode][0];
int min=INF,minindex=0;
int setToBePassedOnToNextCallOfCOST[]=new int[n-1];
for(int i=0;i<setSize;i++)
{
int k=0;//initialise new set
for(int j=0;j<setSize;j++)
{

Reg No: 310114405010

if(inputSet[i]!=inputSet[j])
setToBePassedOnToNextCallOfCOST[k++]=inputSet[j];
}
int temp=COST(inputSet[i],setToBePassedOnToNextCallOfCOST,setSize-1);
if((weight[currentNode][inputSet[i]]+temp) < min)
{
min=weight[currentNode][inputSet[i]]+temp;
minindex=inputSet[i];
}
}
return min;
}
public int MIN(int currentNode,int inputSet[],int setSize)
{
if(setSize==0)
return weight[currentNode][0];
int min=INF,minindex=0;
int setToBePassedOnToNextCallOfCOST[]=new int[n-1];
for(int i=0;i<setSize;i++)//considers each node of inputSet
{
int k=0;
for(int j=0;j<setSize;j++)
{
if(inputSet[i]!=inputSet[j])
setToBePassedOnToNextCallOfCOST[k++]=inputSet[j];
}
int temp=COST(inputSet[i],setToBePassedOnToNextCallOfCOST,setSize-1);
if((weight[currentNode][inputSet[i]]+temp) < min)
{
min=weight[currentNode][inputSet[i]]+temp;
minindex=inputSet[i];
}
}
return minindex;
}
public void eval()
{
int dummySet[]=new int[n-1];
for(int i=1;i<n;i++)
dummySet[i-1]=i;
finalCost=COST(0,dummySet,n-1);

Reg No: 310114405010

constructTour();
}
public void constructTour()
{
int previousSet[]=new int[n-1];
int nextSet[]=new int[n-2]; for(int i=1;i<n;i++)
previousSet[i-1]=i;
int setSize=n-1;
tour[0]=MIN(0,previousSet,setSize);
for(int i=1;i<n-1;i++)
{
int k=0;
for(int j=0;j<setSize;j++)
{
if(tour[i-1]!=previousSet[j])
nextSet[k++]=previousSet[j];
}
--setSize;
tour[i]=MIN(tour[i-1],nextSet,setSize);
for(int j=0;j<setSize;j++)
previousSet[j]=nextSet[j];
}
display();
}
public void display()
{
System.out.println();
System.out.print("The tour is 1-");
for(int i=0;i<n-1;i++)
System.out.print((tour[i]+1)+"-");
System.out.print("1");
System.out.println();
System.out.println("The final cost is "+finalCost);
}
}
class TSPExp
{
public static void main(String args[])
{
TSP obj=new TSP();
}}

Reg No: 310114405010

OUTPUT:
Enter no of nodes:
4
Enter weight of 1 to 2:=>2
Enter weight of 1 to 3:=>3
Enter weight of 1 to 4:=>1
Enter weight of 2 to 3:=>3
Enter weight of 2 to 4:=>2
Enter weight of 3 to 4:=>1
Starting node assumed to be 1
The tour is 1-2-3-4-1
Final cost: 7

Reg No: 310114405010

PROGRAM:
import java.io.*;
class knapo
{
void knapsack(int w[],int p[],int n,int m)
{
int s[][]=new int[n+1][m+1];
for(int wt=0;wt<=m;wt++)
s[0][wt]=0;
for(int i=0;i<=n;i++)
s[i][0]=0;
for(int i=1;i<=n;i++)
for(int j=0;j<=m;j++)
{
if(w[i]<=j)
{
if((p[i]+s[i-1][j-w[i]])>s[i-1][j])
s[i][j]=p[i]+s[i-1][j-w[i]];
else
s[i][j]=s[i-1][j];
}
else
s[i][j]=s[i-1][j];
}
knapsack_items(s,n,m,w,p);
}
void knapsack_items(int s[][],int n,int m,int w[],int p[])
{
int ans[]=new int[n+1];
int i=n;
int k=m;
int profit=0;
while(i>0&&k>0)
{
if(s[i][k]!=s[i-1][k])
{
ans[i]=1;
profit+=p[i];
k=k-w[i];
}

Reg No: 310114405010

i--;
}
System.out.println("values of the element");
for(i=1;i<=n;i++)
System.out.println("Element"+i+":"+ans[i]);
System.out.println("profit="+profit);
}
}
class knapmain
{
public static void main(String args[]) throws IOException
{
knapo ob=new knapo();
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("How many object");
int n=Integer.parseInt(obj.readLine());
System.out.print("enter the capacity of the bag");
int m=Integer.parseInt(obj.readLine());
int p[],w[];
p=new int[n+1];
w=new int[m+1];
System.out.println("enter the profit and weight");
for(int i=1;i<=n;i++)
{
System.out.print("P"+i+":");
p[i]=Integer.parseInt(obj.readLine());
System.out.print("W"+i+":");
w[i]=Integer.parseInt(obj.readLine());
System.out.println();
}
ob.knapsack(w,p,n,m);
}
}

Reg No: 310114405010

OUTPUT:
How many object?
3
Enter the capacity of the bag:10
Enter the profit and weight
P1: 2
W1: 3
P2: 4
W2: 5
P3: 6
W3: 8
Values of the element
Element 1: 1
Element 2: 1
Element 3: 0
Profit: 6

Reg No: 310114405010

PROGRAM:
import java.io.*;
import java.util.*;
public class Queens
{
public static boolean isConsistent(int [] q,int n)
{
for(int i=0;i<n;i++)
{
if(q[i]==q[n])
return false;
if((q[i]-q[n])==(n-i))
return false;
if((q[n]-q[i])==(n-i))
return false;
}
return true;
}
public static void printQueens(int[] q)
{
int N=q.length;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(q[i]==j)
System.out.print("Q");
else
System.out.print("* ");
}
System.out.println();
}
System.out.println();
}
public static void enumerate(int N)
{
int[] a=new int[N];
enumerate(a,0);
}
public static void enumerate(int[] q,int n)

Reg No: 310114405010

{
int N=q.length;
if(n==N)
printQueens(q);
else
{
for(int i=0;i<N;i++)
{
q[n]=i;
if(isConsistent(q,n))
enumerate(q,n+1);
}
}
}
public static void main(String args[])
{
Scanner S=new Scanner(System.in);
System.out.print("Enter the no of Queens:");
int N=S.nextInt();
enumerate(N);
}
}

Reg No: 310114405010

OUTPUT:
Enter the no of Queens:4
*Q**
***Q
Q***
**Q*
**Q*
Q***
***Q
*Q**

Reg No: 310114405010

PROGRAM:
import java.io.*;
public class GraphColoring
{
static int G[][];
static int x[];
static int n,m;
static boolean found=false;
public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[]) throws IOException
{
System.out.println("\t\t\t\tGRAPH COLORING");
System.out.print("Enter the no of vertices:");
n=Integer.parseInt(br.readLine());
G=new int[n+1][n+1];
x=new int[n+1];
System.out.println("if edge b/w the foll vertices enter 1 else enter 0:\n");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if((i!=j)&&(i<j))
{
System.out.print(i+" and "+j+":");
G[i][j]=G[j][i]=Integer.parseInt(br.readLine());
}
if(i==j)
G[i][j]=0;
}
}
System.out.println("Enter the no of colors available:");
m=Integer.parseInt(br.readLine());
System.out.println("Solution:");
mColoring(1);
if(found==false)
System.out.println("Noo solution possible:");
}
static void mColoring(int k)
{
while(true)

Reg No: 310114405010

{
nextValue(k);
if(x[k]==0)
return;
if(k==n)
{
for(int i=1;i<=k;i++)
System.out.print(x[i]+" ");
System.out.println();
found=true;
return;
}
else
mColoring(k+1);
}
}
static void nextValue(int k)
{
int j;
while(true)
{
x[k]=(x[k]+1)%(m+1);
if(x[k]==0)
return;
for(j=1;j<=n;j++)
if((G[k][j]!=0)&&(x[k]==x[j]))
break;
if(j==n+1)
return;
}
}
}

Reg No: 310114405010

OUTPUT:
Enter the no of vertices: 5
If edge between the foll vertices, enter 1 else enter 0:
1 and 2: 1
1 and 3: 1
1 and 4: 0
1 and 5: 1
2 and 3: 1
2 and 4: 0
2 and 5: 1
3 and 4: 1
3 and 5: 0
4 and 5: 1
Enter the no of colors available: 3
Solution:
12313
13212
21313
23121
31212
32121

Reg No: 310114405010

PROGRAM:
import java.util.*;
public class Quicksort
{
static void dispaly(int x[],int n)
{
int i;
System.out.println(" ");
for(i=0;i<n;i++)
{
System.out.println("\t"+x[i]);
}
System.out.println(" ");
}
static int partition(int x[],int lb,int ub)
{
int a,down,temp,up,pj;
a=x[lb];
up=ub;
down=lb;
while(down<up)
{
while(x[down]<=a&&down<up)
down=down+1;
while(x[up]>a)
up=up-1;
if(down<up)
{
temp=x[down];
x[down]=x[up];
x[up]=temp;
}
}
x[lb]=x[up];
x[up]=a;
pj=up;
return (pj);
}
static void quick(int a[],int lb,int ub)
{

Reg No: 310114405010

Stack s=new Stack();


s.push(lb);
s.push(ub);
while(!s.empty())
{
ub=(Integer)s.pop();
lb=(Integer)s.pop();
if(ub<=lb)
continue;
int i=partition(a,lb,ub);
if(i-lb>ub-i)
{
s.push(lb);
s.push(i-1);
}
s.push(i+1);
s.push(ub);
if(ub-i>=i-lb)
{
s.push(lb);
s.push(i-1);
}
}
}
public static void main(String args[])
{
int i,n=10;
int x[]=new int[10];
Scanner in=new Scanner(System.in);
System.out.println("Enter the 10 elementes to be sorted:");
for(i=0;i<n;i++)
x[i]=in.nextInt();
quick(x,0,n-1);
System.out.println("The sorted elements are:");
for(i=0;i<n;i++)
System.out.print(x[i]+" ");
}
}

Reg No: 310114405010

OUTPUT:
Enter 10 elements to sort:
10 9 8 7 6 5 4 3 2 1
The sorted elements are:
1 2 3 4 5 6 7 8 9 10

Reg No: 310114405010

PROGRAM:
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;
public class ConcurrentList
{
public static void main(String args[])
{
LinkedList ll=new LinkedList();
System.out.println("Enter the element to push(max sixe=5)");
Scanner scan=new Scanner(System.in);
int scaninput[]=new int[10];
for(int i=0;i<5;i++)
ll.add(scan.nextInt());
for(int p=0;p<5;p++)
System.out.println("Element in["+p+"] index:"+ll.get(p));
ll.addFirst("0");
System.out.println("After inserting 0 at the beginning, the linked list contains\n:"+ll);
ll.add(2,"2.5");
System.out.println("After inserting 2.5 in the second index, the linked list contains\n:"+ll);
ll.addLast("6");
System.out.println("After inserting 6 an the end, the linked list contains\n:"+ll);
ListIterator itr=ll.listIterator();
System.out.println("Iterating the elements of linked ilst in forward direction");
while(itr.hasNext())
System.out.println(itr.next());
System.out.println("Iterating the elements of Linked List in reverse direction:");
while(itr.hasPrevious())
System.out.println(itr.previous());
System.out.println("deleting the element which is not on the list:");
boolean isRemoved=ll.remove("50");
System.out.println("Is 50 removed from the list?:"+isRemoved);
System.out.println("Now the linked list contains:"+ll);
Object obj=ll.remove(2);
System.out.println(obj+"has been removed from linket list");
System.out.print("After clearing the linked list:");
ll.clear();
System.out.println("Linked list contains:"+ll);
}
}

Reg No: 310114405010

OUTPUT:
Enter the element u want to insert (max size=5)
12345
Element in [0] index: 1
Element in [1] index: 2
Element in [2] index: 3
Element in [3] index: 4
Element in [4] index: 5
After inserting 0 at the beginning, the linked list contains: [0, 1, 2, 3, 4, 5]
After inserting 2.5in the second index, the linked list contains: [0, 1, 2.5, 2, 3, 4, 5]
After inserting 6 at the end, the linked list contains: [0, 1, 2.5, 2, 3, 4, 5, 6]
Iterating the elements of the linked list in forward direction:
0
1
2.5
2
3
4
5
6
Iterating the elements of the linked list in reverse direction:
6
5
4
3
2
2.5
1
0
Deleting the element which is not in the list:
Is 50 removed from the list?: false
Now linked list contains: [0, 1, 2.5, 2, 3, 4, 5, 6]
2.5 has been removed from the lined list
After clearing the linked list:
Lined list contains: []

Reg No: 310114405010

PROGRAM:
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Scanner;
class QueueNext
{
int arr[],size;
public QueueNext(int maxsize)
{
size=maxsize;
this.arr=new int[maxsize];
}
int front=-1;
int rear=0;
public void enqueue(int data)
{
this.arr[rear]=data;
rear=rear+1;
}
public int dequeue()
{
front=front+1;
if(front==0)
return -1;
return this.arr[front];
}
public int queueSize()
{
return size;
}
}
class QueueOperation implements Runnable
{
int size;
QueueNext myqueue;
String name;
int option;
int input[];
public QueueOperation(QueueNext que,String name,int option,int input[])
{

Reg No: 310114405010

this.myqueue=que;
this.name=name;
this.option=option;
if(option==1)
this.input=input;
}
public void run()
{
synchronized(myqueue)
{
if(option==1)
enqueue();
else
dequeue();
}
}
public void enqueue()
{
System.out.println("***********************************************");
for(int i=0;i<input.length;i++)
{
myqueue.enqueue(input[i]);
System.out.println("Enqueue(insert) input to the "+name+":"+input[i]);
}
}
public void dequeue()
{
int data,count=0;
System.out.println("************************************************");
while(count<myqueue.size)
{
data=myqueue.dequeue();
if(data!=-1)
System.out.println("Dequeue(delete) input from the "+name+":"+data);
count++;
}
}
}
public class ConcurrentQueue
{
public static void main(String args[])

Reg No: 310114405010

{
int queuesize=5;
DataInputStream get=new DataInputStream(System.in);
int ch;
System.out.println("Enter the elements you want to enqueue(max size=5): ");
Scanner scan=new Scanner(System.in);
int scaninput[]=new int[5];
int input2[]=new int[5];
for(int i=0;i<5;i++)
{
scaninput[i]=scan.nextInt();
}
for(int p=0;p<5;p++)
{
System.out.println("Element in the ["+p+"] index is:"+scaninput[p]);
}
QueueNext que1=new QueueNext(queuesize);
QueueOperation stop=new QueueOperation(que1,"Concurrent Queue",1,scaninput);
QueueOperation stop1=new QueueOperation(que1,"Concurrent Queue 1",0,scaninput);
Thread t=new Thread(stop);
Thread t1=new Thread(stop1);
t.start();
t1.start();
}
}

Reg No: 310114405010

OUTPUT:
Enter the elements you want to insert (mx size=5):
12345
Element in the [0] index: 1
Element in the [1] index: 2
Element in the [2] index: 3
Element in the [3] index: 4
Element in the [4] index: 5
***************************************************
Enqueue(insert) input to the Concurrent Queue: 1
Enqueue(insert) input to the Concurrent Queue: 2
Enqueue(insert) input to the Concurrent Queue: 3
Enqueue(insert) input to the Concurrent Queue: 4
Enqueue(insert) input to the Concurrent Queue: 5
***************************************************
After dequeue(delete),front element from the Concurrent Queue1: 2
After dequeue(delete),front element from the Concurrent Queue1: 3
After dequeue(delete),front element from the Concurrent Queue1: 4
After dequeue(delete),front element from the Concurrent Queue1: 5

Reg No: 310114405010

PROGRAM:
import java.io.DataInputStream;
import java.util.Scanner;
class StackNext
{
int arr[],size;
public StackNext(int maxsize)
{
size=maxsize;
this.arr=new int[maxsize];
}
int top=0;
public void push(int data)
{
this.arr[top]=data;
top=top+1;
}
public int pop()
{
if(top==0)
return -1;
top=top-1;
return this.arr[top];
}
public int stacksize()
{
return size;
}
}
class StackOperation implements Runnable
{
int size;
StackNext mystack;
String name;
int option;
int input[];
public StackOperation(StackNext stk,String name,int option,int input[])
{
this.mystack=stk;
this.name=name;

Reg No: 310114405010

this.option=option;
if(option==1)
this.input=input;
}
public void run()
{
synchronized(mystack)
{
if(option==1)
pushData();
else
popData();
}
}
public void pushData()
{
for(int i=0;i<input.length;i++)
{
mystack.push(input[i]);
System.out.println("Stack====>"+name+"push input====>"+input[i]);
}
}
public void popData()
{
int data,count=0;
while(count<mystack.size)
{
data=mystack.pop();
if(data!=-1)
{
System.out.println("Stack===>"+name+"pop out===>"+data);
count++;
}
}
}
}
public class ConcurrentStack
{
public static void main(String args[])
{
int stacksize=10;

Reg No: 310114405010

int input[]={1,2,3,4,5,6,7,8,9,10};
int input1[]={1,2,3,4,5};
int input2[]={6,7,8,9,10};
DataInputStream get=new DataInputStream(System.in);
int ch;
System.out.println("Enter the element u want to push=max size=10");
Scanner scan=new Scanner(System.in);
int scaninput[]=new int[10];
for(int i=0;i<10;i++)
{
scaninput[i]=scan.nextInt();
}
for(int p=0;p<10;p++)
{
System.out.println("This is "+p+"th element that you entered "+scaninput[p]);
}
StackNext stk1=new StackNext(stacksize);
StackOperation stop=new StackOperation(stk1,"TestStack",1,scaninput);
StackOperation stop1=new StackOperation(stk1,"TestStack1",0,input2);
Thread t=new Thread(stop);
Thread t1=new Thread(stop1);
t.start();
t1.start();
}
}

Reg No: 310114405010

OUTPUT:
Enter the element you want to push (max size=10):
1 2 3 4 5 6 7 8 9 10
0 th element that you entered: 1
1 th element that you entered: 2
2 th element that you entered: 3
3 th element that you entered: 4
4 th element that you entered: 5
5 th element that you entered: 6
6 th element that you entered: 7
7 th element that you entered: 8
8 th element that you entered: 9
9 th element that you entered: 10
Stack --> TestStack push input--->1
Stack --> TestStack push input--->2
Stack --> TestStack push input--->3
Stack --> TestStack push input--->4
Stack --> TestStack push input--->5
Stack --> TestStack push input--->6
Stack --> TestStack push input--->7
Stack --> TestStack push input--->8
Stack --> TestStack push input--->9
Stack --> TestStack push input--->10
Stack --> TestStack1 pop output--->10
Stack --> TestStack1pop output--->9
Stack --> TestStack1 pop output--->8
Stack --> TestStack1 pop output--->7
Stack --> TestStack1 pop output--->6
Stack --> TestStack1 pop output--->5
Stack --> TestStack1 pop output--->4
Stack --> TestStack1 pop output--->3
Stack --> TestStack1 pop output--->2
Stack --> TestStack1 pop output--->1

Reg No: 310114405010

PROGRAM:
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Scanner;
class QueueNext
{
int arr[],size;
public QueueNext(int maxsize)
{
size=maxsize;
this.arr=new int[maxsize];
}
int front=-1;
int rear=0;
public void enqueue(int data)
{
this.arr[rear]=data;
rear=rear+1;
}
public int dequeue()
{
front=front+1;
if(front==0)
return -1;
return this.arr[front];
}
public int queueSize()
{
return size;
}
}
class QueueOperation implements Runnable
{
int size;
QueueNext myqueue;
String name;
int option;
int input[];
public QueueOperation(QueueNext que,String name,int option,int input[])
{

Reg No: 310114405010

this.myqueue=que;
this.name=name;
this.option=option;
if(option==1)
this.input=input;
}
public void run()
{
synchronized(myqueue)
{
if(option==1)
enqueue();
else
dequeue();
}
}
public void enqueue()
{
System.out.println("***************************************************");
for(int i=0;i<input.length;i++)
{
myqueue.enqueue(input[i]);
System.out.println("queue=====>"+name+"enqueue input====>:"+input[i]);
}
}
public void dequeue()
{
int data,count=0;
System.out.println("***************************************************");
while(count<myqueue.size)
{
data=myqueue.dequeue();
if(data!=-1)
System.out.println("queue=====>"+name+"dequeue output=====>:"+data);
count++;
}
}
}
public class QueueThread
{
public static void main(String args[])

Reg No: 310114405010

{
int queuesize=10;
DataInputStream get=new DataInputStream(System.in);
int ch;
System.out.println("Enter the elements you want to enqueue=max size 10:");
Scanner scan=new Scanner(System.in);
int scaninput[]=new int[10];
int input2[]=new int[10];
for(int i=0;i<10;i++)
{
scaninput[i]=scan.nextInt();
}
for(int p=0;p<10;p++)
{
System.out.println("This is ["+p+"]th element that u have enetered:"+scaninput[p]);
}
QueueNext que1=new QueueNext(queuesize);
QueueOperation stop=new QueueOperation(que1,"Test Queue",1,scaninput);
QueueOperation stop1=new QueueOperation(que1,"Test Queue 1",0,input2);
Thread t=new Thread(stop);
Thread t1=new Thread(stop1);
t.start();
t1.start();
}
}

Reg No: 310114405010

OUTPUT:
Enter the elements you want to enqueue (max size=10):
1 2 3 4 5 6 7 8 9 10
0 th element that you entered: 1
1 th element that you entered: 2
2 th element that you entered: 3
3 th element that you entered: 4
4 th element that you entered: 5
5 th element that you entered: 6
6 th element that you entered: 7
7 th element that you entered: 8
8 th element that you entered: 9
9 th element that you entered: 10
Queue--> TestQueue enqueue input--->1
Queue--> TestQueue enqueue input--->2
Queue --> TestQueue enqueue input--->3
Queue --> TestQueue enqueue input--->4
Queue --> TestQueue enqueue input--->5
Queue --> TestQueue enqueue input--->6
Queue --> TestQueue enqueue input--->7
Queue --> TestQueue enqueue input--->8
Queue --> TestQueue enqueue input--->9
Queue --> TestQueue enqueue input--->10
Queue --> TestQueue1 dequeue output--->1
Queue --> TestQueue1 dequeue output--->2
Queue --> TestQueue1 dequeue output--->3
Queue --> TestQueue1 dequeue output--->4
Queue --> TestQueue1 dequeue output--->5
Queue --> TestQueue1 dequeue output--->6
Queue --> TestQueue1 dequeue output--->7
Queue --> TestQueue1 dequeue output--->8
Queue --> TestQueue1 dequeue output--->9
Queue --> TestQueue1 dequeue output--->10

Vous aimerez peut-être aussi