0% found this document useful (0 votes)
56 views

Assignment Circular Linked List

The document contains code snippets for various data structures and algorithms problems including: 1. Circular linked list insertion and printing. 2. Doubly linked list insertion at front and back, deletion from front and back, and printing. 3. Tower of Hanoi recursive and non-recursive solutions. 4. Infix to postfix conversion using stack. 5. Evaluation of postfix expression using stack. 6. BFS and DFS graph traversal algorithms. 7. Tree traversal recursive solution.

Uploaded by

Mithila Arman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Assignment Circular Linked List

The document contains code snippets for various data structures and algorithms problems including: 1. Circular linked list insertion and printing. 2. Doubly linked list insertion at front and back, deletion from front and back, and printing. 3. Tower of Hanoi recursive and non-recursive solutions. 4. Infix to postfix conversion using stack. 5. Evaluation of postfix expression using stack. 6. BFS and DFS graph traversal algorithms. 7. Tree traversal recursive solution.

Uploaded by

Mithila Arman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Assignment

Circular Linked List


#include <stdio.h>

#include <stdlib.h>

struct Node

int data;

struct Node *next,*prev;

};

#define node struct Node

node *root;

void insert(int v)

node* nn=(node*)malloc(sizeof(Node));

nn->data=v;

nn->next=NULL;

nn->prev=NULL;

if(root==NULL)

root=nn;

return;

if(root->data>=v)

nn->next=root;

nn->next->prev=nn;

root=nn;

return;
}

node* cur=root;

while(cur->next!=NULL && cur->next->data<v)

cur=cur->next;

nn->next=cur->next;

cur->next=nn;

nn->prev=cur;

void print()

if(root==NULL)

puts("List is empty");

return;

printf("List :");

Node *cur;

cur=root;

while(cur!=NULL)

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

cur=cur->next;

puts("");

int main()

root=NULL;
int n,i,k;

scanf("%d",&n);

while(n--)

scanf("%d",&k);

insert(k);

print();

Doubly Linked List


#include<stdio.h>

#include<stdlib.h>

struct Node

int data;

struct Node *next;

struct Node *prev;

};

typedef struct Node node;

node *root,*tail;

void insertfront(int data)

node *nn=(node*)malloc(sizeof(node));

nn->data=data;

nn->next=nn;

nn->prev=nn;
if(root==NULL)

root=nn;

tail=nn;

else

nn->next=root;

nn->prev=tail;

root->prev=nn;

tail->next=nn;

root=nn;

void insertback(int data)

node *nn=(node*)malloc(sizeof(node));

nn->data=data;

nn->next=nn;

nn->prev=nn;

if(root==NULL)

root=nn;

tail=nn;

else

{
tail->next=nn;

nn->next=root;

nn->prev=tail;

tail=nn;

root->prev=tail;

void deletefront()

if(root==NULL)

return;

node *temp=root;

tail->next=root->next;

root=root->next;

root->prev=tail;

free(temp);

void deleteback()

if(root==NULL)

return;

node *temp=root;

node *cur=root;

while(cur->next!=root)
{

temp=cur;

cur=cur->next;

temp->next=root;

tail=temp;

root->prev=tail;

free(cur);

void print()

if(root==NULL)

return;

node *cur=root;

printf("List : ");

do

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

cur = cur->next;

}while(cur != root);

int main()

root=NULL;

tail=NULL;

int n,i,k;

scanf("%d",&n);

while(n--)
{

scanf("%d",&k);

insertback(k);

print();

Tower of hanoi recursive

#include <stdio.h>

void hanoi(int n,char a,char b,char c)

if (n==1)

printf("Move disk no. 1 from rod %c to rod %c\n",a,c);

return;

hanoi(n-1,a,c,b);

printf("Move disk no. %d from rod %c to rod %c\n",n,a,c);

hanoi(n-1,b,a,c);

int main()

int n;

printf("Enter number of disks : ");

scanf("%d",&n);

hanoi(n,'A','B','C');

}
Tower of hanoi non-recursive

#include <iostream>

using namespace std;

struct Node

int n;

char a,b,c;

struct Node* next;

};

