Thursday, August 14, 2014

IP address program

Really short C program to print out your ip address. Run it as an .exe. 


#include<stdlib.h>
main()
{
system("C:\\Windows\\System32\\ipconfig");
}

Wednesday, August 13, 2014

Decimal to Binary Program

Simple program to Convert a decimal number to binary. If you have doubts, let me know
#include <stdio.h>
main()
{
int num,count,temp;
printf("Enter the number");
scanf("%d",&num);
for(count=31;count>= 0;count--)
{
temp=num>>count;
if(temp&1)
printf("1");
else
printf("0");
}
}

Tuesday, August 12, 2014

Program to copy a file

C program to copy file. If you have any doubts, let me know

#include <stdlib.h>
#include <stdio.h>
main()
{
   
FILE *source, *target;
char x, sourcef[100], targetf[100];
printf("File to copy?"); gets(sourcef);   source=fopen(sourcef, "r");  printf("Target File?"); gets(targetf);   target=fopen(targetf, "w");   while((x=fgetc(source))!=EOF) fputc(x, target);   printf("The file has been copied."); }

Program to read from file

Program to read from a file. If you have any doubts, let me know

#include <stdlib.h>
#include <stdio.h>
main()
{
char x, name[100];
FILE *pf;
    printf("Name of file?");
    gets(name);
    pf = fopen(name,"r");
    printf("Contents are :");
         while((x=fgetc(pf))!= EOF)
         printf("%c",x);
    }

Saturday, August 9, 2014

Program to find twin prime numbers.

Program to find twin prime numbers. Please comment if you need help.

#include<stdio.h>
void main( )
{
int  i,n,k,r,a[50],p;
printf("nEnter the range:  ");
scanf("%d", &r);
i=1;
p=0;
while(i<=r)
{
k=0;
n=1;
while(n<=i)
{
if( i%n==0 )
k++;
n++;
}
if(k==2)
{
a[p]=i;
p++;
}
i++;
}
for(n=0;n<p;n++)
{
if(a[n+1] - a[n]==2)
printf("\n%d and %d are twin primes", a[n], a[n+1]);
}
}

Program to implement Fibonacci series

Program to implement Fibonacci series. Comment if you need any help.

#include<stdio.h>
int fibo(int x);
main()
{
int n,i,ar[50];
printf("\n  enter the number of terms:");
scanf("%d",&n);
ar[0]=0;
for(i=1;i<n;i++)
{
ar[i]=fibo(i);
}
printf("\n");
for(i=0;i<n;i++)
printf("%d ",ar[i]);
printf("\n");
}
int fibo(int c)
{
int fib=0;
if((c==1)||(c==2))
return 1;
else
{
fib=fibo(c-1)+fibo(c-2);
return fib;
}
}





Program to write to a file and display the vowel which occurred the most

Program to write to a file and display the vowel which occurred the most often. If you need help comment below

#include<stdio.h>
main()
{
  FILE *f1;
  char c;
  int a,e,i,o,u;
  a=0;
  e=0;
  i=0;
  u=0;
  o=0;
  printf("\nEnter the letters to the file:\n");
  f1=fopen("TEXT.txt","w");
  while((c=getchar())!='\n')
    putc(c,f1);
  fclose(f1);
  f1=fopen("TEXT.txt","r");
  while((c=getc(f1))!=EOF)
   {
     if(c=='a')
       a++;
     else if(c=='e')
       e++;
     else if(c=='i')
       i++;
     else if(c=='o')
       o++;
     else if(c=='u')
       u++;
   }
  fclose(f1);
  if((a>=e)&&(a>=i)&&(a>=o)&&(a>=u))
   printf("\n\n'a'is the vowel which occured the most.\n%d times",a);
  if((e>=a)&&(e>=i)&&(e>=u)&&(e>=o))
   printf("\n\n'e' is the vowel which occured the most.\n%d times",e);
  if((i>=a)&&(i>=e)&&(i>=o)&&(i>=u))
   printf("\n\n'i' is the vowel which occured the most.\n%d times",i);
  if((o>=a)&&(o>=e)&&(o>=i)&&(o>=u))
   printf("\n\n'o'is the vowel which occured the most.\n%d times",o);
  if((u>=a)&&(u>=e)&&(u>=i)&&(u>=o))
   printf("\n\n'u' is the vowel which occured the most.\n%d times",u);
  printf("\n\n");
}

Program to alphabetically sort names.

Program to alphabetically sort names. If you need help, comment below

#include<stdio.h>
#include<string.h>
void sort(int);
void disp(int);
struct student
       {char name[25];
       }a[50],*ptr;
