Thursday, October 8, 2015

Merge a linked list into another linked list at alternate positions

#include <iostream>
using namespace std;
struct node
{
int  data;
node *next;
};

void print(node *t)
{
if(t!=NULL)
{

cout<<t->data<<" ";
print(t->next);

}

}

node * create(int  a)
{
node *t=new node;
t->data=a;
t->next=NULL;
return t;
}
node * altswap(node *t,node *l)
{
if(t==NULL )return l;
if(l==NULL) return t;
node *x,*y;
x=t;
while(t&&l){
y=t->next;
t->next=l;
l=y;
t=t->next;
}

return x;
}
int main()
{
node *t=NULL;
t=create(4);
t->next=create(2);
t->next->next=create(11);
t->next->next->next=create(43);
t->next->next->next->next=create(13);
t->next->next->next->next->next=create(45);
node *l=NULL;
l=create(1);
l->next=create(3);
l->next->next=create(12);
l->next->next->next=create(63);
l->next->next->next->next=create(153);
l->next->next->next->next->next=create(145);

print(t);
cout<<endl;
print(l);
cout<<endl;
t=altswap(t,l);
print(t);
return 0;
}

Swap nodes in a linked list without swapping data

#include <iostream>
using namespace std;
struct node
{
int  data;
node *next;
};

void print(node *t)
{
if(t!=NULL)
{

cout<<t->data<<" ";
print(t->next);

}

}

node * create(int  a)
{
node *t=new node;
t->data=a;
t->next=NULL;
return t;
}
node * swap(node *t,int a,int b)
{
if(t==NULL ||t->next==NULL)
return t;
node *x,*y;
x=t;
while(x->data!=a&&x)
x=x->next;
y=t;
while(y->data!=b)
y=y->next;

node *m=y->next;
node *t1=t;
while(t1->next!=x&&t1->next)
t1=t1->next;
t1->next=y;
y->next=x->next;
node *y1=y;
while(y->next!=y1)
y=y->next;
y->next=x;
x->next=m;
return t;
}
int main()
{
node *t=NULL;
t=create(4);
t->next=create(2);
t->next->next=create(11);
t->next->next->next=create(43);
t->next->next->next->next=create(13);
t->next->next->next->next->next=create(45);
print(t);
cout<<endl;
t=swap(t,2,13);
print(t);
return 0;
}

Wednesday, October 7, 2015

Binary Tree is BST or not c++

#include <iostream>
#include <limits.h>
using namespace std;
struct node
{
int  data;
node *left,*right;
};

void print(node *t)
{
if(t!=NULL)
{
print(t->left);
cout<<t->data<<" ";
print(t->right);

}

}

node * create(int  a)
{
node *t=new node;
t->data=a;
t->left=t->right=NULL;
return t;
}
int isBST(node *t,int min,int max)
{
if(t==NULL)return 1;
if(t!=NULL)
{
if(t->data<min||t->data>max)
return 0;
}

return (isBST(t->left,min,t->data)&&isBST(t->right,t->data,max));
}
int main()
{
node *t=NULL;
t=create(4);
t->left=create(2);
t->right=create(1);
print(t);
if(isBST(t,INT_MIN,INT_MAX))
cout<<" BST \n";
else cout<< "not BST\n";

return 0;
}

String to int and Int to string c++

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
stringstream ss;
// string to int
string str="12";
ss<<str;
int a;
ss>>a;
cout<<a<<"\n";
//int to string
ss<<a;
string str1;
ss>>str1;
cout<<str1<<" ";

return 0;
}

Thursday, September 17, 2015

Reverse and Block wise reverse of given Linked List

#include <iostream>
using namespace std;
struct node {
int data;
node *next;
};
void create(node *&t,int a)
{
if(t==NULL)
{
t=new node;
t->data=a;
t->next=NULL;
}
else
{
create(t->next,a);
}
}
void rev(node *&t)
{
node *temp=NULL;
node *next;
node *cur=t;
while(t!=NULL)
{
next=t->next;
t->next=temp;
temp=t;
t=next;
}
t=temp;
}
void print(node *t)
{

while(t!=NULL)
{
cout<<t->data<<" ";
t=t->next;
}
cout<<endl;
}
node *blockrev(node *t,int a)
{
node *next,*cur=t;
node *head;
int flag=0;

node *temp=NULL;
node *curhead=t;
for(int i=0;i<a&&t!=NULL;i++)
{
next=t->next;
t->next=temp;
temp=t;
t=next;
}

if(next!=NULL)
curhead->next=blockrev(next,a);

return temp;
}
int main()
{
int a[]={2,3,4,5,6,7,8,'\0'};
int n=7;
node *t=NULL;
for(int i=0;i<n;i++)
{
create(t,a[i]);
}
print(t);
cout<<"reversing the list\n";
rev(t);
print(t);
rev(t);
cout<<"\nblock wise reverse of list\n";

t=blockrev(t,2);
print(t);
rev(t);
print(t);
return 0;
}

Friday, September 4, 2015

first come first serve Operating system c code

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

struct process
{
 char name;
 double BT;

}p[5];
int turn=0;
void startnext(int tid)
{
 if(tid==1)
  return;
 tid++;
}
void run(void *tid)
{
 int i=(int )tid;
 while(turn!=i)
 {
  sleep(p[i].BT);
  p[i].BT=0;
  startnext(i);
  pthread_exit(0);
 }
}


int main(int argc,char *argv[])
{
 pthread_t thrs[5];
 int i,j;
 if(argc==6)
 {
  for(i=0;i<5;i++)
  {
   p[i].name=65+i;
   p[i].BT=atoi(argv[i+1]);
  }
 }
 for (int i = 0; i < 5; i++)
 {
  pthread_create(&thrs[i],NULL,&run,(void *)i);
  pthread_join(thrs[i],NULL);
 }
 return 0;
}

Tuesday, September 1, 2015

Add all greater values to every node in a given BST

              50
           /      \
         30        70
        /   \      /  \
     20    40    60   80 

The above tree should be modified to following 

              260
           /      \
         330        150
        /   \       /  \
      350   300    210   80


#include <iostream>
using namespace std;
struct node {
int data;
node *left;
node *right;
};

void addgreater(node *&t,int &sum)
{
if(t==NULL)return ;
if(t)
{
addgreater(t->right,sum);
sum=sum+t->data;
t->data=sum;
addgreater(t->left,sum);
}
}
void create(node *&t,int a)
{
if(t==NULL)
{
t=new node;
t->data=a;
t->left=t->right=NULL;

}
else if(a<t->data)
create(t->left,a);
else
create(t->right,a);

}
void print(node *t)
{
if(t)
{
cout<<t->data<<" ";
print(t->left);
print(t->right);
}
}
int main()
{
int a;
node *t=NULL;

cin>>a;
while(a!=-1){
create(t,a);
cin>>a;
}
print(t);
a=0;
addgreater(t,a);
print(t);

}

Contributors

Translate