struct Node* top;

void push(int n,char a,char b,char c)

struct Node* temp=(struct Node*)malloc(sizeof(struct Node));

temp->a=a;

temp->b=b;

temp->c=c;

temp->n=n;

temp->next=top;

top=temp;

void pop()

struct Node* temp=(struct Node*)malloc(sizeof(struct Node));

if(top==NULL)

return;

temp=top;

top=top->next;

free(temp);
}

int empty()

return(top==NULL)?1:0;

struct Node* Top()

return top;

int main()

int n;

scanf("%d",&n);

push(n,'A','B','C');

while(!empty())

struct Node* temp=Top();

n=temp->n;

char a=temp->a;

char b=temp->b;

char c=temp->c;

pop();

if(n==1)

printf("Move top disk from %c to %c\n",a,c);

else

push(n-1,b,a,c);

push(1,a,b,c);

push(n-1,a,c,b);
}

Infix to Postfix Notation


#include <bits/stdc++.h>

using namespace std;

char a[100],s[100],c;

int temp;

struct Node

char data;

int data2;

struct Node* next;

};

struct Node* top=NULL;

void push(int choice)

struct Node *newNode;

newNode=(struct Node*)malloc(sizeof(struct Node));

if(choice==1)

newNode->data=c;

else if(choice==2)

newNode->data2=temp;

if(top==NULL)

newNode->next=NULL;

else

newNode->next=top;

top=newNode;

}
void pop()

if(top==NULL)

return;

else

struct Node *temp=top;

top=temp->next;

free(temp);

char Top(int choice)

if(choice==1)

return top->data;

return top->data2;

int isEmpty()

return top==NULL;

int isNum(char ch)

return ch>='0' && ch<='9';

int prec(char ch)

int n=-1;

if(ch=='^')
n=3;

else if(ch=='*' || ch=='/')

n=2;

else if(ch=='+' || ch=='-')

n=1;

return n;

void getpostfix()

int i,j=0;

for(i=0;i<strlen(a);i++)

if(isNum(a[i]))

while(isNum(a[i]) && i<strlen(a))

s[j++]=a[i++];

i--;

s[j++]=' ';

else if(a[i]=='(')

c='(';

push(1);

else if(a[i]==')')

while(!isEmpty() && Top(1)!='(')

s[j++]=Top(1);
pop();

if(Top(1)=='(')

pop();

else

while(!isEmpty() && prec(a[i])<=prec(Top(1)))

s[j++]=Top(1);

pop();

c=a[i];

push(1);

while(!isEmpty())

s[j++]=Top(1);

pop();

s[j]='\0';

printf("Postfix : %s\n",s);

void evaluate()

int i,j;

for(i=0;i<strlen(s);i++)

{
if(isNum(s[i]))

j=0;

char b[100];

while(isNum(s[i]) && i<strlen(s))

b[j++]=s[i++];

i--;

if(s[i]!=' ')

i++;

b[j]='\0';

temp=atoi(b);

push(2);

else if(s[i]==' ')

i++;

else

int x=Top(2);

pop();

int y=Top(2);

pop();

switch(s[i])

case '+':

temp=y+x;

push(2);

break;

case '-':

temp=y-x;
push(2);

break;

case '*':

temp=y*x;

push(2);

break;

case '/':

temp=y/x;

push(2);

break;

case '^':

temp=pow(y,x);

push(2);

break;

int main()

gets(a);

getpostfix();

top=NULL;

evaluate();

printf("Result : %d",Top(2));

}
BFS

#include <bits/stdc++.h>

#define N 10000

using namespace std;

int n,m,grid[N][N];

int k,vis[N],ara[N];

