Posts

Showing posts with the label c program

C program to implement Doubly Circular Linked List Data Structure

Simple C program to implement Doubly Circular Linked List Data Structure along with following operations: Insertion at end Insertion at front Insert after a value Insert before a value Delete at end Delete at front Delete after a value Delete before a value Delete element itself Delete the whole list Display linked list Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node{ int data; struct node *next; struct node *prev; }; struct node *head,*tail; void insertend(){ int val; printf("\nEnter the value to be Entered: "); scanf("%d",&val); struct node *head2; head2=head; struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); temp->data=val; temp->next=NULL; temp->prev=NULL; if(head==NULL){ head=temp; temp->next=head; temp->prev=head; } else{ while(head2->next!=head){ head2=head2->next; } temp->prev=head2; temp->next=head; head2->next=temp; tail=temp; head-...

C program to implement Graph data structure with Depth First Search and Breadth First Search

Simple C program to implement Graph data structure with following operations: Depth First Search(DFS) Breadth First Search(BFS) Code: #include<stdio.h> int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20]; int delete(); void add(int item); void bfs(ints,int n); void dfs(ints,int n); void push(int item); int pop(); void main() { intn,i,s,ch,j; char c,dummy; printf("ENTER THE NUMBER VERTICES "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j); scanf("%d",&a[i][j]); } } printf("THE ADJACENCY MATRIX IS\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf(" %d",a[i][j]); } printf("\n"); } do { for(i=1;i<=n;i++) vis[i]=0; printf("\nMENU"); printf("\n1.B.F.S"); printf("\n2.D.F.S"); printf("\nENTER YOUR CHOICE"); scanf("%d",&ch); p...

C program to implement Tree data structure and its operations

Simple C program to implement Tree data structure having following operations: Insert a value Inorder Display Preorder Display Postorder Display Search An Element Delete An Element Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node{ int data; struct node *left; struct node *right; }; struct node *root; struct node *newnode(){ int val; printf("\nEnter the value to be inserted:"); scanf("%d",&val); struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); temp->left=NULL; temp->right=NULL; temp->data=val; return temp; } void insert(struct node *root, struct node *root2) { if (root2->data <= root->data) { if (root->left == NULL) root->left = root2; else insert(root->left, root2); } if (root2->data > root->data) { if (root->right == NULL) root->right = root2; else i...

C program to multiply two matrices and create Single Result Matrix

Simple C program to do multiplication of given matrices Code: #include<conio.h> #include<stdio.h> void main() { clrscr(); int a[3][3],b[3][3],c[3][3],i,j,k; printf("Enter the matrix values of A:"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter the element number A%d%d:",i,j); scanf("%d",&a[i][j]); } } printf("Enter the matrix values of B:"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter the element number B%d%d:",i,j); scanf("%d",&b[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) { c[i][j]=c[i][j]+a[i][k] *b[k][j]; } } } Printf(“Result matrix: “); for(i=0;i<3;i++) { printf("\n"); for(j=0;j<3;j++) { printf("%d",c[i][j]); } } getch(); }

C program to add two matrices into single Matrix

Simple C program to do addition of given matrices Code: #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[4][4],b[4][4],c[4][4]; int i,j,k,m,n,p,q; printf("\nenter no. of rows for matrix A:"); scanf("%d",&m); printf("\nenter no. of coloums for matrix A:"); scanf("%d",&n); printf("\nenter no. of rows for matrix B:"); scanf("%d",&p); printf("\nenter no. of coloums for matrix B:"); scanf("%d",&q); printf("\nenter array A:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } printf("\n"); } printf("\nenter array B:\n"); for(i=0;i<p;i++) { for(j=0;j<q;j++) { scanf("%d",&b[i][j]); } printf("\n"); } if((m==p)&&(n==q)) { printf("\naddition of array is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j]; printf("%d ",c[i][j]); } p...

C program to reverse the elements of an array

Simple C program to reverse elements of an array Code: #include<stdio.h> #include<conio.h> void main() { int n,a[10],i,j,e; clrscr(); printf("enter limit : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter number of a[%d] : ",i); scanf("%d",&a[i]); } for(i=0,j=n-1;i<n/2;i++,j--) { e=a[i]; a[i]=a[j]; a[j]=e; } Printf(“Reversed string: “); for(i=0;i<n;i++) { printf("\n %d ",a[i]); } getch(); }

C program to merge two arrays into single array

Simple C program to merge two arrays into a single array Code: #include<stdio.h> #include<conio.h> int main(){ int a[50],b[50],c[100],i,j=0,n,m; clrscr(); printf("Enter the length of first array: "); scanf("%d",&n); printf("Enter the length of second array: "); scanf("%d",&m); printf("Enter the element of FIRST array: "); for (i=0;i<n;i++){ scanf("%d",&a[i]);} printf("Enter the element of second array: "); for(i=0;i<m;i++){ scanf("%d",&b[i]);} for (i=0;i<(n+m);i++){ if(i>n-1){ c[i]=b[j]; j++; } else{ c[i]=a[i]; } } Printf(“The final Array: ”); for (i=0;i<(m+n);i++){ printf("\n%d",c[i]); } getch(); }

C program to find an element from Array

Simple C program to find an element from Array Code: #include<stdio.h> #include<conio.h> void main() { int a[20],i,x,n; clrscr(); printf(“How many elements?”); scanf(“%d”,&n); printf(“Enter array elements:”); for(i=0;i<n;++i) scanf(“%d”,&a[i]); printf(“nEnter element to search:”); scanf(“%d”,&x); for(i=0;i<n;++i) if(a[i]==x) break; if(i<n) printf(“Element found at index %d”,i); else printf(“Element not found”); getch(); }

C program to find sum of numbers of an array

Simple C program to find sum of numbers of an array Code: #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[50]; int l,i; int sum=0; printf("\n enter length of array:"); scanf("%d",&l); printf("\n enter array:"); for(i=0;i<l;i++) { scanf("%d",&a[i]); sum+=a[i]; } printf("\nsum of array elements is:%d",sum); getch(); }

C program to find maximum and minimum number from an array

Simple C program to find maximum and minimum number from an array Code: #include<stdio.h> #include<conio.h> int main() { int a[50],size,i,big,small; printf("\nEnter the size of the array: "); scanf("%d",&size); printf("\nEnter %d elements in to the array: ", size); for(i=0;i<size;i++) scanf("%d",&a[i]); big=a[0]; for(i=1;i<size;i++){ if(big<a[i]) big=a[i]; } printf("\nLargest element: %d",big); small=a[0]; for(i=1;i<size;i++){ if(small>a[i]) small=a[i]; } printf("Smallest element: %d",small); getch(); }

C program to solve Tower of Hanoi problem for n disks using recursion.

Simple C program to solve Tower of Hanoi problem for n disks using recursion. Code: #include <stdio.h> #include<conio.h> void tower(int n, char from, char to, char through){ if (n==1){ printf("\n Move disk 1 from rod %c to rod %c", from, to); return; } tower(n-1, from, through, to); printf("\n Move disk %d from rod %c to rod %c", n, from, to); tower(n-1, through, to, from); } main(){ int n; clrscr(); printf("Enter the number of disk: "); scanf("%d",&n); tower(n, 'A', 'C', 'B'); getch(); }

C program to evaluate Prefix Expression using Stack data structure

Simple C program to evaluate Prefix Expression using Stack data structure Code: #include<stdio.h> #include<conio.h> #include<string.h> int steck[10]; int top=-1; void push(int value) { steck[top++]=value; } int pop() { return(steck[top--]); } int ope(char c) { if(c=='+'||c=='-'||c=='*'||c=='/') return 1; else return 0; } void main() { char prefix[10]; int len,val,i,opr1,opr2,res; clrscr(); printf("Enter the prefix Expression: "); scanf("%s",prefix); len=strlen(prefix); for(i=len-1;i>=0;i--) { switch(ope(prefix[i])) { case 0: val=prefix[i]; push(val); break; case 1: opr1=pop(); opr2=pop(); switch(prefix[i]) { case '+': res=opr1+opr2; break; case '-': res=opr1-opr2; break; case '*': res=opr1*opr2; break; case '/': res=opr1/opr2; break; } push(res); } } printf("Result is %d",steck[top]); getch(); }

C program to evaluate Postfix Expression using Stack

Simple C program to evaluate Postfix Expression using Stack. Code: #include<stdio.h> #include<conio.h> #include<ctype.h> #include<math.h> int s[50]; int top=-1; int flag=0; int pop() { return(s[top--]); } push(int elem) { if(flag==1){ int num; num=pop(); s[++top]=elem+10*num; } else if(flag==0){ s[++top]=elem; flag=1; } } main(){ char pofx[50],ch; int i=0,op1,op2; printf("Enter the Postfix Expression:"); fgets(pofx,100,stdin); while( (ch=pofx[i++]) != '\n') { if(isdigit(ch)) push(ch-'0'); else if(ch==' ') flag=0; else { flag=0; op2=pop(); op1=pop(); switch(ch) { case '+':push(op1+op2);break; case '-':push(op1-op2);break; case '*':push(op1*op2);break; case '/':push(op1/op2);break; case ‘^’; push(pow(op1,op2));break; default: ...

C program to convert Infix to Prefix using Stack data structure

Simple C program to convert Infix to Prefix using Stack data structure Code: #include<stdio.h> #include<string.h> #include<ctype.h> #include<conio.h> char steck[50]; int top=-1; void push(char ele){ top++; steck[top]=ele; } char pop(){ return (steck[top--]); } int isoperator(char ele){ if (ele=='^' ||ele=='#' || ele=='/' || ele=='+' || ele=='-'){ return 1; } else{ return 0; } } int pre(char ele) { if (ele =='#') { return 4; } else if (ele=='*' || ele=='/'){ return 3; } else if (ele=='+' || ele =='-'){ return 2; } else return 0; } void main(){ char infix[20],postfix[20]; int i,j=0; char c; clrscr(); printf("Enter the String: "); scanf("%s",infix); strrev(infix); printf("%s",infix); // printf("%d",strlen(infix)); for (i=0;i< (strlen(infix)) ;i++){ // c=infix[i]; if(isoperator(infix[i])){ if (top==-1){ push(infix[i]); } else{ while...

C program to convert Infix to Postfix Expression using Stack

Simple C program to convert Infix to Postfix Expression using Stack. Code: #include<stdio.h> #include<conio.h> #include <ctype.h> char s[50]; int top=-1; push(char elem) { s[++top]=elem; } char pop() { return(s[top--]); } int pr(char elem) { switch(elem) { case '#': return 0; case '(': return 1; case '+': case '-': return 2; case '*': case '/': return 3; } } main() { char infx[50],pofx[50],ch,elem; int i=0,k=0; clrscr(); printf("\n\nEnter the infix Expression: "); scanf("%s",infx); push('^'); while( (ch=infx[i++]) != '\0') { if( ch == '(') push(ch); else if(isalnum(ch)) pofx[k++]=ch; else if( ch == ')') { while( s[to...

C program to implement stack data structure

Simple C program to implement stack data structure with following operations: Push Pop Peek Change Delete Code: #include<stdio.h> #include<conio.h>  int stack[100],top=-1,n; void display(){ int i; for(i=0;i<n;i++) printf("\t %d",stack[i]); } void push(){ int element; if(top==n-1) printf("The stack is Full"); else { printf("Enter the Element: "); scanf("%d",&element); top++; stack[top]=element; } } void pop(){ if(top==-1) printf("The stack is Underflow"); else printf("%d is popped",stack[top]); top--; } main(){ int option; clrscr(); printf("\nEnter the number of elements in Stack: "); scanf("%d",&n); do{ printf(...

C program to implement Double Ended Queue Data Structure

Simple C program to implement Double Ended Queue Data Structure Code: #include<stdio.h> #include<conio.h> int f=0,r=0,q[10],n=10; void rinsert(int ele) { if(r==n && r!=0) { printf("Queue overflow"); r=0; f=0; } else { q[r]=ele; r++; printf("%d is inserted at end of queue",ele); } } void finsert(int ele) { if(f==0) { printf("Function not allowed"); } else { f--; q[f]=ele; printf("%d is inserted at front of queue",ele); } } int rdel() { if(r==n) { printf("Function not allowed"); } else if(r==f) { printf("Queue underflow"); } else { r--; printf("%d is deleted",q[r]); return(q[r]); } } int fdel() { if(f==n) printf("Queue underflow"); else { printf("%d is deleted",q[f]); return(q[f++]); } } void main() { int c,ele; l: clrscr(); printf("\nEnter 1 for inserting value at front of queue\nEnter 2 for inserting value at end of queue\n3 f...

C program to implement circular queue data structure

Simple C program to implement circular queue data structure Code: #include<stdio.h> #include<conio.h> int f=0,r=0,q[5],n=5; void insert(int ele) { if(r==n && f==0) printf("Queue overflow"); else if(r==f && r!=0) printf("Queue overflow"); else if(r==n && f!=0) { r=0; q[r]=ele; r++; printf("%d is inserted",ele); } else { q[r]=ele; r++; printf("%d is inserted",ele); } } int del() { if(f==r) { printf("Stack underflow"); f=0; r=0; } else if(f==n && r!=0) { f=0; printf("%d is deleted",q[f]); return(q[f++]); } else { printf("%d is deleted",q[f]); return(q[f++]); } } void main() { int c,ele; l: clrscr(); printf("\nEnter 1 for insert\nEnter 2 for delete"); scanf("%d",&c); if(c==1) { printf("Enter ele"); scanf("%d",&ele); insert(ele); } if(c==2) { del(); } printf("\nEnter 1 for exit...

C program to implement Circular Linked List

Simple C program to implement Circular Linked List with following operations: Insert a number Insert at front Insert after Insert before Delete at end Delete from front Delete after Delete before Delete itself Display from front Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node{ int data; struct node *next; struct node *prev; }; struct node *head,*tail; void insertend(){ int val; printf("\nEnter the value to be Entered: "); scanf("%d",&val); struct node *head2; head2=head; struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); temp->data=val; temp->next=NULL; temp->prev=NULL; if(head==NULL){ head=temp; temp->next=head; temp->prev=head; } else{ while(head2->next!=head){ head2=head2->next; } temp->prev=head2; temp->next=head; head2->next=temp; tail=temp; head->prev=tail; } } void insertf(){ int val; printf("Enter a value to be ent...

C Program to implement Doubly Linked list

Simple C Program to implement Doubly Linked list with following operations: Insertion at end Insertion at front Insert after a value Insert before a value Delete at end Delete at front Delete after a value Delete before a value Delete element itself Delete the whole list Display Code: #include<stdio.h> #include<conio.h> #include<stdlib.h>   struct node{  int data;  struct node *next;  struct node *prev;  }; struct node *head,*tail;   void insertend(){  int val;  printf("\nEnter the value to be Entered: ");  scanf("%d",&val);  struct node *head2;  head2=head;  struct node *temp;  temp=(struct node*)malloc(sizeof(struct node));  temp->data=val;  temp->next=NULL;  temp->prev=NULL;    if(head==NULL){  head=temp;  temp->next=head;  temp->prev=head;  }  else{  while(head2->next!=head){  head2=head2->next;  }  temp-...