RSS

Insertion/Deletion in B tree

Program of insertion and deletion in B tree

 

#include
#include
#define M 5

struct node{
int n; /* n < M No. of keys in node will always less than order of B
tree */
int keys[M-1]; /*array of keys*/
struct node *p[M]; /* (n+1 pointers will be in use) */
}*root=NULL;

enum KeyStatus { Duplicate,SearchFailure,Success,InsertIt,LessKeys };

void insert(int key);
void display(struct node *root,int);
void DelNode(int x);
void search(int x);
enum KeyStatus ins(struct node *r, int x, int* y, struct node** u);
int searchPos(int x,int *key_arr, int n);
enum KeyStatus del(struct node *r, int x);

int main()
{
int key;
int choice;
printf(“Creation of B tree for node %d\n”,M);
while(1)
{
printf(“1.Insert\n”);
printf(“2.Delete\n”);
printf(“3.Search\n”);
printf(“4.Display\n”);
printf(“5.Quit\n”);
printf(“Enter your choice : “);
scanf(“%d”,&choice);

switch(choice)
{
case 1:
printf(“Enter the key : “);
scanf(“%d”,&key);
insert(key);
break;
case 2:
printf(“Enter the key : “);
scanf(“%d”,&key);
DelNode(key);
break;
case 3:
printf(“Enter the key : “);
scanf(“%d”,&key);
search(key);
break;
case 4:
printf(“Btree is :\n”);
display(root,0);
break;
case 5:
exit(1);
default:
printf(“Wrong choice\n”);
break;
}/*End of switch*/
}/*End of while*/
return 0;
}/*End of main()*/

void insert(int key)
{
struct node *newnode;
int upKey;
enum KeyStatus value;
value = ins(root, key, &upKey, &newnode);
if (value == Duplicate)
printf(“Key already available\n”);
if (value == InsertIt)
{
struct node *uproot = root;
root=malloc(sizeof(struct node));
root->n = 1;
root->keys[0] = upKey;
root->p[0] = uproot;
root->p[1] = newnode;
}/*End of if */
}/*End of insert()*/

enum KeyStatus ins(struct node *ptr, int key, int *upKey,struct node
**newnode)
{
struct node *newPtr, *lastPtr;
int pos, i, n,splitPos;
int newKey, lastKey;
enum KeyStatus value;
if (ptr == NULL)
{
*newnode = NULL;
*upKey = key;
return InsertIt;
}
n = ptr->n;
pos = searchPos(key, ptr->keys, n);
if (pos < n && key == ptr->keys[pos])
return Duplicate;
value = ins(ptr->p[pos], key, &newKey, &newPtr);
if (value != InsertIt)
return value;
/*If keys in node is less than M-1 where M is order of B tree*/
if (n < M – 1)
{
pos = searchPos(newKey, ptr->keys, n);
/*Shifting the key and pointer right for inserting the new key*/
for (i=n; i>pos; i–)
{
ptr->keys[i] = ptr->keys[i-1];
ptr->p[i+1] = ptr->p[i];
}
/*Key is inserted at exact location*/
ptr->keys[pos] = newKey;
ptr->p[pos+1] = newPtr;
++ptr->n; /*incrementing the number of keys in node*/
return Success;
}/*End of if */
/*If keys in nodes are maximum and position of node to be inserted is
last*/
if (pos == M – 1)
{
lastKey = newKey;
lastPtr = newPtr;
}
else /*If keys in node are maximum and position of node to be inserted
is not last*/
{
lastKey = ptr->keys[M-2];
lastPtr = ptr->p[M-1];
for (i=M-2; i>pos; i–)
{
ptr->keys[i] = ptr->keys[i-1];
ptr->p[i+1] = ptr->p[i];
}
ptr->keys[pos] = newKey;
ptr->p[pos+1] = newPtr;
}
splitPos = (M – 1)/2;
(*upKey) = ptr->keys[splitPos];

(*newnode)=malloc(sizeof(struct node));/*Right node after split*/
ptr->n = splitPos; /*No. of keys for left splitted node*/
(*newnode)->n = M-1-splitPos;/*No. of keys for right splitted node*/
for (i=0; i < (*newnode)->n; i++)
{
(*newnode)->p[i] = ptr->p[i + splitPos + 1];
if(i < (*newnode)->n – 1)
(*newnode)->keys[i] = ptr->keys[i + splitPos + 1];
else
(*newnode)->keys[i] = lastKey;
}
(*newnode)->p[(*newnode)->n] = lastPtr;
return InsertIt;
}/*End of ins()*/