struct Node

int data;

struct Node* next;

};

struct Node* front=NULL;

struct Node* rear=NULL;

void push(int x)

struct Node* temp=(struct Node*)malloc(sizeof(struct Node));

temp->data=x;

temp->next=NULL;

if(front==NULL && rear==NULL)

front=temp;

rear=temp;

return;

rear->next=temp;

rear=temp;

void pop()
{

struct Node* temp=front;

if(front==NULL)

return;

if(front==rear)

front=rear=NULL;

else

front=front->next;

free(temp);

int Front()

return front->data;

int isEmpty()

return front==NULL;

void bfs(int s)

memset(vis,0,sizeof(vis));

vis[s]=1;

push(s);

k=0;

while(!isEmpty())

int i,u=Front();

ara[k++]=u;

pop();
for(i=1;i<=n;i++)

if(grid[u][i]==1 && !vis[i])

push(i);

vis[i]=1;

int main()

int i,u,v,s;

scanf("%d %d",&n,&m);

for(i=0;i<m;i++)

scanf("%d %d",&u,&v);

grid[u][v]=1;

grid[v][u]=1;

printf("Enter starting node : ");

scanf("%d",&s);

bfs(s);

printf("Nodes reachable from Node %d is : ",s);

for(i=1;i<k;i++)

printf("%d ",ara[i]);

}
DFS

#include <bits/stdc++.h>

#define N 10000

using namespace std;

int n,m,grid[N][N],vis[N];

void dfs(int u)

vis[u]=1;

printf(" %d",u);

for(int i=1;i<=n;i++)

if(grid[u][i]==1 && !vis[i])

dfs(i);

int main()

int i,u,v;

scanf("%d %d",&n,&m);

for(i=0;i<m;i++)

scanf("%d %d",&u,&v);

grid[u][v]=1;

grid[v][u]=1;

memset(vis,0,sizeof(vis));

printf("DFS traversal :");

for(i=1;i<=n;i++)
{

if(!vis[i])

dfs(i);

Tree Treversal Recursive


#include <bits/stdc++.h>

using namespace std;

struct Node

int data;

Node* right;

Node* left;

};

Node* create(int data)

Node* newNode=(struct Node*)malloc(sizeof(struct Node));

newNode->data=data;

newNode->left=newNode->right=NULL;

return newNode;

Node* Insert(Node* root,int data)

if(root==NULL)

root=create(data);

else if(data<=root->data)

root->left=Insert(root->left,data);
else

root->right=Insert(root->right,data);

return root;

void printPostorder(Node* node)

if (node==NULL)

return;

printPostorder(node->left);

printPostorder(node->right);

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

void printInorder(Node* node)

if (node==NULL)

return;

printInorder(node->left);

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

printInorder(node->right);

void printPreorder(Node* node)

if (node==NULL)

return;

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

printPostorder(node->left);

printPostorder(node->right);

}
int main()

Node* root=NULL;

int i,n,k;

printf("Enter number of nodes : ");

scanf("%d",&n);

for(i=0;i<n;i++)

scanf("%d",&k);

root=Insert(root,k);

printf ("Post traversal : \n");

printPostorder(root);

printf ("\nInorder traversal : \n");

printInorder(root);

printf ("\nPreorder traversal : \n");

printPreorder(root);

Tree Treversal Non-Recursive


#include <bits/stdc++.h>

using namespace std;

struct Node

int data;

Node* right;

Node* left;

};

Node* root;
Node* create(int data)

Node* newNode=(struct Node*)malloc(sizeof(struct Node));

newNode->data=data;

newNode->left=newNode->right=NULL;

return newNode;

Node* Insert(Node* root,int data)

if(root==NULL)

root=create(data);

else if(data<=root->data)

root->left=Insert(root->left,data);

else

root->right=Insert(root->right,data);

return root;

void post(Node* node)

if (node==NULL)

return;