int main()
       {ptr=a;
        int i,j,n;
        printf("\nEnter the number of students:");
        scanf("%d",&n);
        n++;
        printf("\nEnter the names:\n");
        for(i=0;i<n;i++)
               gets((ptr+i)->name);
        sort(n);
        disp(n);
        return 0;
       }
void sort(int n)
       {

        int i,j;
        char copy[25];
        for(i=0;i<n;i++)
               for(j=0;j<strlen((ptr+i)->name);j++)
                       {if((ptr+i)->name[j]<97)
                               (ptr+i)->name[j]+=32;
                       }

        for(i=0;i<n;i++)
               for(j=0;j<n-1;j++)
                       if(strcmp((ptr+j)->name,(ptr+j+1)->name)>0)
                               {strcpy(copy,(ptr+j)->name);
                                strcpy((ptr+j)->name,(ptr+j+1)->name);
                                strcpy((ptr+j+1)->name,copy);
                               }
       }







void disp(int n)
       {
        int i;
        for(i=0;i<n;i++)
               (ptr+i)->name[0]-=32;
        printf("\n\nThe sorted list is:\n");
        for(i=1;i<n;i++)
               puts((ptr+i)->name);

       }

Program for various operations with doubly link list

Program for the various operations with a doubly link list. Comment below if you need help

#include<stdio.h>

#include<stdlib.h>

struct node

{

 int data;

 struct node *next,*prev;

}*start=NULL,*newn,*temp,*ptr;

int num,pos,ele;

void insert_beg();

void insert_beg();

void insert_end();

void insert_aftr();

void insert_pos();

void delete_beg();

void delete_end();

void delete_ele();

void display();

main()

{

 int ch,c,ch1,p,ch2,q;

 do

 {

 printf("\n DLL MENU\n1.Ins\n2.Del\n3.Disp");

 printf("\nEnter the number corresponding to your operation:");

 scanf("%d",&ch);

 switch(ch)

 {

 case 1:do

 {

 printf("\nINSERT MENU\n1.beginning\n2.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\n");

 break;

 }

 printf("\nDo you want to continue-Insert Menu?(1.Yes;2.No)");

 scanf("%d",&p);

 }while(p!=2);

 break;

 case 2:do

 {

 printf("\nDELETE MENU\n1.Front\n2.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\n");

 break;

 }

 printf("\nDo you want to continue-Delete Menu?(1.Yes;2.No):");

 scanf("%d",&q);

 }while(q!=2);

 break;

 case 3:display();

 break;

 default:printf("\nWrong Choice\n");

 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->next!=NULL)

 {

 printf("%d<->",temp->data);

 temp=temp->next;

}

printf("%d",temp->data);

 }

}

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;

 newn->prev=NULL;

 }

 else

 {

 newn->prev=NULL;

 newn->next=start;

 start->prev=newn;

 }

 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;

 newn->prev=NULL;

 start=newn;

 }

 else

 {

 temp=start;

 while(temp->next!=NULL)

 {

 temp=temp->next;

 }

 temp->next=newn;

 newn->prev=temp;

 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)

 {

 if(temp->next==NULL)

 insert_end();

 else

{ newn->next=temp->next;

 newn->prev=temp;

 temp->next->prev=newn;

 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;

 while(temp!=NULL)

 {

 k++;

 if(k==pos)

 {

 newn->next=temp;

 temp->prev->next=newn;

 newn->prev=temp->prev;

 temp->prev=newn;

 flag=1;

 break;

 }

 temp=temp->next;

 }

 if(flag==0)

 printf("\nPosition not found\n");

 }

}

void delete_beg()

{

 if(start==NULL)

 printf("\nLL is empty\n");

 else

{

 if(start->next==NULL)

{

free(start);

start=NULL;

}

else

 {

 temp=start;

 start=start->next;

 start->prev=NULL;

 free(temp);

 }

}

}

void delete_end()

{

 if(start==NULL)

 printf("\nLL is empty\n");

 else if(start->next==NULL)

 delete_beg();

 else

 {ptr =temp;

 temp=start;

 while(temp->next!=NULL)

 {

 temp=temp->next;

 }

 temp->prev->next=NULL;

 free(temp);

 }

}

void delete_ele()

{

 int flag=0;

 if(start==NULL)

 printf("\nLL 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;

 break;

 }

 else if(temp->data==num)

 {

 if(temp->next==NULL)

 {

 delete_end();

 flag=1;

 break;

 }

 else

 {

 ptr=temp;

 temp->prev->next=temp->next;

 temp->next->prev=temp->prev;

 free(ptr);

 flag=1;

 break;

 }

 }

 temp=temp->next;

 }

 if(flag==0)

 printf("\nElement not found\n");

 }

}