void display(struct node *ptr, int blanks)
{
if (ptr)
{
int i;
for(i=1;i<=blanks;i++)
printf(” “);
for (i=0; i < ptr->n; i++)
printf(“%d “,ptr->keys[i]);
printf(“\n”);
for (i=0; i <= ptr->n; i++)
display(ptr->p[i], blanks+10);
}/*End of if*/
}/*End of display()*/

void search(int key)
{
int pos, i, n;
struct node *ptr = root;
printf(“Search path:\n”);
while (ptr)
{
n = ptr->n;
for (i=0; i < ptr->n; i++)
printf(” %d”,ptr->keys[i]);
printf(“\n”);
pos = searchPos(key, ptr->keys, n);
if (pos < n && key == ptr->keys[pos])
{
printf(“Key %d found in position %d of last dispalyed
node\n”,key,i);
return;
}
ptr = ptr->p[pos];
}
printf(“Key %d is not available\n”,key);
}/*End of search()*/

int searchPos(int key, int *key_arr, int n)
{
int pos=0;
while (pos < n && key > key_arr[pos])
pos++;
return pos;
}/*End of searchPos()*/

void DelNode(int key)
{
struct node *uproot;
enum KeyStatus value;
value = del(root,key);
switch (value)
{
case SearchFailure:
printf(“Key %d is not available\n”,key);
break;
case LessKeys:
uproot = root;
root = root->p[0];
free(uproot);
break;
}/*End of switch*/
}/*End of delnode()*/

enum KeyStatus del(struct node *ptr, int key)
{
int pos, i, pivot, n ,min;
int *key_arr;
enum KeyStatus value;
struct node **p,*lptr,*rptr;

if (ptr == NULL)
return SearchFailure;
/*Assigns values of node*/
n=ptr->n;
key_arr = ptr->keys;
p = ptr->p;
min = (M – 1)/2;/*Minimum number of keys*/

pos = searchPos(key, key_arr, n);
if (p[0] == NULL)
{
if (pos == n || key < key_arr[pos])
return SearchFailure;
/*Shift keys and pointers left*/
for (i=pos+1; i < n; i++)
{
key_arr[i-1] = key_arr[i];
p[i] = p[i+1];
}
return –ptr->n >= (ptr==root ? 1 : min) ? Success : LessKeys;
}/*End of if */

if (pos < n && key == key_arr[pos])
{
struct node *qp = p[pos], *qp1;
int nkey;
while(1)
{
nkey = qp->n;
qp1 = qp->p[nkey];
if (qp1 == NULL)
break;
qp = qp1;
}/*End of while*/
key_arr[pos] = qp->keys[nkey-1];
qp->keys[nkey – 1] = key;
}/*End of if */
value = del(p[pos], key);
if (value != LessKeys)
return value;

if (pos > 0 && p[pos-1]->n > min)
{
pivot = pos – 1; /*pivot for left and right node*/
lptr = p[pivot];
rptr = p[pos];
/*Assigns values for right node*/
rptr->p[rptr->n + 1] = rptr->p[rptr->n];
for (i=rptr->n; i>0; i–)
{
rptr->keys[i] = rptr->keys[i-1];
rptr->p[i] = rptr->p[i-1];
}
rptr->n++;
rptr->keys[0] = key_arr[pivot];
rptr->p[0] = lptr->p[lptr->n];
key_arr[pivot] = lptr->keys[–lptr->n];
return Success;
}/*End of if */
if (posn > min)
{
pivot = pos; /*pivot for left and right node*/
lptr = p[pivot];
rptr = p[pivot+1];
/*Assigns values for left node*/
lptr->keys[lptr->n] = key_arr[pivot];
lptr->p[lptr->n + 1] = rptr->p[0];
key_arr[pivot] = rptr->keys[0];
lptr->n++;
rptr->n–;
for (i=0; i < rptr->n; i++)
{
rptr->keys[i] = rptr->keys[i+1];
rptr->p[i] = rptr->p[i+1];
}/*End of for*/
rptr->p[rptr->n] = rptr->p[rptr->n + 1];
return Success;
}/*End of if */

if(pos == n)
pivot = pos-1;
else
pivot = pos;

lptr = p[pivot];
rptr = p[pivot+1];
/*merge right node with left node*/
lptr->keys[lptr->n] = key_arr[pivot];
lptr->p[lptr->n + 1] = rptr->p[0];
for (i=0; i < rptr->n; i++)
{
lptr->keys[lptr->n + 1 + i] = rptr->keys[i];
lptr->p[lptr->n + 2 + i] = rptr->p[i+1];
}
lptr->n = lptr->n + rptr->n +1;
free(rptr); /*Remove right node*/
for (i=pos+1; i < n; i++)
{
key_arr[i-1] = key_arr[i];
p[i] = p[i+1];
}
return –ptr->n >= (ptr == root ? 1 : min) ? Success : LessKeys;
}/*End of del()*/

 

 

 
Leave a comment