stack<Node*>s;

stack<int>st;

s.push(root);

while(!s.empty())

Node* cur=s.top();

s.pop();
st.push(cur->data);

if(cur->left)

s.push(cur->left);

if(cur->right)

s.push(cur->right);

while(!st.empty())

printf("%d ",st.top());

st.pop();

void in(Node* node)

if (node==NULL)

return;

stack<Node*>s;

Node *cur=root;

while (cur!=NULL || !s.empty())

while(cur!=NULL)

s.push(cur);

cur=cur->left;

cur=s.top();

s.pop();

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

cur=cur->right;
}

void pre(Node* node)

if(node==NULL)

return;

stack<Node*>s;

s.push(root);

while(!s.empty())

struct Node* temp=s.top();

s.pop();

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

if(temp->right)

s.push(temp->right);

if(temp->left)

s.push(temp->left);

int main()

root=NULL;

int i,n,k;

printf("Enter number of nodes : ");

scanf("%d",&n);

for(i=0;i<n;i++)

scanf("%d",&k);

root=Insert(root,k);
}

printf ("Post traversal : \n");

post(root);

printf ("\nInorder traversal : \n");

in(root);

printf ("\nPreorder traversal : \n");

pre(root);

BST (Insert, delete,search,min,max,successor, predecessor)

#include <bits/stdc++.h>

using namespace std;

struct Node

int data;

Node* right;

Node* left;

};

Node* create(int data)

Node* temp=new Node();

temp->data=data;

temp->left=NULL;

temp->right=NULL;

return temp;

Node* minval(Node* node)

{
Node* cur=node;

while(cur->left!=NULL)

cur=cur->left;

return cur;

Node* maxval(Node* node)

Node* cur=node;

while(cur->right!=NULL)

cur=cur->right;

return cur;

Node* insert(Node* root,int data)

if(root==NULL)

root=create(data);

else if(data<=root->data)

root->left=insert(root->left,data);

else

root->right=insert(root->right,data);

return root;

Node* del(Node* root,int data)

if(root==NULL)

return root;

if(data<root->data)

root->left=del(root->left,data);

else if(data>root->data)
root->right=del(root->right,data);

else

if(root->left==NULL)

struct Node* temp=root->left;

free(root);

return temp;

struct Node* temp=minval(root->right);

root->data=temp->data;

root->right=del(root->right,temp->data);

return root;

void inorder(Node* node)

if(node==NULL)

return;

inorder(node->left);

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

inorder(node->right);

int search(Node* node,int val)

if(node==NULL)

return 0;
if(node->data==val)

return 1;

int res=0;

res+=search(node->left,val);

res+=search(node->right,val);

return res>0;

void findpredecessor(Node* node,Node*& pre,int val)

if(node==NULL)

return;

if(node->data==val)

if(node->left!=NULL)

Node* cur=node->left;

while(cur->right!=NULL)

cur=cur->right;

pre=cur;

if(node->data>val)

findpredecessor(node->left,pre,val);

else

pre=node;

findpredecessor(node->right,pre,val);
}

void findsuccessor(Node* node,Node*& suc,int val)

if(node==NULL)

return;

if(node->data==val)

if(node->right!=NULL)

Node* cur=node->right;

while(cur->left!=NULL)

cur=cur->left;

suc=cur;

if(node->data>val)

suc=node;

findsuccessor(node->left,suc,val);

else

findsuccessor(node->right,suc,val);

int main()

Node* root=NULL;
int i,n,k;

root=insert(root,75);

root=insert(root,80);

root=insert(root,65);

root=insert(root,90);

root=insert(root,45);

root=insert(root,70);

root=insert(root,85);

printf("Max value is %d\n",maxval(root)->data);

printf("Min value is %d\n",minval(root)->data);

int key=50;

if(search(root,key)==0)

printf("Not found\n");

else

printf("Found\n");

Node* suc=NULL,*pre=NULL;