Program for polynomial addition

This is a program for polynomial addition. Please be sure to enter the coefficients in the decreasing order of power. Comment below if you need help
#include<stdio.h> struct node { int coef; int pow; struct node *link; }*poly,*poly1=NULL,*poly2=NULL,*node,*next; void polcreate(struct node *next) { int ch; do { printf("\nCoeff of the term?"); scanf("%d",&next->coef); printf("\nPower of the term?"); scanf("%d",&next->pow); next->link=(struct node*)malloc(sizeof(struct node)); next=next->link; printf("\nEnter 1 to continue press 2 to terminate the expression : "); scanf("%d",&ch); }while(ch==1); } void show(struct node *next) { while(next->link!=NULL) { printf("%dX^%d",next->coef,next->pow); next= next->link; if( next->link!=NULL) printf("+"); } printf("\n"); } void polyadd(struct node *poly1,struct node *poly2,struct node *poly) { while(poly1->link && poly2->link) { if(poly1->pow > poly2->pow) { poly->pow= poly1->pow; poly->coef= poly1->coef; poly1= poly1->link; } else if(poly1->pow < poly2->pow) { poly->pow= poly2->pow; poly->coef= poly2->coef; poly2= poly2->link; } else { poly->pow= poly1->pow; poly->coef= poly1->coef + poly2->coef; poly1= poly1->link; poly2= poly2->link; } poly->link= (struct node*)malloc(sizeof(struct node)); poly=poly->link; poly->link=NULL; } while(poly1->link||poly2->link) { if(poly1->link) { poly->pow=poly1->pow; poly->coef=poly1->coef; poly1= poly1->link; } if(poly2->link) { poly->pow= poly2->pow; poly->coef= poly2->coef; poly2= poly2->link; } poly->link= (struct node*)malloc(sizeof(struct node)); poly = poly->link; poly->link=NULL; } } int main() { poly1=(struct node*)malloc(sizeof(struct node)); poly2=(struct node*)malloc(sizeof(struct node)); poly=(struct node*)malloc(sizeof(struct node)); printf("\nPOLYNOMIAL 1(decreasing order of power)\n"); polcreate(poly1); printf("\nPOLYNOMIAL 2(decreasing order of power)\n"); polcreate(poly2); printf("\nFirst polynomial : \n"); show(poly1); printf("Second polynomial : \n"); show(poly2); polyadd(poly1,poly2,poly); printf("\n sum is as follows\n"); show(poly); }

Program for quick sort

Program to implement quick sort. If you need help, comment below

#include<stdio.h> int a[20],b[20],n,z; void quick(int x[],int first,int last) { int pivot; if(first<last) { pivot=partition(x,first,last); quick(x,first,pivot-1); quick(x,pivot+1,last); } } int partition(int x[],int first,int last) { int pivot,temp,i,j; pivot=first; i=first; j=last; while(i<j) { while((x[i]<=x[pivot])&&(i<last)) i++; while(x[j]>x[pivot]) j--; if(i<j) { temp=x[i]; x[i]=x[j]; x[j]=temp; } for(z=1;z<=n;z++) printf("%d\t ",a[z]); printf("\n"); } temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; return j; } int main() { int i,j,c,ch; printf("\nQuicksort"); printf("\nEnter the no. of elements in the array:\t"); scanf("%d",&n); printf("\nEnter the elements into the array:\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); quick(a,1,n); printf("\nAfter sorting:\n"); for(i=1;i<=n;i++) printf("%d ",a[i]); }

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));

Program for merge sort.

Program for merge sort. If you need help, feel free to ask.

#include<stdio.h> void mergesort(int x[20],int n) { int temp[20],i,j,k,l1,l2,size,u1,u2,a; size=1; while(size<n) { l1=0; k=0; while(l1+size<n) { l2=l1+size; u1=l2-1; u2=((l2+size-1<n)?l2+size-1:n-1); for(i=l1,j=l2;i<=u1&&j<=u2;k++) { if(x[i]<=x[j]) { temp[k]=x[i]; i++; } else { temp[k]=x[j]; j++; } } for(;i<=u1;k++) { temp[k]=x[i]; i++; } for(;j<=u2;k++) { temp[k]=x[j]; j++; } l1=u2+1; } for(i=l1;k<n;k++) temp[k++]=x[i]; for(i=0;i<n;i++) x[i]=temp[i]; size=size*2; printf("\n"); for(a=0;a<n;a++) printf("%d ",x[a]); printf("\n"); } } main() { int x[20],n,i; printf("\nEnter the value of the number of elements you wish to enter\n"); scanf("%d",&n); printf("\nEnter values of the elements you wish to sort"); for(i=0;i<n;i++) scanf("%d",&x[i]); printf("\nBefore Sorting this is how it looks\n:"); for(i=0;i<n;i++) printf(" %d",x[i]); mergesort(x,n); }

