Posts

Showing posts from 2017

Binary Search Tree Mirror Image creation......

/*Binary Search Tree with Mirror Image Conversion of the original Tree ... */ #include<stdio.h> #include<stdlib.h> typedef struct BinarySearchTree { struct BinarySearchTree *left; struct BinarySearchTree *right; int val; }bst; typedef struct Queue { bst *val; struct Queue *next; }qu; void enqueu(qu **,bst *); bst *dequeu(qu **); int isempty(qu *); bst * create(bst *,int); void inorder(bst *); void preorder(bst *); void postorder(bst *); void levelorder(bst *); void mirimg(bst *); int main() { bst *root=NULL; int g,ch; while(1) { printf("\n\n 1) Insert\n 2) Preorder traversal\n 3) Inorder traversal\n 4) Postorder traversal\n 5) Level Order Traversal\n 6) Construct Mirro Image of the tree\n 7) Exit"); printf("\n\n Enter your choice : "); scanf("%d",&ch); switch(ch) { case 1: printf("\nInsert value for the tree: "); scanf("%d",&g); root=create(root,g); break; c

Binary Search Tree With Level Order Traversal

Image
/*Binary Search Tree with level order traversal ... Queue for level order traversal is implemented using linked list*/ #include<stdio.h> #include<stdlib.h> typedef struct BinarySearchTree { struct BinarySearchTree *left; struct BinarySearchTree *right; int val; }bst; typedef struct Queue { bst *val; struct Queue *next; }qu; void enqueu(qu **,bst *); bst *dequeu(qu **); int isempty(qu *); bst * create(bst *,int); void inorder(bst *); void preorder(bst *); void postorder(bst *); void levelorder(bst *); int main() { bst *root=NULL; int g,ch; while(1) { printf("\n 1) Insert\n 2) Preorder traversal\n 3) Inorder traversal\n 4) Postorder traversal\n 5) Level Order Traversal\n 6) Exit"); printf("\n Enter your choice : "); scanf("%d",&ch); switch(ch) { case 1: printf("\nInsert value for the tree: "); scanf("%d",&g); root=create(root,g); break; case 2: printf("\nValues in

Different functions of Linked List using menu driven approach

/*insertend(),inserbef(),insertatpos() functions are used to create a linked using its position value,del() function will delete the element you have specified,disp() function is used to display the elements in the linked list*/ #include<stdio.h> #include<stdlib.h> typedef struct Linked { int val; struct Linked *next; }lnk; lnk* insertbeg(lnk *,int); lnk* insertend(lnk *,int); lnk *insertatpos(lnk *,int,int); lnk *del(lnk *,int); void disp(lnk *); int main() { lnk *head=NULL; int ch,v,p; while(1) { printf("\n1)Insert End\n2) Insert begining\n3)Insert at Position\n4)Delete\n5)Display\n6)Exit"); printf("\n Enter your choice : "); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the value to be inserted : ");  scanf("%d",&v);  head=insertend(head,v);  break; case 2: printf("\nEnter the value to be inserted : ");  scanf("%d",&v);  head=insertbeg(head,v);  b

Linked list created and there after elements are displayed

Image
#include<stdio.h> #include<stdlib.h> typedef struct Linked { int val; struct Linked *next; }lnk; lnk* createlink(); void disp(); int main() { lnk *head; head=createlink(); printf("Values in the linked list :: "); disp(head); } lnk *createlink() { lnk *h=NULL,*tmp,*ptr; char ch; int v; while(1) { printf("\nEnter the value for the node (enter -99 to exit) : "); scanf("%d",&v); if(v==-99) return h; tmp=(lnk *)malloc(sizeof(lnk)); tmp->val=v; tmp->next=NULL; if(h==NULL) h=tmp; else ptr->next=tmp; ptr=tmp; } } void disp(lnk *h) { while(h!=NULL) { printf("%d,",h->val); h=h->next; } printf("\b"); }

Circular Queue using Structure