Posted by on March 8, 2011 in C/C++

 

Min Heap

Min Heap C program

#include<iostream.h>
#include<conio.h>
int heapsize;
void minheap(int*,int);
void insert(int*,int);
int extract_min(int*);
void decrease_key(int*,int,int);
void decreasekey(int*,int,int);
void main()
{
int i,start,A[10],min,position,key;
cout<<“Enter the number to be inserted\n”;
cin>>heapsize;
cout<<” enter the numbers\n”;
for(i=1;i<=heapsize;i++)
{
cin>>A[i];
}
start=heapsize/2;
while(start>0)
{
minheap(A,start);
start–;
}
cout<<“\nThe  minheap is:-\n”;
for(i=1;i<=heapsize;i++)
{
cout<<A[i]<<“\n”;
}

cout<<“\nThe extract min operation:-\n”;
min=extract_min(A);
cout<<“The minimum value of heap is : “<<min<<“\n\n”;
cout<<“The resulting minheap is:-\n”;
for(i=1;i<=heapsize;i++)
{
cout<<A[i]<<“\n”;
}

cout<<“\nThe decrease key operation:-\n”;
cout<<“Enter any key with its position\n”;
cin>>key>>position;
decrease_key(A,position,key);
cout<<“\nThe resulting minheap is:-\n”;
for(i=1;i<=heapsize;i++)
{
cout<<A[i]<<“\n”;
}

cout<<“\nThe insert key operation:-\n”;
cout<<“Enter the key u want 2 insert\n”;
cin>>key;
insert(A,key);
cout<<“\nThe resulting minheap is:-\n”;
for(i=1;i<=heapsize;i++)
{
cout<<A[i]<<“\n”;
}
}

void minheap(int*A,int i)
{
int lc,rc,minimum,num;
lc=(2*i);
rc=lc+1;
if((lc<=heapsize)&&(A[lc]<A[i]))
minimum=lc;
else
minimum=i;
if((rc<=heapsize)&&(A[rc]<A[minimum]))
minimum=rc;
if(minimum!=i)
{
num=A[i];
A[i]=A[minimum];
A[minimum]=num;
minheap(A,minimum);
}}

int extract_min(int*A)
{
int min;
min=A[1];
A[1]=A[heapsize];
heapsize–;
minheap(A,1);
return(min);
}

void decrease_key(int*A,int i,int key)
{
int parent,num;
parent=i/2;
if(A[i]<key)
cout<<“Cannot increase key as already smaller\n”;
else
A[i]=key;
while((i>1)&&(A[parent]>A[i]))
{
num=A[i];
A[i]=A[parent];
A[parent]=num;
i=parent;
parent=i/2;
}
}