Program for heap sort

Program for Heap Sort. Kinda tricky program. If you need help, comment below

#include<stdio.h> int tree[20],n,ah[20],c=0; void build(int m,int item) { int ptr,par,f=0; m=m+1; ptr=m; while(ptr>1) { par=ptr/2; if(item<=tree[par]) { tree[ptr]=item; f=1; break; } tree[ptr]=tree[par]; ptr=par; } if(f==0) tree[1]=item; } void delheap(int k,int m) { int left,right,ptr,last,i; ah[k]=tree[1]; last=tree[m]; m=m-1; ptr=1; left=2; right=3; while(right<=m) { if((last>=tree[left])&&(last>=tree[right])) { tree[ptr]=last; return; } if(tree[right]<=tree[left]) { tree[ptr]=tree[left]; ptr=left; } else { tree[ptr]=tree[right]; ptr=right; } left=2*ptr; right=left+1; } if((left==m)&&(last<tree[left])) { tree[ptr]=tree[left]; ptr=left; } tree[ptr]=last; c++; printf("\n\nPass %d: ",c); for(i=1;i<=m;i++) printf("%d ",tree[i]); } main() { int i,j; printf("\nEnter total number of elements:"); scanf("%d",&n); printf("\nEnter elements to be inserted : "); for(i=1;i<=n;i++) scanf("%d",&tree[i]); for(i=1;i<n;i++) build(i,tree[i+1]); printf("\n\nHeap : "); for(i=1;i<=n;i++) printf("%d ",tree[i]); for(i=n,j=1;i>1;i--,j++) delheap(j,i); if(i==1) ah[n]=tree[1]; printf("\n\nHeap sort : "); for(i=1;i<=n;i++) printf("%d ",ah[i]); }

Another program for DFS and BFS

Another implementation of DFS and BFS. Comment if you need help.