findsuccessor(root,suc,key);

printf("Successor of %d is %d\n",key,suc->data);

findpredecessor(root,pre,key);

printf("Successor of %d is %d\n",key,pre->data);

printf("Inorder traversal : ");

inorder(root);

root=del(root,70);

printf("\nInorder traversal after deleting : ");

inorder(root);

}
ONLINE
Balanced Parenthesis Check

#include <bits/stdc++.h>

using namespace std;

struct Node

char data;

struct Node* next;

};

struct Node* top;

void push(char ch)

struct Node *newNode;

newNode=(struct Node*)malloc(sizeof(struct Node));

newNode->data=ch;

if(top==NULL)

newNode->next=NULL;

else

newNode->next=top;

top=newNode;

void pop()

if(top==NULL)

return;
else

struct Node *temp=top;

top=temp->next;

free(temp);

char Top()

return top->data;

bool isEmpty()

if(top==NULL)

return true;

return false;

int main()

top=NULL;

char a[1000];

gets(a);

int i;

for(i=0;i<strlen(a);i++)

if(isEmpty())

push(a[i]);

continue;
}

if(a[i]=='(' || a[i]=='{' || a[i]=='[')

push(a[i]);

else

if(a[i]==')')

if(Top()=='(')

pop();

else

break;

else if(a[i]=='}')

if(Top()=='{')

pop();

else

break;

else if(a[i]==']')

if(Top()=='[')

pop();

else

break;

if(isEmpty())
puts("Balanced");

else

puts("Not balanced");

Tower of Hanoi 4 pegs


#include <stdio.h>

int cnt=0;

void hanoi(int n,int from,int to,int aux1,int aux2)

if(n==0)

return;

if(n==1)

printf("Move disk %d from rod %d to rod %d\n",n,from,to);

cnt++;

return;

hanoi(n-2,from,aux1,aux2,to);

printf("Move disk %d from rod %d to rod %d\n",n-1,from,aux2);

printf("Move disk %d from rod %d to rod %d\n",n,from,to);

printf("Move disk %d from rod %d to rod %d\n",n-1,aux2,to);

cnt+=3;

hanoi(n-2,aux1,to,from,aux2);

int main()

int n;

scanf("%d",&n);

hanoi(n,1,4,2,3);
printf("Movement = %d",cnt);

Infix to postfix + Valid check

#include <bits/stdc++.h>

using namespace std;

char a[100],s[100],c;

int temp;

struct Node

char data;

int data2;

struct Node* next;

};

struct Node* top=NULL;

void push(int choice)

struct Node *newNode;

newNode=(struct Node*)malloc(sizeof(struct Node));

if(choice==1)

newNode->data=c;

else if(choice==2)

newNode->data2=temp;

if(top==NULL)

newNode->next=NULL;

else

newNode->next=top;

top=newNode;

}
void pop()

if(top==NULL)

return;

else

struct Node *temp=top;

top=temp->next;

free(temp);

char Top(int choice)

if(choice==1)

return top->data;

return top->data2;

int isEmpty()

return top==NULL;

int isAlpha(char ch)

return ch>='a' && ch<='z';

int isNum(char ch)

return ch>='0' && ch<='9';

}
int isOp(char ch)

return ch=='+' || ch=='-' || ch=='*' || ch=='/';

int prec(char ch)

int n=-1;

if(ch=='/')

n=3;

else if(ch=='*')

n=2;

else if(ch=='+' || ch=='-')

n=1;

return n;

void getpostfix()

int i,j=0;

for(i=0;i<strlen(a)-1;i++)

if(isOp(a[i]) && isOp(a[i+1]))

printf("Not valid");

return;

int cnt=0;

for(i=0;i<strlen(a);i++)

