Showing posts with label display. Show all posts
Showing posts with label display. Show all posts

Saturday, August 9, 2014

Program for various operations of Binary Search Tree

Programs for the various operations of a binary search tree. Really lengthy program, but its not so bad if you know the logic. Comment below if you need help

#include<stdio.h> #include<stdlib.h> #define max(A,B) (((A)>(B))?(A):(B)) struct node { int data; struct node *rc,*lc; }*newn,*ptr=NULL,*pre=NULL,*root=NULL,*succ=NULL; void ins(); void del(int); void inorder(struct node *temp); void preorder(struct node *temp); void postorder(struct node *temp); void display(struct node*); void trav(); int height(struct node*); int depth(struct node*); int level(struct node*); main() { int ch,r,x,h,l,d; do { printf("\nMenu for BST:\n1.Ins\n2.Del\n3.Traverse\n4.Lvl\n5.Hght\n6.Disp\n"); printf("\nChoice?:"); scanf("%d",&ch); switch(ch) { case 1: ins(); break; case 2: printf("Enter element the element you wish to insert:"); scanf("%d",&x); del(x); break; case 3: display(root); break; case 4:l=level(root); printf("\nthe maximum level of the ttree is:%d",l); break; case 5:h=height(root); printf("\nheight of the tree is:%d",h); break; case 6:display(root); break; case 7:break; default: printf("\nWrong choice\n"); break; } }while(ch!=7); } void ins() { int d,flag=0; ptr=root; printf("\nEnter the element:"); scanf("%d",&d); newn=(struct node*)malloc(sizeof(struct node)); newn->data=d; newn->lc=NULL; newn->rc=NULL; while((ptr!=NULL)&&(flag==0)) { if(d<ptr->data) { pre=ptr; ptr=ptr->lc; } else if(d>ptr->data) { pre=ptr; ptr=ptr->rc; } else { flag=1; printf("Element already exists!"); } } if(ptr==NULL) { if(root==NULL) { root=newn; ptr=root; } else if(pre->data<d) { pre->rc=newn; ptr=pre->rc; } else { pre->lc=newn; ptr=pre->lc; } } } void del(int x) { ptr=root; while(ptr!=NULL) { if(x<ptr->data) { pre=ptr; ptr=ptr->lc; } else if(x>ptr->data) { pre=ptr; ptr=ptr->rc; } else if(ptr->data==x) { if(ptr->lc==NULL&&ptr->rc==NULL) { if(ptr==root) { free(root); root=NULL; break; } else if(pre->lc==ptr) pre->lc=NULL; else if(pre->rc==ptr) pre->rc=NULL; free(ptr); break; } else if(ptr->lc!=NULL&&ptr->rc==NULL) { if(pre->lc==ptr) pre->lc=ptr->lc; else if(pre->rc==ptr) pre->rc=ptr->lc; free(ptr); break; } else if(ptr->lc==NULL&&ptr->rc!=NULL) { if(pre->lc==ptr) pre->lc=ptr->rc; else if(pre->rc==ptr) pre->rc=ptr->rc; free(ptr); break; } else if(ptr->lc!=NULL&&ptr->rc!=NULL) { succ=ptr->rc; if(succ->lc==NULL) { if(pre->lc==ptr) { pre->lc=succ; succ->lc=ptr->lc; } else if(pre->rc==ptr) { pre->rc=succ; succ->rc=ptr->rc; } free(ptr); break; } else { while(succ->lc!=NULL) { pre=succ; succ=succ->lc; } } ptr->data=succ->data; pre->lc=succ->rc; free(ptr); break; } } } if(ptr==NULL) { printf("\nElement absent\n"); } } void postorder(struct node *temp) { if(temp!=NULL) { postorder(temp->lc); postorder(temp->rc); printf(" %d",temp->data); } } void preorder(struct node *temp) { if(temp!=NULL) { printf(" %d",temp->data); preorder(temp->lc); preorder(temp->rc); } } void display(struct node *root1) { int i; if(root1==NULL) printf("Empty tree!"); printf("\nEnter the order\n1:inorder\n2:preorder\n3:postorder\n"); scanf("%d",&i); if(i==1) { printf("\nINORDER:\n"); inorder(root1); } else if(i==2) { printf("\nPREORDER:\n"); preorder(root1); } else { printf("\nPOSTORDER:\n"); postorder(root1); } } void inorder(struct node *temp) { if(temp!=NULL) { inorder(temp->lc); printf(" %d",temp->data); inorder(temp->rc); } } int height(struct node *root) { if(root==NULL) return 0; else return(max(height(root->lc),height(root->rc))+1); } int level(struct node *root) { if(root==NULL) return 0; else return max(height(root->lc),height(root->rc));