#include<stdio.h> #include<stdlib.h> //Structure for Vertex Nodes struct vertexNode { int element; int visited; struct edgeNode *edge; struct vertexNode *next; }; typedef struct vertexNode vertexNode; vertexNode *start = NULL; //Structure for Edge Nodes struct edgeNode { struct vertexNode *connectsTo; struct edgeNode *next; }; typedef struct edgeNode edgeNode; //Function for creating new vertex node void addVertex(int element) { vertexNode *newVertex,*temp; //Allocating memory for new vertex node and initializing the values newVertex = (vertexNode *) malloc(sizeof(vertexNode)); newVertex->element = element; newVertex->visited =0; newVertex->edge=NULL; newVertex->next=NULL; if(start==NULL) //If the graph is empty { start=newVertex; } else //If the graph is not empty { temp= start; while(temp->next!=NULL) { temp = temp->next; } temp->next = newVertex; } } //Function for creating new edge node void addEdge(int v1, int v2) { edgeNode *newEdge,*temp; vertexNode *temp1,*v,*u; temp1 = start; /*Finding the source and destination vertex v is the source vertex and u is the destination vertex*/ while(temp1) { if(temp1->element == v1) v=temp1; if(temp1->element == v2) u=temp1; temp1=temp1->next; } newEdge = (edgeNode *) malloc(sizeof(edgeNode)); newEdge->connectsTo = u; newEdge->next = NULL; if(v->edge==NULL) { v->edge = newEdge; } else { temp=v->edge; while(temp->next!=NULL) { temp=temp->next; } temp->next=newEdge; } } //Function for displaying the graph void displayGraph() { vertexNode *temp1; edgeNode *temp2; temp1=start; while(temp1) { printf("%d->",temp1->element); if(temp1->edge!=NULL) { temp2=temp1->edge; while(temp2) { printf("%d->",temp2->connectsTo->element); temp2=temp2->next; } } printf("\n|\n"); printf("v\n"); temp1=temp1->next; } } struct stackNode { vertexNode *vElement; struct stackNode *next; }; typedef struct stackNode stackNode; stackNode *top = NULL; //Stack push function void push(vertexNode *v) { stackNode *newStackNode; newStackNode = (stackNode *) malloc(sizeof(stackNode)); newStackNode->vElement = v; newStackNode->next = NULL; if(top==NULL) { top=newStackNode; } else { newStackNode->next = top; top = newStackNode; } } //Stack pop function vertexNode * pop() { vertexNode *temp=NULL; if(top==NULL) { printf("\nStack is Empty"); } else { temp = top->vElement; top = top->next; } return(temp); } void dfs() { vertexNode *temp; edgeNode *temp2; push(start); while(top) { temp = pop(); if(temp->visited==0) { printf("%d\t",temp->element); temp->visited = 1; } temp2=temp->edge; while(temp2) { if(temp2->connectsTo->visited==0) push(temp2->connectsTo); temp2=temp2->next; } } } //Structure for Queue Nodes struct queueNode { vertexNode *vElement; struct queueNode *next; }; typedef struct queueNode queueNode; queueNode *qFront=NULL,*qRear=NULL; //Queue Insert function void enQueue(vertexNode *v) { queueNode *tmp_ptr=NULL; tmp_ptr=(queueNode *)malloc(sizeof(queueNode)); tmp_ptr->vElement = v; tmp_ptr->next = NULL; if(qFront == NULL) { qFront=tmp_ptr; qRear=tmp_ptr; } else { qRear->next=tmp_ptr; qRear=tmp_ptr; } } //Queue Delete function vertexNode * deQueue() { /*Delete element from Queue*/ vertexNode *temp=NULL; temp = qFront->vElement; if(qFront==NULL) printf("\nThe Queue is Empty"); else if(qFront==qRear) qFront=qRear=NULL; else qFront=qFront->next; return(temp); } //Function for Breadth First Search void bfs() { vertexNode *temp; edgeNode *temp2; enQueue(start); while(qFront) { temp =deQueue(); if(temp->visited==0) { printf("%d\t",temp->element); temp->visited = 1; } temp2=temp->edge; while(temp2) { enQueue(temp2->connectsTo); temp2=temp2->next; } } } int main() { int i,num,v1,v2; do { printf("\n1.INSERT\n2.ADD EDGE\n3.BFS traversal\n4.DFS traversal\n5.DISplay\n6.exit\nenter your choice:"); scanf("%d",&i); switch(i) { case 1: printf("\nEnter vertex:\t"); scanf("%d",&num); addVertex(num); break; case 4: dfs(); break; case 2: printf("\nEnter vertices to be connected:\t"); scanf("%d%d",&v1,&v2); addEdge(v1,v2); break; case 3: bfs(); break; case 5: displayGraph(); break; case 6: break; default: printf("ERROR! Invalid Choice..\n"); } } while(i!=6); }

DFS and BFS of a tree

Program for DFS and BFS of a tree. Comment below if you need help

#include<stdio.h> int adj[50][50]; int visited[50]; void adjcreate(); void dfs(int); void bfs(int); int n; main() { int i, v, ch; adjcreate(); do { printf("\nMENU:\n1.DFS\n2.BFS\n3.Exit\n"); printf("Enter your choice:"); scanf("%d",&ch); switch (ch) { case 1: printf("\nEnter starting node for Depth First Search:"); scanf("%d",&v); for (i=1;i<=n;i++) visited[i]=0; dfs(v); break; case 2: printf("\nEnter starting node for Breadth First Search:"); scanf("%d",&v); for (i=1;i<=n;i++) visited[i]=0; bfs(v); break; case 3: break; default: printf("Wrong choice!\n"); break; } }while(ch!=3); } void adjcreate() { int i,j,ch; printf("Enter number of nodes :"); scanf("%d", &n); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("\n Press 1 if %d connected to %d",i,j); scanf("%d",&ch); if(ch==1) { adj[i][j]=1; } else { adj[i][j]=0; } } } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf(" %d ",adj[i][j]); printf("\n"); } } void dfs(int v) { int i,stack[50],top=-1,pop; top++; stack[top]=v; while(top>=0) { pop=stack[top]; top--; if(visited[pop]==0) { printf("%d ",pop); visited[pop]=1; } else continue; for(i=n;i>=1;i--) { if(adj[pop][i]==1&&visited[i]==0) { top++; stack[top]=i; } } } } void bfs(int v) { int i,front,rear; int que[50]; front=rear=-1; printf("%d ",v); visited[v]=1; rear++; front++; que[rear]=v; while(front<=rear) { v=que[front]; front++; for (i=1;i<=n;i++) { if (adj[v][i]==1&&visited[i]==0) { printf("%d ",i); visited[i]=1; rear++; que[rear]=i; } } } }