Posts

Showing posts from August, 2015

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

Java Program code for implementing array of objects

import java.util.*; class Sports { int cr; boolean ingame,outgame; Sports(boolean ig,boolean og) { cr=0; ingame=ig; outgame=og; } void cre() { if(ingame==true) cr+=5; if(outgame==true) cr+=10; } } class Marksheet extends Sports { int m1,m2,m3,tot,avg; char grade; Marksheet(int a,int b,int c,boolean ig,boolean og) { super(ig,og); m1=a; m2=b; m3=c; } void calculate() { cre(); tot=m1+m2+m3; avg=tot/3; if(avg>=90) { grade='O'; cr+=15; } else if(avg>=80) { grade='E'; cr+=10; } else if(avg>=70) { grade='A'; cr+=5; } else if(avg>=60) grade='B'; else if(avg>=50) grade='C'; else if(avg>=40) grade='D'; else grade='F'; } } class Student extends Marksheet { int roll; String name; String addr; String phno; Student(int r,String nm,String adr,String phn,int a,int b,int c,boolean ig,boolean og) { super(a,b,c,ig,og); roll=r; name=nm; addr=adr; phno=phn; }

C Program code for solving Josephus problem using circular linked list

//Josephus problem using circular linked list #include<stdio.h> #include<conio.h> typedef struct circular { int val; struct circular *next; }cir; cir *create(); void disp(cir *); void insertafter(cir *,int); cir *insertbefore(cir *,int); cir *del(cir *,int); cir *josephus(cir*,int); void main() { int ch,v,p; cir *head; while(1) { printf("\n1) Create \n2) Insert after\n3) Insert before \n4) Delete \n5) Display \n6) Josephus Problem\n7) Exit"); printf("\nEnter your choice : "); scanf("%d",&ch); switch(ch) { case 1: head=create(); break; case 2: printf("\nEnter the node value after which you want to insert the new node : "); scanf("%d",&p); insertafter(head,p); break; case 3: printf("\nEnter the node value before which you want to insert the new node :"); scanf("%d",&p); head=insertbefore(head,p); break; case 4: printf("\nEnter the node

Program code for bubble sort using linked list

//code for bubble sort using linked list #include<stdio.h> #include<conio.h> #include<alloc.h> typedef struct linked { int val; struct linked *next; }lnk; lnk *create(); void bubblesort(lnk *); void disp(lnk *); void main() { lnk *h; printf("\n\nEnter the values (0 to exit) : "); h=create(); printf("\n\nValues in the linked list before sorting are : "); disp(h); bubblesort(h); printf("\n\nValues in the linked list after sorting are : "); disp(h); getch(); } lnk *create() { lnk *temp,*ptr,*h=NULL; int v; while(1) { scanf("%d",&v); if(v==0) return h; temp=(lnk*)malloc(sizeof(lnk)); temp->val=v; temp->next=NULL; if(h==NULL) h=temp; else ptr->next=temp; ptr=temp; } } void disp(lnk *h) { while(h!=NULL) { printf("%d",h->val); h=h->next; } } void bubblesort(lnk *h) { int v; lnk *ptr,*loc; loc=h; while(h!=NULL) { ptr=loc; whil

Program code for split up linked list into odd and even value

//Split up two linked list into even and odd value #include<stdio.h> #include<conio.h> #include<alloc.h> typedef struct linked { int val; struct linked *next; }lnk; lnk *create(); void splitlink(lnk *,lnk **,lnk **); void disp(lnk *); void main() { lnk *h1=NULL,*h2=NULL,*h; printf("\n\nEnter the values (0 to exit) : "); h=create(); printf("\n\nValues in the linked list are : "); disp(h); splitlink(h,&h1,&h2); printf("\n\nValues in the first linked list are : "); disp(h1); printf("\n\nValues in the second linked list are : "); disp(h2); getch(); } lnk *create() { lnk *temp,*ptr,*h=NULL; int v; while(1) { scanf("%d",&v); if(v==0) return h; temp=(lnk*)malloc(sizeof(lnk)); temp->val=v; temp->next=NULL; if(h==NULL) h=temp; else ptr->next=temp; ptr=temp; } } void disp(lnk *h) { while(h!=NULL) { printf("%d",h->val); h

Program code to merge two sorted linked list in sorted order

//Merge two already sorted linked list in sorted order #include<stdio.h> #include<conio.h> #include<alloc.h> typedef struct linked { int val; struct linked *next; }lnk; lnk *create(); lnk *merge(lnk *,lnk *); void disp(lnk *); void main() { lnk *h1,*h2,*h; printf("\n\nEnter the values (0 to exit) : "); h1=create(); printf("\n\nEnter the values (0 to exit) : "); h2=create(); printf("\n\nValues in the first linked list are : "); disp(h1); printf("\n\nValues in the second linked list are : "); disp(h2); h=merge(h1,h2); printf("\n\nValues in the merged linked list are : "); disp(h); getch(); } lnk *create() { lnk *temp,*ptr,*h=NULL; int v; while(1) { scanf("%d",&v); if(v==0) return h; temp=(lnk*)malloc(sizeof(lnk)); temp->val=v; temp->next=NULL; if(h==NULL) h=temp; else ptr->next=temp; ptr=temp; } } void disp(lnk *h) { while(

2D array using java

class JavaArr { public static void main(String []ar) { int [][]a=new int [5][5]; int k; for(int i=0;i<5;i++) { k=1; for(int j=0;j<5;j++) { a[i][j]=k++; } } System.out.println("Values in the array are :"); for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { System.out.print("   "+a[i][j]); } System.out.println(); } } } I/O : array is declared and value is passed through loop and values are displayed. type the program and run it , I hope its very simple to understand.