{
if(isAlpha(a[i]) || isNum(a[i]))

cnt++;

if(!cnt)

printf("Not valid");

return;

cnt=0;

for(i=0;i<strlen(a);i++)

if(isOp(a[i]))

cnt++;

if(!cnt)

printf("Not valid");

return;

for(i=0;i<strlen(a);i++)

if(isAlpha(a[i]) || isNum(a[i]))

s[j++]=a[i];

else if(a[i]=='(')

c='(';

push(1);

}
else if(a[i]==')')

while(!isEmpty() && Top(1)!='(')

s[j++]=Top(1);

pop();

if(isEmpty())

printf("Not valid");

return;

if(Top(1)=='(')

pop();

else

printf("Not valid");

return;

else

while(!isEmpty() && prec(a[i])<=prec(Top(1)))

s[j++]=Top(1);

pop();

c=a[i];

push(1);
}

while(!isEmpty())

s[j++]=Top(1);

pop();

s[j]='\0';

for(i=0;s[i];i++)

if(s[i]=='(' || s[i]==')')

printf("Not valid");

return;

printf("Postfix : %s\n",s);

printf("Valid");

int main()

gets(a);

getpostfix();

BFS (Print all node’s level/distance from root)

#include <bits/stdc++.h>

#define N 100
using namespace std;

int n,m,vis[N],level[N],grid[N][N];

struct Node

int data;

struct Node* next;

};

struct Node* front=NULL;

struct Node* rear=NULL;

void push(int x)

struct Node* temp=(struct Node*)malloc(sizeof(struct Node*));

temp->data=x;

temp->next=NULL;

if(front==NULL && rear==NULL)

front=temp;

rear=temp;

return;

rear->next=temp;

rear=temp;

void pop()

if(front==NULL)

return;

struct Node* temp=front;

if(front==rear)
{

front=NULL;

rear=NULL;

else

front=front->next;

free(temp);

int Front()

return front->data;

int isEmpty()

return front==NULL;

void bfs(int u)

vis[u]=1;

push(u);

while(!isEmpty())

int i,v=Front();

pop();

for(i=0;i<n;i++)

if(grid[v][i] && !vis[i])

vis[i]=1;
level[i]=level[v]+1;

push(i);

int main()

scanf("%d",&n);

int i,a,b;

for(i=0;i<n-1;i++)

scanf("%d %d",&a,&b);

grid[a][b]=1;

grid[b][a]=1;

int s;

scanf("%d",&s);

bfs(s);

printf("Node\tLevel\n");

for(i=0;i<n;i++)

printf("%d\t%d\n",i,level[i]);

BST build + Valid check

#include <bits/stdc++.h>

using namespace std;

struct Node
{

int data;

Node* right;

Node* left;

};

Node* create(int data)

Node* temp=(struct Node*)malloc(sizeof(struct Node));

temp->data=data;

temp->left=NULL;

temp->right=NULL;

return temp;

Node* insert(Node* root,int data)

if(root==NULL)

root=create(data);

else if(data<=root->data)

root->left=insert(root->left,data);

else

root->right=insert(root->right,data);

return root;

void pre(Node* node)

if(node==NULL)

return;

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

pre(node->left);
pre(node->right);

void in(Node* node)

if(node==NULL)

return;

in(node->left);

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

in(node->right);

int isBst(Node* node)

if(node==NULL)

return 1;

if(node->left!=NULL && node->left->data>node->data)

return 0;

if(node->right!=NULL && node->right->data<node->data)

return 0;

if(!isBst(node->left) || !isBst(node->right))

return 0;

return 1;

int main()

Node* root=NULL;

int i,n,k;

root=insert(root,50);

root=insert(root,40);

root=insert(root,60);
root=insert(root,30);

root=insert(root,45);

root=insert(root,55);

root=insert(root,70);

root=insert(root,20);

root=insert(root,35);

in(root);

puts("");

pre(root);

puts("");

if(isBst(root))

puts("YES");

else

puts("NO");

Important

Number of Unmatched Parenthesis


#include <bits/stdc++.h>

using namespace std;

int cnt;

char a[10000];

struct Node

char data;

struct Node* next;

};