void decreasekey(int*A,int i,int key)
{
int parent,num;
parent=i/2;
A[i]=key;
while((i>1)&&(A[parent]>A[i]))
{
num=A[i];
A[i]=A[parent];
A[parent]=num;
i=parent;
parent=i/2;
}
}

void insert(int*A,int key)
{
heapsize++;
A[heapsize]=-1000;
decreasekey(A,heapsize,key);
}
 
Leave a comment

Posted by on March 8, 2011 in C/C++

 

Insertion/Deletion in a Queue Data Structure

Insertion/Deletion in a Queue Data Structure in C

#include<stdio.h>
#include<conio.h>
#include<process.h>
int queue[5];
long front,rear;
void display();
void main()
{
int choice,info;
clrscr();
//initilisin queue
int tqueue();
while(1)
{
clrscr();
//displaying menu
printf(“MENU \n”);
printf(“1: insert an element in queue\n”);
printf(“2: delete an element from queue\n”);
printf(“3: display the queue\n”);
printf(“exit!\n”);
printf(“your choice:”);
scanf(“%i”,&choice);
switch(choice)
{
case 1: if(rear<4)
{
printf(“enter the no”);
scanf(“%d”,&info);
if(front==-1)
{
front=0;
rear=0;
}
else
rear=rear+1;
queue[rear]=info;
}
else
printf(“queue is full”);
break;
case 2:int info;
if(front!=-1)
{
info=queue[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else
front=front+1;
printf(“no deleted is =%d”,info);
}
else
printf(“queue is empty”);
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf(“you entered wrong choice!”);
break;
}
}
}
void initqueue()
{
//initilising front & rear to -1
front=rear=-1;
}
/*displays the current position of the queue*/
void display()
{
int i;
//displaying elements in queue
for(i=front;i<=rear;i++)
printf(“%i\n”,queue[i]);
getch();
}

 

 
Leave a comment

Posted by on March 8, 2011 in C/C++

 

Treaps

Treaps are binary search trees in which each node has both a key and a priority.
Here’s the implementation in C:

#include<stdio.h>

#include<iostream.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct btreenode
{
int data;
int priority;
struct btreenode *leftchild;
struct btreenode *rightchild;
};
struct btreenode *bt;
void insert(struct btreenode**,int,int);
void inorder(struct btreenode*);
void show(struct btreenode*);
void insertfunc();
void deletefunc();
void searchfunc();
void search(struct btreenode*,int);
void del(struct btreenode**,int);
void main()
{
int choice;
bt=NULL; //empty tree
char ch;
do
{
printf(“What operation do you wish to perform on the tree\n”);
printf(“1. Insertion\n”);
printf(“2. Deletion\n”);
printf(“3. Searching\n”);
printf(“4. Show elements\n”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
insertfunc();
break;
case 2:
deletefunc();
break;
case 3:
searchfunc();
break;
case 4:
printf(“Elements are:\n”);
show(bt);
break;
}
cout<<“\nDo you want to continue (y or n)\n”;
cin>>ch;
}while(ch==’y’||ch==’Y’);
}
void inorder(struct btreenode *sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
inorder(sr->rightchild);
}
else
return;
}
void insertfunc()
{
int req,i=1,num,num2;
printf(“Enter the no. of items to be inserted\n”);
scanf(“%d”,&req);
while(i++<=req)
{
printf(“Enter the data\n”);
scanf(“%d”,&num);
printf(“Enter the priority\n”);
scanf(“%d”,&num2);


insert(&bt,num,num2);
}
}
void deletefunc()
{
int num;
printf(“Enter the element to be deleted\n”);
scanf(“%d”,&num);
del(&bt,num);
}
void searchfunc()
{
int num;
printf(“Enter the element to be searched\n”);
scanf(“%d”,&num);
search(bt,num);
}
void insert(struct btreenode **sr,int num,int num2)
{
if(*sr==NULL)
{
*sr=(btreenode*)malloc(sizeof(struct btreenode));
(*sr)->leftchild=NULL;
(*sr)->data=num;
(*sr)->priority=num2;
(*sr)->rightchild=NULL;
}
else
{
if(num<(*sr)->data)
insert(&((*sr)->leftchild),num,num2);
else
insert(&((*sr)->rightchild),num,num2);
}
return;
}
void search(struct  btreenode *sr,int num)
{
if(sr!=NULL)
{
search(sr->leftchild,num);
if(sr->data==num)
{
cout<<“Element found”<<endl;
return;
}
search(sr->rightchild,num);
}
else
{
return;
}
}
void del(struct btreenode **sr,int num)
{
if(*sr!=NULL)
{
del(&((*sr)->leftchild),num);
if((*sr)->data==num)
{
if((*sr)->leftchild!=NULL)
{
(*sr)=(*sr)->leftchild;
free((*sr)->leftchild);
return;
}
else
if((*sr)->rightchild!=NULL)
{
(*sr)=(*sr)->rightchild;
free(&((*sr)->rightchild));
return;
}
else
{
free(&((*sr)));
}
}
del(&((*sr)->rightchild),num);
}
}
void show(struct btreenode *sr)
{
if(sr!=NULL)
{
show(sr->leftchild);
printf(“\t%d”,sr->data);
printf(“\t%d\n”,sr->priority);
show(sr->rightchild);
}
else
return;

}

 
Leave a comment

