Assignment Circular Linked List
Assignment Circular Linked List
#include <stdlib.h>
struct Node
int data;
};
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;
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();
#include<stdlib.h>
struct Node
int data;
};
node *root,*tail;
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;
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();
#include <stdio.h>
if (n==1)
return;
hanoi(n-1,a,c,b);
hanoi(n-1,b,a,c);
int main()
int n;
scanf("%d",&n);
hanoi(n,'A','B','C');
}
Tower of hanoi non-recursive
#include <iostream>
struct Node
int n;
char a,b,c;
};
temp->a=a;
temp->b=b;
temp->c=c;
temp->n=n;
temp->next=top;
top=temp;
void pop()
if(top==NULL)
return;
temp=top;
top=top->next;
free(temp);
}
int empty()
return(top==NULL)?1:0;
return top;
int main()
int n;
scanf("%d",&n);
push(n,'A','B','C');
while(!empty())
n=temp->n;
char a=temp->a;
char b=temp->b;
char c=temp->c;
pop();
if(n==1)
else
push(n-1,b,a,c);
push(1,a,b,c);
push(n-1,a,c,b);
}
char a[100],s[100],c;
int temp;
struct Node
char data;
int data2;
};
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
top=temp->next;
free(temp);
if(choice==1)
return top->data;
return top->data2;
int isEmpty()
return top==NULL;
int n=-1;
if(ch=='^')
n=3;
n=2;
n=1;
return n;
void getpostfix()
int i,j=0;
for(i=0;i<strlen(a);i++)
if(isNum(a[i]))
s[j++]=a[i++];
i--;
s[j++]=' ';
else if(a[i]=='(')
c='(';
push(1);
else if(a[i]==')')
s[j++]=Top(1);
pop();
if(Top(1)=='(')
pop();
else
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];
b[j++]=s[i++];
i--;
if(s[i]!=' ')
i++;
b[j]='\0';
temp=atoi(b);
push(2);
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
int n,m,grid[N][N];
int k,vis[N],ara[N];
struct Node
int data;
};
void push(int x)
temp->data=x;
temp->next=NULL;
front=temp;
rear=temp;
return;
rear->next=temp;
rear=temp;
void pop()
{
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++)
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;
scanf("%d",&s);
bfs(s);
for(i=1;i<k;i++)
printf("%d ",ara[i]);
}
DFS
#include <bits/stdc++.h>
#define N 10000
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++)
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));
for(i=1;i<=n;i++)
{
if(!vis[i])
dfs(i);
struct Node
int data;
Node* right;
Node* left;
};
newNode->data=data;
newNode->left=newNode->right=NULL;
return newNode;
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;
if (node==NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
printf("%d ",node->data);
if (node==NULL)
return;
printInorder(node->left);
printf("%d ",node->data);
printInorder(node->right);
if (node==NULL)
return;
printf("%d ",node->data);
printPostorder(node->left);
printPostorder(node->right);
}
int main()
Node* root=NULL;
int i,n,k;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&k);
root=Insert(root,k);
printPostorder(root);
printInorder(root);
printPreorder(root);
struct Node
int data;
Node* right;
Node* left;
};
Node* root;
Node* create(int data)
newNode->data=data;
newNode->left=newNode->right=NULL;
return newNode;
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;
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();
if (node==NULL)
return;
stack<Node*>s;
Node *cur=root;
while(cur!=NULL)
s.push(cur);
cur=cur->left;
cur=s.top();
s.pop();
printf("%d ",cur->data);
cur=cur->right;
}
if(node==NULL)
return;
stack<Node*>s;
s.push(root);
while(!s.empty())
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;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&k);
root=Insert(root,k);
}
post(root);
in(root);
pre(root);
#include <bits/stdc++.h>
struct Node
int data;
Node* right;
Node* left;
};
temp->data=data;
temp->left=NULL;
temp->right=NULL;
return temp;
{
Node* cur=node;
while(cur->left!=NULL)
cur=cur->left;
return cur;
Node* cur=node;
while(cur->right!=NULL)
cur=cur->right;
return cur;
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;
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)
free(root);
return temp;
root->data=temp->data;
root->right=del(root->right,temp->data);
return root;
if(node==NULL)
return;
inorder(node->left);
printf("%d ",node->data);
inorder(node->right);
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;
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);
}
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);
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);
inorder(root);
root=del(root,70);
inorder(root);
}
ONLINE
Balanced Parenthesis Check
#include <bits/stdc++.h>
struct Node
char data;
};
newNode->data=ch;
if(top==NULL)
newNode->next=NULL;
else
newNode->next=top;
top=newNode;
void pop()
if(top==NULL)
return;
else
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;
}
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");
int cnt=0;
if(n==0)
return;
if(n==1)
cnt++;
return;
hanoi(n-2,from,aux1,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);
#include <bits/stdc++.h>
char a[100],s[100],c;
int temp;
struct Node
char data;
int data2;
};
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
top=temp->next;
free(temp);
if(choice==1)
return top->data;
return top->data2;
int isEmpty()
return top==NULL;
}
int isOp(char ch)
int n=-1;
if(ch=='/')
n=3;
else if(ch=='*')
n=2;
n=1;
return n;
void getpostfix()
int i,j=0;
for(i=0;i<strlen(a)-1;i++)
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]==')')
s[j++]=Top(1);
pop();
if(isEmpty())
printf("Not valid");
return;
if(Top(1)=='(')
pop();
else
printf("Not valid");
return;
else
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();
#include <bits/stdc++.h>
#define N 100
using namespace std;
int n,m,vis[N],level[N],grid[N][N];
struct Node
int data;
};
void push(int x)
temp->data=x;
temp->next=NULL;
front=temp;
rear=temp;
return;
rear->next=temp;
rear=temp;
void pop()
if(front==NULL)
return;
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++)
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]);
#include <bits/stdc++.h>
struct Node
{
int data;
Node* right;
Node* left;
};
temp->data=data;
temp->left=NULL;
temp->right=NULL;
return temp;
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;
if(node==NULL)
return;
printf("%d ",node->data);
pre(node->left);
pre(node->right);
if(node==NULL)
return;
in(node->left);
printf("%d ",node->data);
in(node->right);
if(node==NULL)
return 1;
return 0;
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
int cnt;
char a[10000];
struct Node
char data;
};
newNode->data=ch;
if(top==NULL)
newNode->next=NULL;
else
newNode->next=top;
top=newNode;
void pop()
if(top==NULL)
return;
else
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>
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;
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;
for(int i=1;i<a;i++)
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);
If(tot==INF)
puts("Not possible");
else
printf("Movemnet = %lld",tot);
#include <bits/stdc++.h>
#define N 10000
int n,m,grid[N][N],vis[N];
void dfs(int u)
vis[u]=1;
for(int i=1;i<=n;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);
#include <bits/stdc++.h>
#define N 10000
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
int n,m,grid[N][N];
int k,vis[N],cost[N];
struct Node
int data;
};
void push(int x)
temp->data=x;
temp->next=NULL;
front=temp;
rear=temp;
return;
rear->next=temp;
rear=temp;
}
void pop()
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;
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++)
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;
scanf("%d",&s);
scanf("%d",&d);
bfs(s,d);
printf("Shortest distance from %d to %d is %d\n",s,d,cost[d]);
#include <bits/stdc++.h>
#define N 100
int n,m,vis[N],level[N],grid[N][N],mx=-1;
struct Node
int data;
};
void push(int x)
temp->data=x;
temp->next=NULL;
front=temp;
rear=temp;
return;
rear->next=temp;
rear=temp;
}
void pop()
if(front==NULL)
return;
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++)
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;
scanf("%d",&root);
bfs(root);
struct Node
int data;
Node* right;
Node* left;
};
temp->data=data;
temp->left=NULL;
temp->right=NULL;
return temp;
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;
}
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);