Posts

Implement Linked List using Java

import java.util.*; import java.io.*; import java.lang.*; class Linked { int val; Linked next; Linked(int g) { val=g; next=null; } void addNode(int p) { Linked ob=new Linked(p); Linked t; t=this; while(t.next!=null) { t=t.next; } t.next=ob; } void disp() { Linked t; t=this; while(t!=null) { System.out.println(t.val); t=t.next; } } } class LinkedPrg { public static void main(String []ar) throws IOException { Scanner cin=new Scanner(System.in); int p,g; char ch='y'; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the value for the head node : "); g=Integer.parseInt(br.readLine()); Linked ob=new Linked(g); while(true) { System.out.println("Do you want to continue?(y/n) : "); ch=br.readLine().charAt(0); if(ch=='n' || ch=='N') break; System.out.println("Enter the value for the node : "); g=Integer.parseInt(br.readLine()); ...

Bubble Sort using Command Line Arguments in JAVA

class BubblePrg { public static void main(String []a) { int ar[],m,i,j,temp,k; m=10; ar=new int[m]; i=0; for(String x:a) { ar[i++]=Integer.parseInt(x); } System.out.println("Before Sorting Array is : "); for(i=0;i<m;i++) System.out.print(ar[i]+","); for(i=0;i<m;i++) { for(j=0;j<m-i-1;j++) { if(ar[j]>ar[j+1]) { temp=ar[j]; ar[j]=ar[j+1]; ar[j+1]=temp; } } System.out.println("\n \nAfter Pass : "+i); for(k=0;k<m;k++) System.out.print(ar[k]+","); } System.out.println("\n \nAfter Sorting Array is : "); for(i=0;i<m;i++) System.out.print(ar[i]+","); } } I/O :  after compiling the java source code run it as follows : $> java BubblePrg 12 21 23 34 4 22 65 56 76

JAVA program to add two distance

class Distance { int ft,inch; Distance(int f,int i) { ft=f; inch=i; } Distance addDist(Distance ob,Distance ob1) { int f=0,i=0; i=inch+ob.inch+ob1.inch; if(i>=12) { f=i/12; i=i%12; } f=ft+ob.ft+f+ob1.ft; Distance ob2=new Distance(f,i); return ob2; } void dispDist() { System.out.println(ft+"ft "+inch+"inch"); } } class AddDistance { public static void main(String []ar) { Distance ob=new Distance(12,22); Distance ob1=new Distance(13,12); Distance ob2=new Distance(11,11); Distance ob3; ob3=ob.addDist(ob1,ob2); ob.dispDist(); ob1.dispDist(); ob2.dispDist(); ob3.dispDist(); } } I/O: run this program , its clear to understand ....

JAVA program to add , subtract two Rational Number

class Ration { int num,den; Ration(int n,int d) { if(d<n) { num=d; den=n; } else { num=n; den=d; } } Ration() { den=0; num=0; } void disp() { System.out.println("Value is : "+num+"/"+den); } void adjust() { int a,b,t; b=num; a=den; while(a%b!=0) { t=a%b; a=b; b=t; } num=num/b; den=den/b; } Ration subRation(Ration ob) { Ration ob1=new Ration(); int t; t=den*ob.den; ob1.num=num*(t/den)-ob.num*(t/ob.den); ob1.den=t; ob1.adjust(); return ob1; } Ration addRation(Ration ob) { Ration ob1=new Ration(); int t; t=den*ob.den; ob1.num=ob.num*(t/ob.den)+num*(t/den); ob1.den=t; ob1.adjust(); return ob1; } } class Rational { public static void main(String p[]) { Ration ob=new Ration(4,6); Ration ob1=new Ration(2,4); Ration ob2; ob.adjust(); ob1.adjust(); ob.disp(); ob1.disp(); ob2=ob.addRation(ob1); ob2.disp(); ob2=ob.subRation(ob1); ob2.disp(); } }

JAVA Program to add, multiply two Matrix object

class Matrix { int m,n; int ar[][]; Matrix(int a,int b) { int i; m=a; n=b; ar=new int[a][]; for(i=0;i<m;i++) ar[i]=new int[b]; } void insertData() { int i,j,k=1; for(i=0;i<m;i++) { k=1; for(j=0;j<n;j++) ar[i][j]=k++; } } Matrix addMat(Matrix ob) { int i,j; Matrix ob1=new Matrix(m,n); if(m==ob.m && n==ob.n) { for(i=0;i<m;i++) for(j=0;j<n;j++) ob1.ar[i][j]=ar[i][j]+ob.ar[i][j]; } else System.out.println("Condition not matched "); return ob1; } Matrix multiMat(Matrix ob) { int i,j,k; Matrix ob1=new Matrix(m,n); if(n==ob.m) { for(i=0;i<m;i++) { for(j=0;j<ob.n;j++) { ob1.ar[i][j]=0; for(k=0;k<n;k++) ob1.ar[i][j]+=ar[i][k]*ob.ar[k][j]; } } } else System.out.println("Condition not matched "); return ob1; } void disp() { int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) System.out.print(ar[i][j]+" "); System.out.println(); } } } class AddMatrix {...

Circular Queue using Linked List ....

#include<stdio.h> #include<conio.h> typedef struct circular { int val; struct circular *next; }cir; cir *createnode(cir *,int ); void enqueue(cir **,int); int dequeue(cir **); int isempty(cir *); int peek(cir *); void disp(cir *); void main()  {  cir *h=NULL;  int v,p;  while(1)  {  printf("\n 1.insert \n2.delete \n3.peek \n4.display \n5.exit");  printf("enter the choice");  scanf("%d",&p);  switch(p)  {  case 1:printf("enter the value"); scanf("%d",&v); enqueue(&h,v); break;  case 2:printf("deleted value is:"); scanf("%d",&v); break;  case 3:printf("front value: "); scanf("%d",&v); break;  case 4:printf("display the value"); disp(h); break;  case 5: exit(0);  default:printf("not a proper choice");  }  }  }  cir *createnode(cir *h,int v)  {  cir *ptr,*temp;  temp=(cir*)malloc(sizeof(c...

Polynomial Addition using Linked List

#include<stdio.h> #include<stdlib.h> typedef struct poly { int coff,powr; struct poly *next; }pl; void addnode(pl **, int ,int); void creatpoly(pl **); void addpoly(pl *,pl *,pl **); void display(pl *); void main() { int n; pl *pl1,*pl2,*pl3; pl1=pl2=pl3=NULL; clrscr(); printf("\nEnter 1st Polynomial\n"); creatpoly(&pl1); display(pl1); printf("\nEnter 2nd Polynomial\n"); creatpoly(&pl2); display(pl2); printf("\nSum Poly\n"); addpoly(pl1,pl2,&pl3); display(pl3); getch(); } void creatpoly(pl **ptr) {    int cof,pow,temp=10;    char ch;    while(1)    { printf("\nEnter Coff of X : "); scanf("%d",&cof); printf("\nEnter Powr of X : "); scanf("%d",&pow); if(pow<temp) addnode(ptr,cof,pow); else { printf("\nPower must be lesser than the previous term :"); continue; } temp=pow; printf("\nDo you ...