Posts

Kruskal’s algorithm C++

Image
 Kruskal’s algorithm C++ Add caption #include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int cost[10][10],i,j,k,n,m,c,visit,visited[10],l,v,count,count1,vst,p; main() { int dup1,dup2; cout<<"enter no of vertices"; cin >> n; cout <<"enter no of edges"; cin >>m; cout <<"EDGE Cost"; for(k=1;k<=m;k++) { cin >>i >>j >>c; cost[i][j]=c; cost[j][i]=c; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(cost[i][j]==0) cost[i][j]=31999; visit=1; while(visit<n) { v=31999; for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(cost[i][j]!=31999 && cost[i][j]<v  && cost[i][j]!=-1 ) { int count =0; for(p=1;p<=n;p++) { if(visited[p]==i || visited[p]==j) count++; } if(count >= 2) { for(p=1;p<=n;p++) if(cost[i][p]!=31999 && p!=j) ...

c++ linked list

Image
c++ linked list #include<bits/stdc++.h> using namespace std; struct node     {         int x,*p;         struct node *next;     }; void addNode(struct node *ptr)     {         int i;         cout<<"Enter the value of x[x>0]";         cin>>i;         if(i<=0)         {             ptr->next=NULL;         }         else         {            struct node *temp=(struct node*)malloc(sizeof(struct node));            ptr->x=i;            ptr->next=temp;            addNode(ptr->next);         }     } ...

Menu C++

Image
Menu C++ Add caption #include<bits/stdc++.h> using namespace std; void insertFunction(); void deleteFunction(); void displayFunction(); void exitFunction(); int stackArray[100]; int topIndex = -1, item =0; int main() {     while(1)     {         system("cls");         printf("Enter your choice: \n1. Insert/Push\n2. Delete/POP\n3. Display\n4. Exit\n\n");         int a;         scanf("%d",&a);         switch(a)         {         case 1:             insertFunction();             break;         case 2:             deleteFunction();             break;         case 3:       ...

Stack C++

Image
Stack C++ #include<bits/stdc++.h> using namespace std; int main(){     stack<int>myStack;     int value;     cin>>value;     myStack.push(value);     cin>>value;     myStack.push(value);     cin>>value;     myStack.push(value);     myStack.push(value);     cin>>value;     myStack.push(value);     cin>>value;     myStack.push(value);     cout<<"stack top : "<<myStack.top()<<endl;     myStack.pop();     cout<<"stack top : "<<myStack.top();     while(  !myStack.empty()){         cout<<myStack.top()<<endl;         myStack.pop();     } }

Recursion C++

Image
Recursion C++ #include<bits/stdc++.h> using namespace std; int fact(int); int main(){     cout<< fact(31); } int fact( int i){     if ( i == 0)         return 1;     else         return i * fact( i - 1); }

maximum value C++

Image
maximum value C++ #include<iostream> using namespace std; int main(){     int a[100],mine;     cout<<"Enter any five array element : "<<endl;     for(int i=0;i<5;i++){         cin>>a[i];     }     mine=a[0];     for(int i=0;i<5;i++){         if(mine<a[i]){             mine=a[i];         }     }     cout<<"Maximum value : "<<mine<<endl; }

Selection sort C++

Image
         Selection sort C++ #include<bits/stdc++.h> using namespace std; int  main() {      int a[50],n,i,j;      cout<< "Enter the size of list : "<<endl;      cin>>n;      cout<< "Enter "<<n<< "th array element : "<<endl;      for(i=0; i<n; i++){            cin>> a[i];      }      for(i=0;i<n-1;i++){            for(j=i+1; j<n; j++){                 if(a[i] > a[j])                 {                    swap(a[j],a[i]);                  }            }      }      cout<< ...