Posts

Stack Using Linked List in C

#include<stdio.h> #include<conio.h> typedef struct StackLinked { int val; struct StackLinked *next; }sl; sl *push(sl *,int); int pop(sl **); int peek(sl *); int isempty(sl *); void disp(sl *); void main() { sl *top=NULL; int ch,v; while(1) { printf("\n\n1) Push\n\n2) Pop\n\n3) Peek\n\n4) Display\n\n5) Exit"); printf("\n Enter your choice :"); scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter the number you want to push : "); scanf("%d",&v); top=push(top,v); break; case 2:  if(!isempty(top))   {   printf("\n\nYou have popped %d",pop(&top));   }   else   printf("\n\nNothing to pop"); break; case 3: if(!isempty(top))   {   printf("\n\nTop most value is %d",peek(top));   }   else   printf("\nNothing in the stack"); break; case 4: if(!isempty(top))   {   printf("\n\nValues in th...

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(...

Program code for polynomial addition using C

/*code for adding two polynomials. please avoid giving duplicate power term. you can enter term values (coefficient and power) in any order. this code is designed for sorting them in appropriate order */ #include<stdio.h> #include<conio.h> #include<alloc.h> typedef struct Polynomial{ int coeff; int powe; struct Polynomial *next; }pl; pl *create(); pl *polyadd(pl *,pl *); void bubblesort(pl *); void disp(pl *); void main() { pl *h1,*h2,*h3; clrscr(); printf("\n\nEnter value for first polynomial : "); h1=create(); printf("\n\nEnter value for second polynomial : "); h2=create(); bubblesort(h1); bubblesort(h2); printf("\n\nFirst Polynomial is : "); disp(h1); printf("\n\nFirst Polynomial is : "); disp(h2); h3=polyadd(h1,h2); printf("\n\nAdded result of two Polynomial is : "); disp(h3); getch(); } pl *create() { pl *temp,*ptr,*h=NULL; int v,u; char ch; while(1) { printf(...

C program code for implementing matrix using linear linked list

//matrix using linked list #include<stdio.h> #include<conio.h> typedef struct linked { int val; struct linked *next; }lnk; typedef struct linkedaddr { lnk *node; struct linkedaddr *link; }lnkadr; lnk *createcol(); lnkadr *createrow(); void disprow(lnkadr *); void dispcol(lnk *); void main() { lnkadr *h; clrscr(); h=createrow(); printf("Values in the matrix : \n\n\n"); disprow(h); getch(); } lnkadr *createrow() { //lnk *h; lnkadr *head=NULL,*temp,*ptr; char ch; while(1) { temp=(lnkadr *) malloc(sizeof(lnkadr)); temp->node=createcol(); temp->link=NULL; if(head==NULL) { head=temp; } ptr->link=temp; ptr=temp; printf("Do you have more row in the matrix ?(y/n) : "); fflush(stdin); scanf("%c",&ch); if(ch=='n' || ch=='N') return head; } } lnk *createcol() { lnk *h=NULL,*ptr,*temp; int v; while(1) { printf("Enter value for the cell of the matrix (0 to exit...