Pascal's Triangle Program

This is Pascal's triangle program. Was probably one of the hardest programs I had during my second year. But looking back, its not as hard as it seems. For those who have trouble with this program, I suggest you to write it down, step by step with pencil and paper to see how the program works. If you need help, feel free to ask.

#include<stdio.h>
main()
{
int d,i,j,n,a[50],k,b[50];
printf("enter limit");
scanf("%d",&n);
d=n;
for(i=0;i<n;i++)
{
a[0]=1;
a[i]=1;
for(j=1;j<i;j++)
{
a[j]=b[j-1]+b[j];
}
for(k=0;k<(d-1);k++)
{
printf(" ");
}
d--;
for(k=0;k<=i;k++)
{
printf("%d",a[k]);
printf(" ");
}
for(k=0;k<=i;k++)
printf("\n");
for(k=0;k<=i;k++)
b[k]=a[k];
}
}

Program to count number of occurrences of a word in a sentence.

Simple C program to count the number of occurrences of a word in a sentence. If you need help, feel free to ask.


#include<stdio.h>
#include<string.h>
main()
{
  int len,c=0,i=0,j=0,ans=0;
  char str1[50],str2[20],new[20];
  printf("\n enter the text: ");
  gets(str1);
  printf("\n enter the word: ");
  gets(str2);
  len=strlen(str1);
  for(i=0;i<=len;i++)
   {
     if(str1[i]!=' ')
      {
       new[j]=str1[i];
       j++;
      }
     if(str1[i]==' '||str1[i]=='\0')
       {
           new[j]='\0';
           j=0;
           ans=strcmp(new,str2);
           if(ans==0)
             c++;
        }
    }
   printf("\n the number of occurence is %d\n",c);
}

Program to check for a leap year.

Simple C program to check whether a year is leap year or not. If you have any doubts, please let me know.

#include<stdio.h> void main() { int num; printf ("enter number"); scanf ("%d",&num); if(num%4==0&&num%100!=0)||(num%400==0) { printf ("leap year"); } else { printf ("no"); } }

Program to print out largest of 3 numbers

Program to print out the largest of 3 numbers. If you have any doubts, please let me know.


#include<stdio.h> void main() { int large,x,y,z; printf("enter the 3 numbers"); scanf("%d",&x); scanf("%d",&y); scanf("%d",&z); large=(x>y?(x>z?x:z):(y>z?y:z)); printf("%d",large); }

Program for counting years, weeks and days.

Simple C program for counting the number of years, weeks and days. If you have any doubts, let me know


#include<stdio.h> main() { int c,year,x,week,day; printf("enter no of days"); scanf("%d",&c); year=c/365; x=c%365; week=x/7; day=x%7; printf("YEAR %d",year); printf("WEEK %d",week); printf("DAY %d",day); }

Program to remove the vowels from a string.

Simple C program to remove the vowels from a string. If you have any doubts, please let me know.

#include<stdio.h> #include<string.h> main() { char s[200]; int i=0,j=0,n; printf("\n\n Enter the sentence : "); gets(s); n=strlen(s); while(i<n) { if((s[i]=='a')||(s[i]=='e')||(s[i]=='i')||(s[i]=='o')||(s[i]=='u')||(s[i]=='A')||(s[i]=='E')||(s[i]=='I')||(s[i]=='O')||(s[i]=='U')||(s[i]==' ')) { for(j=i;j<n;j++) { s[j]=s[j+1]; } n--; } else i++; } printf("\n\n New sentence : "); for(i=0;i<n;i++) printf("%c",s[i]); printf("\n\n"); }

Program of the various operations on a dequeue

This is a simple C program of the various operation on a dequeue.