Posted by on March 8, 2011 in C/C++

 

Tree Traversal

C Program for traversing a tree in Preorder, Postorder and Inorder:

 

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
{
int num;
struct node *left;
struct node *right;
};
typedef struct node node;
node *root=NULL;
node *insert(struct node *tree, long num);
void preorder(node *tree);
int select();
void inorder(node *tree);
void postorder(node *tree);
int count=1;
void main()
{
int choice;
long digit;
do
{
choice=select();
switch(choice)
{
case 1:puts(“enter integer : to quit enter 0”);
if(digit!=0)
{
scanf(“%d”,&digit);
root=insert(root,digit);
}
continue;
case 2:puts(“\n preorder traversing tree”);
preorder(root);
continue;
case 3:puts(“\n inorder traversing tree”);
inorder(root);
continue;
case 4:puts(“\n postorder traversing tree”);
postorder(root);
continue;
case 5:puts(“end”);
exit(0);
}
}
while(choice !=5);
}
int select()
{
int selection;
do
{
puts(“\n enter 1:insert a node in the BT”);
puts(“\n enter 2:display(preorder) the BT”);
puts(“\n enter 3:display(inorder) the BT”);
puts(“\n enter 4:display(postorder) the BT”);
puts(“enter your choice”);
scanf(“%d”,&selection);
if((selection<1)||(selection>5))
{
puts(“wrong choice:try again”);
getch();
}
}
while((selection<1)||(selection>5));
return(selection);
}
node *insert(node *p,long digit)
{
if(p==NULL)
{
p=(node *)malloc(sizeof(node));
p->left=p->right=NULL;
p->num=digit;
count++;
}
else
if(count %2==0)
p->left=insert(p->left,digit);
else
p->right=insert(p->right,digit);
return(p);
}
void preorder(node *p)
{
if(p!=NULL)
{
printf(“%d\n”,p->num);
preorder(p->left);
preorder(p->right);
}
}
void inorder(node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf(“%d\n”,p->num);
inorder(p->right);
}
}
void postorder(node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf(“\n%d”,p->num);
}
}
 
Leave a comment

Posted by on March 8, 2011 in C/C++

 

Convert Infix Expression to Postfix form

The following C program converts infix expression to postfix expression. Note that the input infix expression must have opening and closing parenthesis enclosing each operation.
For eg. for converting a+b/c*d to postfix form give the input as ((a+b)/(c*d))

