Posts

Showing posts from September, 2015

Binary Tree Creation and traversal using C coding

Image
#include<stdio.h> #include<conio.h> #include<malloc.h> typedef struct tree { int val; struct tree *lch; struct tree *rch; }tr; typedef struct stack { tr *node; struct stack *next; }st; void push(st **,tr *); tr *pop(st **); int isempty(st *); tr *createroot(); void insert(st *); void inorder(tr *); void main() { st *h=NULL; tr *ptr; clrscr(); ptr=createroot(); push(&h,ptr); insert(h); printf("\n\nInorder traversal form of the tree is : "); inorder(ptr); getch(); } tr *createroot() { int v; tr *ptr; printf("\nEnter the values : "); scanf("%d",&v); ptr=(tr *)malloc(sizeof(tr)); ptr->val=v; ptr->lch=ptr->rch=NULL; return(ptr); } void insert(st *h) { tr *ptr,*temp; char ch; int v; while(!isempty(h)) { ptr=pop(&h); printf("\n%d has left child?(y/n) : ",ptr->val); fflush(stdin); scanf("%c",&ch); if(ch=='y' || ch=='Y

Program Code for Circle drawing

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<dos.h> void disp(float,int,int); void disp1(float,int,int); void main() { int gd=DETECT,gm,h,k,y,fl=1,m; float r; initgraph(&gd,&gm,"C:\\TC\\BGI"); printf("Enter the radius " ); scanf("%f",&r); printf("Enter the center (h,k) :"); scanf("%d %d",&h,&k); m=r; while(1) {  if(kbhit())  break; if(r<=0) fl=0; if(r>=m) fl=1; if(fl==1) { r--; disp(r,h,k); } else { r++; disp1(r,h,k); } sleep(1); } } void disp(float r,int h,int k) { int x,xe,y; x=0; xe=r/1.414; while(x<xe) { y=sqrt(pow(r,2)-pow(x,2)); putpixel(x+h,y+k,1); putpixel(-x+h,y+k,2); putpixel(x+h,-y+k,3); putpixel(-x+h,-y+k,4); putpixel(y+h,x+k,1); putpixel(-y+h,x+k,2); putpixel(y+h,-x+k,3); putpixel(-y+h,-x+k,4); x++; } } void disp1(float r,int h,int k) { int x,xe,y; x=0; xe=r/1

Code for Linked List using Java

import java.io.*; class Linked { int a; Linked next; Linked(int g) { a=g; next=null; } void addNode(int g) { Linked temp,node; temp=this; while(temp.next!=null) { temp=temp.next; } node=new Linked(g); temp.next=node; } void displ() { Linked temp; temp=this; while(temp!=null) { System.out.println(temp.a); temp=temp.next; } } } class LinkedPrg { public static void main(String []at) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int p; char ch; System.out.println("Enter values(less than 0 to exit) : "); p=Integer.parseInt(br.readLine()); Linked ob=new Linked(p); for(;;) { System.out.println("Enter values (less than 0 to exit) :  "); p=Integer.parseInt(br.readLine()); if(p<0) break; ob.addNode(p); } System.out.println("Values in the linked list are"); ob.displ(); } }

code for impementing sorting using Selection Sort algorithm

//code for selection sort with intermediate PASS result #include<stdio.h> int selectsmallest(int ar[],int i,int m) { int j,loc,min,k; min=ar[i]; loc=i; for(j=i+1;j<m;j++) { if(ar[j]<min) { min=ar[j]; loc=j; } } printf("\nPass %d :",i); for(k=0;k<m;k++) printf("%d",ar[k]); printf("\n\nsmallest element is %d at %d position",ar[loc],loc); printf("\nswap this value with %d position",i); return loc; } void selectionsort(int ar[],int m) { int loc,j,i,k,temp; for(i=0;i<m;i++) { loc=selectsmallest(ar,i,m); //printf("%d",ar[loc]); temp=ar[loc]; ar[loc]=ar[i]; ar[i]=temp; } } void main() { int i,m,ar[10]; printf("\nEnter the length of the array : "); scanf("%d",&m); printf("\nEnter the values : "); for(i=0;i<m;i++) scanf("%d",&ar[i]); selectionsort(ar,m); printf("\nafter sorting : "); for(i=0;i<m;i++) printf(