#include<stdio.h> void input_re(); void output_re(); void insert_rear(); void insert_front(); void delet_front(); void delet_rear(); void display(); int dq[50],n,ch,rear=-1,front=-1,d,x,i; main() { int ch,c; printf("\nEnter the size of deque:"); scanf("%d",&n); do{ printf(" MENU\n1.input restricted deque\n2.output restricted deque"); printf("\n enter the choice"); scanf("%d",&ch); switch(ch) { case 1: input_re(); break; case 2: output_re(); break; default: printf("\n Wrong option"); } printf("\n do you want to continue::"); scanf("%d",&c); }while(c==1); } void input_re() { int c; do { int ch; printf("MENU\n1.insert rear\n2.delete front\n3.delete rear\n4.display"); printf("\n enter the choice"); scanf("%d",&ch); switch(ch) { case 1: insert_rear(); break; case 2: delet_front(); break; case 3: delet_rear(); break; case 4: display(); break; default: printf("\n Wrong option"); } printf("\n do you want to continue the input restricted function :"); scanf("%d",&c); }while(c==1); } void output_re() { int ch,c; do { printf("menu\n1.insert rear\n2.insert front\n3.delet from front\n4.display"); printf(" enter the choice"); scanf("%d",&ch); switch(ch) { case 1: insert_rear(); break; case 2: insert_front(); break; case 3: delet_front(); break; case 4: display(); break; default: printf(" invalid option"); } printf("\n do you want to continue the output restricted function:"); scanf("%d",&c); }while(c==1); } void insert_rear() { printf("\n enter the element to be inserted"); scanf("%d",&x); if(((front==0)&&(rear==n-1))||(front==rear+1)) printf("\n deque overflow"); else if((front==-1)&&(rear==-1)) {front=0; rear=0; dq[rear]=x; } else rear=(rear+1)%n; dq[rear]=x; } void insert_front() { printf("\n enter the element to insert:"); scanf("%d",&x); if(((front==0)&&(rear==n-1))||(front==rear+1)) printf("\n deque is full"); else if((front==-1)&&(rear==-1)) front=rear=0; else { if(front==0) front=n-1; else front=front-1; dq[front]=x; }} void delet_front() { if((front==-1)&&(rear==-1)) printf("\n deque is empty"); else { if(front==rear) { front=-1; rear=-1; } else front++; } } void delet_rear() { if((front==-1)&&(rear==-1)) printf("\n deque is empty"); else { if(front==rear) { front=-1; rear=-1; } else { if(rear==0) rear=n-1; else rear=rear-1; } } } void display() { if((front==-1)&&(rear==-1)) printf("\n deque is empty"); else { if(front>rear) { for(i=front;i<n;i++) { d=dq[i]; printf("%d\t",d); } for(i=0;i<=rear;i++) { d=dq[i]; printf("%d\t",d); } } else { for(i=front;i<=rear;i++) { d=dq[i]; printf("%d\t",d); } } } }

Insertion and deletion of number into/from a queue

Simple C program with the deletion and insertion of elements into a queue. If you have any questions, please let me know.

#include<stdio.h>
void eq();
void dq();
void display();
int front=(-1),rear=(-1),q[50],n,i,d,x,a;
main()
{
int ch,c;
printf("enter the number of elements");
scanf("%d",&n);
do
{
printf("MENU \n1.enter \n2.delete \n3.display");
printf("enter choice");
scanf("%d",&ch);
switch(ch)
{
case 1: eq();
break;

case 2: dq();
break;

case 3: display();
break;

default: printf("worong chouice");
break;
}
printf("do you want to continue? \n1.yes \n2.no \n");
scanf("%d",&c);
}
while(c==1);
}
void eq()
{
printf("enter the number to be entered");
scanf("%d",&x);
if(rear==(n-1))
printf("full \n");
else if((front==(-1))&&(rear==(-1)))
{
front=0;
rear=rear+1;
q[rear]=x;
}
else
{
rear=rear+1;
q[rear]=x;
}
}
void dq()
{
if(front==(-1))
printf(" underflow ");
if((rear==(-1))&&(front==(-1)))
printf("its empty \n");
else if(front==rear)
{
d=q[front];
front=rear=(-1);
}
else
{
d=q[front];
front=front+1;
}
}
void display()
{
for(i=front;i<=rear;i++)
printf("%d ",q[i]);
printf(" \n");
}


SImple Matrix addition

Simple C program for the addition of 2 matrices. If you have any doubts, please let me know.