Program code for inserting node at any position in Linked List

//C Program code for inserting new node at  any position you like #include<stdio.h> #include<conio.h> #include<alloc.h> typedef struct linked { int val; struct linked *next; }lnk; lnk *insert(lnk *,int,int); void disp(lnk *); void main() { lnk *h=NULL; int ch,pos,v; while(1) { printf("\n1) Insert at any position \n2) Display \n3) Exit"); printf("\nEnter your choice : "); scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter the value : "); scanf("%d",&v); printf("\n Enter the posiotion of new node : "); scanf("%d",&pos); h=insert(h,v,pos); break; case 2: printf("\n\nValues are :"); disp(h); break; case 3: exit(0); default: printf("\n\nWrong choice"); } } } lnk *insert(lnk *h,int v,int po) { lnk *ptr,*temp,*pre; int i; temp=(lnk*)malloc(sizeof(lnk)); temp->val=v; temp->next=NULL; if(h==NULL) { printf(&qu

Program code to draw a line using DDA in C

#include<stdio.h> #include<conio.h> #include<graphics.h> int round(float x) { int p; p=x; return p; } void main() { int gd=DETECT,gm,x1,y1,dx,dy,x2,y2,length,i,x,y; initgraph(&gd,&gm,"C:\\TC\\BGI"); printf("Enter the starting points (x,y) : "); scanf("%d %d",&x1,&y1); printf("Enter the ending points (x,y) : "); scanf("%d %d",&x2,&y2); if(abs(x2-x1)>=abs(y2-y1)) length=abs(x2-x1); else length=abs(y2-y1); i=1; dx=(x2-x1)/length; dy=(y2-y1)/length; x=x1; y=y1; while(i<=length) { putpixel(round(x),round(y),2); //x1=x1+dx; y=y+dy; i++; } i=1; x=x1; y=y1; while(i<=length) { putpixel(round(x),round(y),2); x=x+dx; //y1=y1+dy; i++; } i=1; x=x1; y=y1; while(i<=length) { putpixel(round(x),round(y),3); x=x+dx; y=y+dy; i++; } getch(); closegraph(); }

Program code for converting Infix expression into Postfix expression

#include<stdio.h> #include<conio.h> #define MAX 100 typedef struct stack {  char data[MAX];  int top; }stack; int getpriority(char); int isempty(stack *); int isfull(stack *); char pop(stack *); void push(stack *,char); char peek(stack *); void intopost(char [],char []); void main() { char str[50],str1[50]; printf("Enter the infix expression: "); gets(str); intopost(str,str1); printf("Post fix expression is : "); puts(str1); getch(); } // Following function is converting infix into postfix notation void intopost(char str[],char str1[]) { stack s; char x; int i,j; s.top=-1; clrscr(); i=j=0;   while(str[i]!='\0')   {     if(isalnum(str[i]))  str1[j++]=str[i];     else        if(str[i] == '(')   push(&s,'(');        else        { if(str[i] == ')')     while((x=pop(&s))!='(')     str1[j++]=x; else { while(getpriority(str[i])<=getpriori

SPECIAL STACK USING C

//This is a special stack in which popped value is replaced with -1 and value insertion is done at the  position where -1 is found first from the bottom. #include<stdio.h> #include<conio.h> #define max 50 typedef struct stack { int ar[max]; int top; }st; void push(st *,int); int pop(st *); int peek(st); int isfull(st); int isempty(st); void disp(st); void main() { int ch,g; st s; s.top=-1; while(1) { printf("\n1)Push\n2)Pop\n3)Peek\n4)Disp\n5)Exit"); printf("\n Enter your choice ; "); scanf("%d",&ch); switch(ch) { case 1: if(!isfull(s)) { printf("Enter the value : "); scanf("%d",&g); push(&s,g); } else printf("No More Space"); break; case 2: if(!isempty(s)) printf("Popped value is %d",pop(&s)); else printf("Nothing to display "); break; case 3: if(!isempty(s)) { printf("Top value is %d",peek(s)); } els