Image
#include<stdio.h> #include<stdlib.h> #define max 5 typedef struct Queue { int val[max]; int rear,front; }qu; void enque(qu *,int); int deque(qu*); int peek(qu); int isempty(qu); int isfull(qu); void disp(qu); void main() { qu q; int g,v; q.rear=q.front=-1; while(1) { printf("\n1) Insert\n2) Delete\n3) Peek\n4) Display\n5) Exit"); printf("\n Enter your choice :"); scanf("%d",&g); switch(g) { case 1: printf("\n Enter the value"); scanf("%d",&v); if(!isfull(q)) enque(&q,v); else printf("\nNo more space"); break; case 2: if(!isempty(q)) printf("\nDeleted element is %d",deque(&q)); else printf("\nNothing to delete"); break; case 3: if(!isempty(q)) printf("\n value at front %d",peek(q)); else printf("\n Nothing in the front"); break; case 4: if(!isempty(q)) { printf("\nValues int queue

Linear Queue using Structure

Image
#include<stdio.h> #include<stdlib.h> #define max 50 typedef struct Queue { int val[max]; int front,rear; }qu; void enqueu(qu *,int); int dequeu(qu *); int peek(qu); int isempty(qu); int isfull(qu); void disp(qu); int main() { int ch,v; qu q; q.front=q.rear=-1; while(1) { printf("\n1) Insert \n2) Delete \n3)Front value\n4)Display\n5) Exit"); printf("\n Enter the choice : "); scanf("%d",&ch); switch(ch) { case 1: if(!isfull(q)) { printf("\n Enter the value : "); scanf("%d",&v); enqueu(&q,v); } else printf("\nNo more space"); break; case 2: if(!isempty(q)) { v=dequeu(&q); printf("\nThe deleted value from the front is %d",v); } else printf("\nNothing to delete"); break; case 3: if(!isempty(q)) { printf("\nThe Value at the front is %d",peek(q)); } else printf("\nNothing in the queue"); br

Infix to Postfix conversion along with Postfix evaluation

#include<stdio.h> #include<conio.h> #define max 50 typedef struct stack { char ar[max]; int top; }st; typedef struct stack1 { float res[max]; int top; }st1; void push(st *,char); void push1(st1 *,float); char pop(st *); float pop1(st1 *); char peek(st); int isfull(st); int isempty(st); float pfixeval(char []); void intopost(char [],char []); int getpriority(char); int main() { char expr[max],res[max]; float p; printf("\nEnter the expression (in digits only) to be evaluated in Infix form and rest is our responsibility : "); gets(expr); intopost(expr,res); printf("\nThe postfix expression is : "); puts(res); p=pfixeval(res); printf("\nEvaluated result is : %f",p); getch(); } void intopost(char expr[],char res[]) { st s; char ch; int i,j; s.top=-1; for(i=0,j=0;expr[i]!='\0';i++) { if(expr[i]==' ') continue; else if(expr[i]=='(' || expr[i]=='{' || expr[i]=='[&#

Infix to Postfix conversion using stack

#include<stdio.h> #include<conio.h> #define max 50 typedef struct stack { char ar[max]; int top; }st; void push(st *,char); char pop(st *); char peek(st); int isfull(st); int isempty(st); void intopost(char [],char []); int getpriority(char); int main() { char expr[max],res[max]; printf("enter the expression : "); gets(expr); intopost(expr,res); printf("The postfix expression is : "); puts(res); getch(); } void intopost(char expr[],char res[]) { st s; char ch; int i,j; s.top=-1; for(i=0,j=0;expr[i]!='\0';i++) { if(expr[i]==' ') continue; else if(expr[i]=='(' || expr[i]=='{' || expr[i]=='[') push(&s,expr[i]); else if((expr[i]>=48 && expr[i]<=57) || (expr[i]>=65 && expr[i]<=90) || (expr[i]>=97 && expr[i]<=122)) res[j++]=expr[i]; else if(expr[i]=='+' || expr[i]=='-' || expr[i]=='*' || expr[i]=='/'

pattern with sound

Image
//print pattern with background sound..... keep your mother board speaker connected...... #include<stdio.h> #include<conio.h> #include<dos.h> void main() { int i,j,m,k=1,t=1;//,g; clrscr(); printf("enter the height"); scanf("%d",&m); printf("\n\n"); for(i=0;i<m;i++) { for(j=0;j<m;j++) { textcolor(t); textbackground(t+1); t++; sound(30*(k++)*2); if((i==0 || i==m-1) || (j==0 || j==m-1)) cprintf(" *"); else if(i==j) cprintf(" $"); else cprintf(" #"); delay(100); } printf("\n"); } nosound(); getch(); }