#include<stdio.h> #define size 30 int sp1[size][3],sp2[size][3],sp3[size][3],s1=0,s2=0,s3=0,r,c; int main() { void add(); int a[size][size],i,j; printf("\n\n\t\tEnter number of rows and columns :"); scanf("%d%d",&r,&c); printf("\n\n\t\tEnter matrix 1 : \n"); for(i=0;i<r;i++) for(j=0;j<c;j++) scanf("%d",&a[i][j]); for(i=0;i<r;i++) for(j=0;j<c;j++) { if(a[i][j]!=0) { s1++; sp1[s1][0]=i; sp1[s1][1]=j; sp1[s1][2]=a[i][j]; } } printf("\n\n\t\tEnter matrix 2 : \n"); for(i=0;i<r;i++) for(j=0;j<c;j++) scanf("%d",&a[i][j]); for(i=0;i<r;i++) for(j=0;j<c;j++) { if(a[i][j]!=0) { s2++; sp2[s2][0]=i; sp2[s2][1]=j; sp2[s2][2]=a[i][j]; } } sp1[0][0]=r; sp1[0][1]=c; sp1[0][2]=s1; sp2[0][0]=r; sp2[0][1]=c; sp2[0][2]=s2; printf("\n\n\t\t The matrices are\n\t\t\tA : \n"); for(i=0;i<=s1;i++) { printf("\n"); for(j=0;j<3;j++) printf("\t %d",sp1[i][j]); } printf("\n\t\t\tB : \n"); for(i=0;i<=s2;i++) { printf("\n"); for(j=0;j<3;j++) printf("\t %d",sp2[i][j]); } printf("\n\n"); add(); printf("\n\n"); } void add() { int i=1,j=1; while((i!=s1+1)&&(j!=s2+1)) { if(sp1[i][0]==sp2[j][0]) { if(sp1[i][1]==sp2[j][1]) { s3++; sp3[s3][0]=sp1[i][0]; sp3[s3][1]=sp1[i][1]; sp3[s3][2]=sp1[i][2]+sp2[j][2]; i++; j++; } else if(sp1[i][1]<=sp2[j][1]) { s3++; sp3[s3][0]=sp1[i][0]; sp3[s3][1]=sp1[i][1]; sp3[s3][2]=sp1[i][2]; j++; } else { s3++; sp3[s3][0]=sp2[j][0]; sp3[s3][1]=sp2[j][1]; sp3[s3][2]=sp2[j][2]; j++; } } else if(sp1[i][0]<sp2[j][0]) { s3++; sp3[s3][0]=sp1[i][0]; sp3[s3][1]=sp1[i][1]; sp3[s3][2]=sp1[i][2]; i++; } else { s3++; sp3[s3][0]=sp2[j][0]; sp3[s3][1]=sp2[j][1]; sp3[s3][2]=sp2[j][2]; j++; } } if(i!=s1+1) { while(i!=s1+1) { s3++; sp3[s3][0]=sp1[i][0]; sp3[s3][1]=sp1[i][1]; sp3[s3][2]=sp1[i][2]; i++; } } if(j!=s2+1) { while(j!=s2+1) { s3++; sp3[s3][0]=sp2[j][0]; sp3[s3][1]=sp2[j][1]; sp3[s3][2]=sp2[j][2]; j++; } } sp3[0][0]=r; sp3[0][1]=c; sp3[0][2]=s3; printf("\nThe Sum is :\n"); for(i=0;i<=s3;i++) { printf("\n"); for(j=0;j<3;j++) printf("\t%d",sp3[i][j]); } }

Insertion, deletion and reversal of a link list.

Simple C program for the insertion, deletion and reversal of a link list. If you have any doubts, please let me know.