struct Node* top;


void push(char ch)

struct Node *newNode;

newNode=(struct Node*)malloc(sizeof(struct Node));

newNode->data=ch;

if(top==NULL)

newNode->next=NULL;

else

newNode->next=top;

top=newNode;

void pop()

if(top==NULL)

return;

else

struct Node *temp=top;

top=temp->next;

free(temp);

char Top()

return top->data;

bool isEmpty()

if(top==NULL)
return true;

return false;

void chk()

char x;

for(int i=0;i<strlen(a);i++)

if(a[i]=='(')

push('(');

continue;

if(isEmpty())

continue;

if(a[i]==')')

cnt+=2;

pop();

int main()

top=NULL;

gets(a);

chk();

printf("%d",strlen(a)-cnt);

}
Tower of Hanoi for N disk, K pegs

#include<bits/stdc++.h>

using namespace std;

#define INF 1000000000000000000

long long int mov[51][51][51],inn[51][51];

long long int func(long long int a,long long int b,long long int k)

if(a==0)

return 0;

if(a==1)

if(b<2)

return INF;

return 1;

if(b<3)

return INF;

long long int &aa=mov[a][b][k];

long long int &iin=inn[a][b];

if(aa!=-1)

return aa;

aa=INF;

if(iin!=-1)

if(iin>=k)

aa=func(iin,b,1)+func(a-iin,b-1,1)+func(iin,b,k);
else

aa=func(iin,b,1)+func(a-iin,b-1,k-iin);

return aa;

long long int ab=INF,in;

for(int i=1;i<a;i++)

long long int bb=2*func(i,b,1)+func(a-i,b-1,1);

if(bb<ab)

ab=bb;

in=i;

iin=in;

if(in>=k)

aa=func(in,b,1)+func(a-in,b-1,1)+func(in,b,k);

else

aa=func(in,b,1)+func(a-in,b-1,k-in);

return aa;

int main()

memset(mov,-1,sizeof(mov));

memset(inn,-1,sizeof(inn));

int disk,peg;

scanf("%d %d",&disk,&peg);

long long int tot=func(disk,peg,1);

If(tot==INF)
puts("Not possible");

else

printf("Movemnet = %lld",tot);

Number of connected components in graph

#include <bits/stdc++.h>

#define N 10000

using namespace std;

int n,m,grid[N][N],vis[N];

void dfs(int u)

vis[u]=1;

for(int i=1;i<=n;i++)

if(grid[u][i]==1 && !vis[i])

dfs(i);

int main()

int i,u,v;

scanf("%d %d",&n,&m);

for(i=0;i<m;i++)

scanf("%d %d",&u,&v);

grid[u][v]=1;

grid[v][u]=1;

}
memset(vis,0,sizeof(vis));

int comp=0;

for(i=1;i<=n;i++)

if(!vis[i])

comp++;

dfs(i);

printf("Number of components in graph is %d",comp);

Detect Cycle in graph

#include <bits/stdc++.h>

#define N 10000

using namespace std;

int grid[N][N],color[N];

int n,m,flag=0;

void dfs(int u)

color[u]=1;

for(int i=1;i<=n;i++)

if(grid[u][i]==1)

if(color[i]==0)

dfs(i);

else if(color[i]==1)
{

flag=1;

return;

color[u]=2;

int main()

int i,u,v;

scanf("%d %d",&n,&m);

for(i=0;i<m;i++)

scanf("%d %d",&u,&v);

grid[u][v]=1;

memset(color,0,sizeof(color));

for(i=1;i<=n;i++)

if(color[i]==0)

dfs(i);

if(flag==1)

puts("Cycle exists");

else

puts("No cycle");

}
Shortest Distance from source to destination
#include <bits/stdc++.h>

#define N 10000