#include<stdio.h>
#include<string.h>
void main()
{
char s[100],str[100],oparr[100];
int l,i,j=0,top=0;
printf(“Enter a string in INFIX\n”);
scanf(“%s”,&s);
l=strlen(s);
for(i=0;i<l;i++)
{if(s[i]==’+’||s[i]==’-‘||s[i]==’*’||s[i]==’/’||s[i]==’%’)
{oparr[j++]=s[i];}
if(s[i]!='(‘&& s[i]!=’+’ && s[i]!=’-‘ && s[i]!=’*’ && s[i]!=’/’ && s[i]!=’%’)
{if(s[i]==’)’)
printf(“%c”,oparr[–j]);
else
printf(“%c”,s[i]);}}
}
 
Leave a comment

Posted by on March 8, 2011 in C/C++

 

Create your own UNIX Shell

C program for creating your own UNIX Shell:- 🙂

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<wait.h>
#define BUF 200
#define ARGV 40
#define DELIM “\n\t\r “
int main(int argc, char **argv){
int i,n;
char buf[BUF+1];
char *clargs[ARGV];
int returnval;
for(;;)
{
n=1;
for(i=0;i<ARGV;i++)
clargs[i]=””;
for(i=0;i<=BUF;i++)
buf[i]=”;
write(STDOUT_FILENO, “Shell> “, 7);
/*2nd ARGUMENT IS THE NAME OF YOUR SHELL AND THE 3rd ARGUMENT IS THE LENGTH OF THE NAME*/
read(STDIN_FILENO, buf, BUF);
printf(“BUF = %s”,buf);
if(!strcmp(buf, “exit\n”))
exit(0);
clargs[0]=strtok(buf, DELIM);
while(clargs[n]=strtok(NULL,DELIM))
n++;
switch(fork())
{
case 0:if((execvp(clargs[0], &clargs[0]))<0)
exit(200);
default:wait(&returnval);
printf(“Exit status of command: %d\n”, WEXITSTATUS(returnval));
}
}
}
 
Leave a comment

Posted by on March 8, 2011 in Unix/Linux

 

Implementation of certain UNIX Commands

There are the following programs in this post:
  • Cat command
  • Copy command
  • ls|sort
  • wc
  • wc file1 file2
  • ls|sort|wc