#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }*start=NULL,*newn,*temp,*prev,*ptr,*rev; int num,pos,ele; void insert_beg(); void insert_end(); void insert_aftr(); void insert_pos(); void delete_beg(); void delete_end(); void delete_ele(); void display(); void reverse(); main() { int ch,c,ch1,c1,ch2,c2; do { printf("\n MENU\n1.Insert\n2.Delete\n3.Reverse\n4.Display"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1:do { printf("\nINSERT\n1.At beginning\n2.At End\n3.After a node\n4.At a position\n5.Display"); printf("\nEnter your choice:"); scanf("%d",&ch1); switch(ch1) { case 1:insert_beg(); break; case 2:insert_end(); break; case 3:insert_aftr(); break; case 4:insert_pos(); break; case 5:display(); break; default:printf("\nWrong Choice"); break; } printf("\nDo you want to continue-Insert Menu?(1.Yes;2.No):"); scanf("%d",&c1); }while(c1!=2); break; case 2:do { printf("\nDELETE\n1.At Front\n2.At End\n3.A specific node\n4.Display"); printf("\nEnter your choice:"); scanf("%d",&ch2); switch(ch2) { case 1:delete_beg(); break; case 2:delete_end(); break; case 3:delete_ele(); break; case 4:display(); break; default:printf("\nWrong Choice"); break; } printf("\nDo you want to continue-Delete Menu?(1.Yes;2.No):"); scanf("%d",&c2); }while(c2!=2); break; case 3:reverse(); break; case 4:display(); break; default:printf("Wrong Choice"); break; } printf("\nDo you want to continue-Main Menu?(1.Yes;2.No):"); scanf("%d",&c); }while(c!=2); } void display() { if(start==NULL) printf("\nLink List is empty\n"); else { temp=start; while(temp!=NULL) { printf(" %d",temp->data); temp=temp->next; } } } void reverse() { if(start==NULL) printf("\nLink List is empty\n"); else { temp=start; prev=NULL; while(temp!=NULL) { rev=prev; prev=temp; temp=temp->next; prev->next=rev; } start=prev; } } void insert_beg() { printf("\nEnter the element to be inserted:"); scanf("%d",&num); newn=(struct node*)malloc(sizeof(struct node)); newn->data=num; if(start==NULL) newn->next=NULL; else newn->next=start; start=newn; } void insert_end() { printf("\nEnter the element to be inserted:"); scanf("%d",&num); newn=(struct node*)malloc(sizeof(struct node)); newn->data=num; if(start==NULL) { newn->next=NULL; start=newn; } else { temp=start; while(temp->next!=NULL) { temp=temp->next; } temp->next=newn; newn->next=NULL; } } void insert_aftr() { int flag=0; if(start==NULL) printf("\nLink List is empty\n"); else { printf("\nEnter the element after which insertion must be done:"); scanf("%d",&ele); printf("\nEnter the element to be inserted:"); scanf("%d",&num); newn=(struct node*)malloc(sizeof(struct node)); newn->data=num; temp=start; while(temp!=NULL) { if(temp->data==ele) { newn->next=temp->next; temp->next=newn; flag=1; break; } temp=temp->next; } if(flag==0) printf("\nElement not found\n"); } } void insert_pos() { int k=1,flag=0; printf("\nEnter the position for insertion:"); scanf("%d",&pos); if(pos==1) insert_beg(); else { printf("\nEnter the element to be inserted:"); scanf("%d",&num); newn=(struct node*)malloc(sizeof(struct node)); newn->data=num; temp=start->next; prev=start; while(temp!=NULL) { k++; if(k==pos) { newn->next=temp; prev->next=newn; flag=1; break; } prev=temp; temp=temp->next; } if(flag==0) printf("\nPosition not found\n"); } } void delete_beg() { if(start==NULL) printf("\nLink List is empty\n"); else { temp=start; start=start->next; free(temp); } } void delete_end() { if(start==NULL) printf("\nLink List is empty\n"); else if(start->next==NULL) delete_beg(); else { temp=start; while(temp->next!=NULL) { prev=temp; temp=temp->next; } prev->next=NULL; free(temp); } } void delete_ele() { int flag=0; if(start==NULL) printf("\nLink List is empty\n"); else { printf("\nEnter the element to be deleted:"); scanf("%d",&num); temp=start; while(temp!=NULL) { if(start->data==num) { delete_beg(); flag=1; temp=start; } else if(temp->data==num) { ptr=temp; prev->next=temp->next; temp=temp->next; free(ptr); flag=1; } prev=temp; temp=temp->next; } if(flag==0) printf("\nElement not found\n"); } }

Insertion, Deletion, Peep, Display, Isempty of a Stack

This is a program for the operations with a stack. If you have any doubts, please let me know.


#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }*top=NULL,*newn,*temp; int num; void ins(); void del(); void peep(); void display(); void empty(); main() { int ch,c; do { printf("operations with a stack! "); printf("\nTHE MAIN MENU\n1.ins\n2.Del\n3.Peep\n4.Dip\n5.Empty"); printf("\n choice?:"); scanf("%d",&ch); switch(ch) { case 1:ins(); break; case 2:del(); break; case 3:peep(); break; case 4:display(); break; case 5:empty(); break; default:printf("\nWrong Choice"); break; } printf("\ncontinue?(1.Yes;2.No):"); scanf("%d",&c); }while(c!=2); } void ins() { printf("\nspecify the element you wish to enter:"); scanf("%d",&num); newn=(struct node *)malloc(sizeof(struct node)); newn->data=num; if(top==NULL) newn->next=NULL; else newn->next=top; top=newn; } void del() { if(top==NULL) printf("\nUnderflow"); else { temp=top; top=top->next; free(temp); } } void peep() { if(top==NULL) printf("\nUnderflow"); else printf("\n %d",top->data); } void display() { if(top==NULL) printf("\nUnderflow"); else { temp=top; while(temp!=NULL) { printf(" %d",temp->data); temp=temp->next; } } } void empty() { if(top==NULL) printf("\nStack is empty"); else printf("\nStack is not empty"); }