using namespace std;

int n,m,grid[N][N];

int k,vis[N],cost[N];

struct Node

int data;

struct Node* next;

};

struct Node* front=NULL;

struct Node* rear=NULL;

void push(int x)

struct Node* temp=(struct Node*)malloc(sizeof(struct Node));

temp->data=x;

temp->next=NULL;

if(front==NULL && rear==NULL)

front=temp;

rear=temp;

return;

rear->next=temp;

rear=temp;

}
void pop()

struct Node* temp=front;

if(front==NULL)

return;

if(front==rear)

front=rear=NULL;

else

front=front->next;

free(temp);

int Front()

return front->data;

int isEmpty()

return front==NULL;

void bfs(int s,int d)

memset(vis,0,sizeof(vis));

memset(cost,0,sizeof(vis));

vis[s]=1;

cost[s]=0;

push(s);

while(!isEmpty())

int i,u=Front();
pop();

if(u==d)

return;

for(i=1;i<=n;i++)

if(grid[u][i]==1 && !vis[i])

push(i);

vis[i]=1;

cost[i]=cost[u]+1;

int main()

int i,u,v,s,d;

scanf("%d %d",&n,&m);

for(i=0;i<m;i++)

scanf("%d %d",&u,&v);

grid[u][v]=1;

grid[v][u]=1;

printf("Enter source : ");

scanf("%d",&s);

printf("Enter destination : ");

scanf("%d",&d);

bfs(s,d);
printf("Shortest distance from %d to %d is %d\n",s,d,cost[d]);

Height of a tree (BFS)

#include <bits/stdc++.h>

#define N 100

using namespace std;

int n,m,vis[N],level[N],grid[N][N],mx=-1;

struct Node

int data;

struct Node* next;

};

struct Node* front=NULL;

struct Node* rear=NULL;

void push(int x)

struct Node* temp=(struct Node*)malloc(sizeof(struct Node*));

temp->data=x;

temp->next=NULL;

if(front==NULL && rear==NULL)

front=temp;

rear=temp;

return;

rear->next=temp;

rear=temp;
}

void pop()

if(front==NULL)

return;

struct Node* temp=front;

if(front==rear)

front=NULL;

rear=NULL;

else

front=front->next;

free(temp);

int Front()

return front->data;

int isEmpty()

return front==NULL;

void bfs(int u)

vis[u]=1;

push(u);

while(!isEmpty())
{

int i,v=Front();

pop();

for(i=1;i<=n;i++)

if(grid[v][i] && !vis[i])

vis[i]=1;

level[i]=level[v]+1;

mx=max(mx,level[i]);

push(i);

int main()

scanf("%d",&n);

int i,a,b;

for(i=0;i<n-1;i++)

scanf("%d %d",&a,&b);

grid[a][b]=1;

int root;

printf("Enter source : ");

scanf("%d",&root);

bfs(root);

printf("Height of the tree is %d",mx+1);


}

Height of Binary tree/BST


#include <bits/stdc++.h>

using namespace std;

struct Node

int data;

Node* right;

Node* left;

};

Node* create(int data)

Node* temp=(struct Node*)malloc(sizeof(struct Node));

temp->data=data;

temp->left=NULL;

temp->right=NULL;

return temp;

Node* insert(Node* root,int data)

if(root==NULL)

root=create(data);

else if(data<=root->data)

root->left=insert(root->left,data);

else

root->right=insert(root->right,data);

return root;
}

int height(Node* node)

if(node==NULL)

return 0;

return max(height(node->left)+1,height(node->right)+1);

int main()

Node* root=NULL;

int i,n,k;

root=insert(root,50);

root=insert(root,40);

root=insert(root,60);

root=insert(root,30);

root=insert(root,45);

root=insert(root,55);

root=insert(root,70);

root=insert(root,20);

root=insert(root,35);

printf("Height of the tree is %d",height(root));

You might also like