“cat” command:- Used to write/create a file(cat>file1) and to display its contents(cat file1)
#include<stdio.h>
#include<fcntl.h>
main(int argc, char** argv)
{
int i,n,fd1,fd2;
char buff[1024];
if(argc==1)
{
fd1=open(“newfile”,O_CREAT|O_WRONLY,0644);
while((n=read(1,buff,1024))>0)
{
write(fd1,buff,n);
}
close(fd1);
}
else
for(i=1;i<=argc;i++)
{
fd1=open(argv[i],O_RDONLY);
while((n=read(fd1,buff,1024))>0)
{
write(1,buff,n);
}
close(fd1);
}
“copy” command:- Used to copy contents of one file to another (cp file1 file 2)
#include<stdio.h>
#include<fcntl.h>
main(int argc, char **argv)
{
int n,fd1,fd2;
char buff[1024];
fd1=open(argv[1],O_RDONLY);
fd2=open(argv[2],O_CREAT|O_WRONLY,0644);
while((n=read(fd1,buff,1024))>0)
{
write(fd2,buff,n);
}
close(fd1);
close(fd2);
}
“ls|sort” command:- Used to list all files and directories in sorted order
#include<stdio.h>
main()
{
int pfd[2];
pipe(pfd);
int id=fork();
if(id==0)
{
close(1);
dup(pfd[1]);
close(pfd[0]);
close(pfd[1]);
execlp(“ls”,”ls”,0);
}
else
{
close(0);
dup(pfd[0]);
close(pfd[0]);
close(pfd[1]);
execlp(“sort”,”sort”,0);
}
}
“wc” command:-
#include<stdio.h>
#include<fcntl.h>
int main(int argc,char *argv[])
{
int id,fd,i;
id=fork();
if(id==0)
{
if(argc>1)
{
execvp(“wc”,argv);
exit(0);
}
}
else
wait();
return 0;
}
“wc f1 f2” command:-
#include<stdio.h>
#include<fcntl.h>
main()
{
int id,fd1;
id=fork();
if(id==0)
{
fd1=open(“f3”,O_CREAT|O_WRONLY,0644);
close(1);
dup(fd1);
execlp(“wc”,”wc”,”f1″,0);
}
else
wait();
}
“ls|sort|wc” command:-
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
int main()
{
int pfd1[2],pfd2[2],chid,chid2;
pipe(pfd1);
chid=fork();
if(chid==0)
{
pipe(pfd2);
chid2=fork();
if(chid2==0)
{
//ls process or inner child process
close(pfd2[0]);
close(1);
dup(pfd2[1]);
close(pfd2[1]);
close(pfd1[0]);
close(pfd1[1]);
execlp(“ls”,”ls”,0);
exit(0);
}
else
{
close(pfd2[1]);
close(0);
dup(pfd2[0]);
close(pfd2[0]);
printf(“the flag”);
close(1);
dup(pfd1[1]);
close(pfd1[1]);
close(pfd1[0]);
execlp(“sort”,”sort”,0);
waitpid(chid2,NULL,0);
exit(0);
}
}
else
{
close(pfd1[1]);
close(0);
dup(pfd1[0]);
close(pfd1[0]);
execlp(“wc”,”wc”,0);
waitpid(chid,NULL,0);
exit(0);
}
return 0;
}
 
Leave a comment

Posted by on March 7, 2011 in Unix/Linux

 

Hiding a message or other data in an image

1. Compress the data file u want to hide in a zip file.
2. Open MS-DOS prompt
3. Move to the directory that contains the .rar file and the image you wish to hide the text in.
4. Type a command similar to the below command.

copy /b image_name.jpg + compressed_file_name.rar

5. The process of hiding data is completed

6. Now to get back the file, rename the image file and change its extension to .zip
7. Extract the data file and u r done

Notice the size difference between the normal image and the image containing data.
Hope u like it pls comment if u hav any queries!!!!

This how Hacker send their secret message to another.

 
Leave a comment

Posted by on March 7, 2011 in INFO

 

C# Web Browser with Popup Blocker

C# Web Browser with pop-up blocker:
1). Create a new C# windows application project in Visual Studio.
2). Resize the form to 800×500 px.
3). Drag the following controls from the toolbox(press Ctrl+Alt+X to show toolbox):
a). 1 Buttons (name it as buttonGo)
b). 1 Text Boxes (name it as buttonGo)
c). 1 Microsoft Web Browser
NOTE: We’ll use the activeX web browser control and not the web browser that is directly visible in the toolbox. To get this active web browser control right click on toolbox, select “Choose Toobox Items”, goto COM Components tab, select Microsoft Web Browser and click OK.

5). Arrange all the controls to give your form the following look:

6). Button “Go” is for navigating to the address in the text box on the left to it.
7). Replace “Afact” everywhere in the code below with the name you gave to your application.

8). Write the following code to buttonGo events and NewWindow2 event of the web browser control:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Afact
{
public partial class Afact : Form
{

public Afact()
{
InitializeComponent();
}

private void Afact_Load(object sender, EventArgs e)
{
axWebBrowser1.Navigate(“www.google.com”);

}

private void buttonGo_Click(object sender, EventArgs e)
{
axWebBrowser1.Navigate(textBoxURL.Text);
}

private void axWebBrowser1_NewWindow2(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
{
// Is document ready state complete ? If not, that’s an ad popup. Don’t allow it
if (axWebBrowser1.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
{
e.ppDisp = null;
e.cancel = true;
return;
}

/* Else let’s create a new form instance and
send the new web browser instance back to the caller.*/
Afact newwindow = new Afact();
newwindow.Text = “Afact New Window”;
e.ppDisp = newwindow.axWebBrowser1.Application;
newwindow.Show();
}

}
}

 

Download the project from the BOX on the right side!!!

 

 
Leave a comment

Posted by on March 7, 2011 in C#