pattern

Image
/*pattern G F E D C B A  F E D C B A   E D C B A    D C B A     C B A      B A       A       */ #include<stdio.h> #include<conio.h> void main() { int p=65,n,m,i,j,t; clrscr(); printf("\nEnter the height of the pattern : "); scanf("%d",&n); printf("\n\n"); for(i=n-1;i>=0;i--) { p=65+i; for(j=0;j<n-i;j++) printf(" "); for(j=0;j<=i;j++) printf("%c ",p--); printf("\n"); } getch(); } I/O :

simple example of calloc

//example of using calloc #include<stdio.h> #include<conio.h> void main() { int *p; int n,i; printf("\nEnter the number of elements : "); scanf("%d",&n); p=(int *)calloc(n,sizeof(int)); printf("\nEnter the values : "); for(i=0;i<n;i++) scanf("%d",(p+i)); printf("\nValues are :"); for(i=0;i<n;i++) printf("%d,",*(p+i)); getch(); } Input / Output : Enter the number of elements : 6 Enter the values : 12 23 34 45 56 67 Values are :12,23,34,45,56,67,

Program to create , access structure

Image
#include<stdio.h> #include<string.h> typedef struct Student { int roll; char name[50]; int m[3]; int tot; float avg; char grade; }std; void insertdata(std [],int); void dispdata(std [],int); void topper(std [],int); void topper_each_sub(std [],int); void arrang_by_name(std [],int); int main() { std s[50]; int m; printf("Enter the total number of students :"); scanf("%d",&m); printf("\nEnter the records for %d students",m); insertdata(s,m); printf("\nRecords of %d students : ",m); dispdata(s,m); printf("\nTopper in the department : "); topper(s,m); printf("\nTopper in each subjects : "); topper_each_sub(s,m); printf("\nStudent's record in ascending order of name : "); arrang_by_name(s,m); dispdata(s,m); return 0; } void insertdata(std p[],int k) { int i,j; for(i=0;i<k;i++) { printf("\nInsert records for student %d : ",i+1); printf("

Different types of Abbreviation of a name

#include<stdio.h> void abbr1(char []); void abbr2(char []); void abbr3(char []); int main() { char str[50]; printf("Enter the name : "); gets(str); printf("\n Abbreviated for of the name is : "); abbr1(str); printf("\n Abbreviated for of the name is : "); abbr2(str); printf("\n Abbreviated for of the name is : "); abbr3(str); return 0; } void abbr1(char str[]) // input: Rajib Kumar Das ==> output: R.K.D { char abbr[15]; int i,j; abbr[0]=str[0]; for(i=1,j=1;str[i]!='\0';i++) { if(str[i]==' ' && str[i+1]!=' ') { abbr[j++]='.'; abbr[j++]=str[i+1]; } } abbr[j]='\0'; puts(abbr); } void abbr2(char str[]) // input: Rajib Kumar Das ==> output: R.K.Das { char abbr[15]; int i,j,t; abbr[0]=str[0]; for(i=1,j=1;str[i]!='\0';i++) { if(str[i]==' ' && str[i+1]!=' ') { t=i+1; abb

Replace a particular word of a sentence with another word

Image
#include<stdio.h> #include<string.h> //replace a particular word in a sentence with another word int main() { char str[100],strn[100],word[15],wrd[15],wrd1[15]; int j,i,k; printf("Enter the sentence : "); gets(str); printf("Enter the word to be replaced : "); gets(wrd); printf("Enter the word to be replaced with : "); gets(wrd1); k=0; for(i=0;str[i];i++) { j=0; while(str[i]!=' ' && str[i]!='\0') { word[j++]=str[i++]; } word[j]='\0'; if(strcmp(word,wrd)==0) { j=0; while(wrd1[j]!='\0') strn[k++]=wrd1[j++]; strn[k++]=' '; } else { j=0; while(word[j]!='\0') strn[k++]=word[j++]; strn[k++]=' '; } } strn[--k]='\0'; printf("The new senetence is %s\n\n",strn); } I/O