Posts

Showing posts from April, 2016

bfs & dfs

#include<stdio.h> int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1; void bfs(int v) {  for(i=1;i<=n;i++)   if(a[v][i] && !visited[i])    q[++r]=i;  if(f<=r)  {   visited[q[f]]=1;   bfs(q[f++]);  } } void main() {  int v;  printf("\n Enter the number of vertices:");  scanf("%d",&n);  for(i=1;i<=n;i++)  {   q[i]=0;   visited[i]=0;  }  printf("\n Enter graph data in matrix form:\n");  for(i=1;i<=n;i++)   for(j=1;j<=n;j++)    scanf("%d",&a[i][j]);  printf("\n Enter the starting vertex:");  scanf("%d",&v);  bfs(v);  printf("\n The node which are reachable are:\n");  for(i=1;i<=n;i++)   if(visited[i])    printf("%d\t",i);   else    printf("\n Bfs is not possible"); } #include <stdio.h> int a[20][20],reach[20],n; void dfs(int v) {     int i;     reach...

Merge sorted two linked list into a single list

#include<stdio.h> #include<stdlib.h> #include<string.h> struct node { int data; struct node *next,*c; }c; struct node *insert(struct node *first,int data) { struct node *newnode=(struct node *)malloc(sizeof(struct node)); struct node *temp; newnode->data=data; newnode->next=NULL; if(first==NULL) { first=newnode; temp=first; } else { temp=first; while(temp->next!=NULL) { temp=temp->next; } temp->next=newnode; } return temp; } void display(struct node *temp) { while(temp!=NULL) { printf("%d\n",temp->data); temp=temp->next; } } struct node * merge(struct node *a,struct node *b){ struct node *f=NULL;  if(a ==  NULL)         return b;     else if(b == NULL)         return a;  if(a->data <= b->data){          f = a;         f->next = merge(a->next,b);   ...

merge sorted linked list in c

MergeSorted(Node a,Node b) if a is NULL and b is NULL return NULL if a is NULL return b if b is NULL return a Node c //Combined List if((*a).value<(*b).value) c=a (*c).next=MergeSorted((*a).next,b) else c=b (*c).next=MergeSorted(a,(*b).next) return c

cycle detection in list

int HasCycle(Node* head) { // Complete this function // Do not write the main method Node * fast = head; Node * slow = head; while(fast!=NULL && slow!=NULL && fast->next) { fast=fast->next->next; slow=slow->next; if(fast==slow) { return 1; } } return 0; }

merge point of list

int getCount(Node* head) { Node* current = head; int count = 0; while (current != NULL) { count++; current = current->next; } return count; } int getNode(int d, Node* head1, Node* head2) { int i; Node* current1 = head1; Node* current2 = head2; for(i = 0; i < d; i++) { if(current1 == NULL) { return -1; } current1 = current1->next; } while(current1 != NULL && current2 != NULL) { if(current1 == current2) return current1->data; current1= current1->next; current2= current2->next; } return -1; } int FindMergeNode(Node *headA, Node *headB) { // Complete this function // Do not write the main method. int c1 = getCount(headA); int c2 = getCount(headB); int d; if(c1 > c2) { d = c1 - c2; return getNode(d, headA, headB); } else { d = c2 - c1; return getNode(d, headB, headA); } }

reverse doubly linked list

void reverse( struct node **head_ref) {       struct node *temp = NULL;        struct node *current = *head_ref;               /* swap next and prev for all nodes of         doubly linked list */       while (current !=  NULL)       {         temp = current->prev;         current->prev = current->next;         current->next = temp;                      current = current->prev;       }                    /* Before changing head, check f...

reversing linked list using recurtion

PrintReverse(Node head) if head is NULL return //Nothing to do! PrintReverse((*head).next) //Print the remaining list print head's value                          Stack holding takes place recursively and list will be reversed recursively

Level order traversel in bst

/* struct node {     int data;     node* left;     node* right; }*/ int max(int a,int b)     {     if(a>b)         return a;     else         return b; }  int  height(struct node *root)      {      if(root==NULL)          return 0;      else          return 1+max(height(root->left),height(root->right));  } void printGivenLevel(struct node* root, int level) {     if (root == NULL)         return;     if (level == 1)         printf("%d ", root->data);     else if (level > 1)     {     ...

Top view Of Binary search tree

/* struct node {     int data;     node* left;     node* right; }; */ void inorder(node * root)     {     if(root!=NULL)         {         inorder(root->left);         printf("%d ",root->data);     } } void inorder1(node * root)     {     if(root!=NULL)         {         printf("%d ",root->data);         inorder1(root->right);             } } void top_view(node * root) {  inorder(root->left);    printf("%d ",root->data);     inorder1(root->right); }

Lowest common ancester in binary search tree

static node *lca(struct node *root,int n1,int n2)     {     if (root == NULL) return NULL;       if (root->data > n1 && root->data > n2)         return lca(root->left, n1, n2);     if (root->data < n1 && root->data < n2)         return lca(root->right, n1, n2);     return root;            }

Height of binary tree

/*The tree node has data, left child and right child struct node {     int data;     node* left;     node* right; }; */ int max(int a,int b)     {     if(a>b)         return a;     else         return b; } int height(node * root) {   if(root==NULL)return 0;     else         return 1+max(height(root->left),height(root->right)); }  

Java code to remove duplicates in string

import java.io.*; import java.util.*; public class Solution {     public static void main(String[] args) {        String string = "Johnwilliams"; Set<Character> c = new LinkedHashSet<Character>(); for(int i=0;i<string.length();i++)     {     c.add(string.charAt(i)); }         for(char c1:c)     {     System.out.print(c1); }                 } }

Permutaion of string

#include <stdio.h> #include <string.h> void swap(char *x, char *y) {     char temp;     temp = *x;     *x = *y;     *y = temp; } void permute(char *a, int l, int r) { int i; if (l == r)     printf("%s\n", a); else {     for (i = l; i <= r; i++)     {         swap((a+l), (a+i));         permute(a, l+1, r);         swap((a+l), (a+i));     } } } int main() {     char str[] = "MOHAN";     int n = strlen(str);     permute(str, 0, n-1);     return 0; }

BINARY SEARCH TREE IN C

#include<stdio.h> #include<string.h> struct node { int data; struct node *left; struct node *right; }; struct node *insert(struct node *root,int data) { struct node *newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=data; newnode->left=NULL; newnode->right=NULL; if(root==NULL) { root=newnode; } else if(root->data>data) { root->left=insert(root->left,data); } else { root->right=insert(root->right,data); } return root; } void in(struct node *root) { if(root!=NULL) { in(root->left); printf(" %d ",root->data); in(root->right); } } void in1(struct node *root) { if(root!=NULL) { in(root->left); printf(" %d ",root->data); } } void in2(struct node *root) { if(root!=NULL) { printf(" %d ",root->data); in(root->right); } } main() { struct node *root=NULL; root=insert(root,15); root=insert(root,10); root=insert(root,20); root=insert(root,8); root=insert(root,12); root=insert(root,16); root=ins...

My Text Encoder Application

Image