#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void search();
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n\t1----INSERT A NODE IN A BINARY TREE.";
cout<<"\n\t2----SEARCH.";
cout<<"\n\t3----EXIT.";
cout<<"\n\tENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\n\tENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
search();
getch();
break;
case '3':
exit(0);
break;
}
} while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
elseif(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
int t=0;
void src(node *tree,int);
void search()
{
int sr;
cout<<"Enter data to search\n";
cin>>sr;
src(tree,sr);
if(t==1)
cout<<"Element found";
else
cout<<"Not Found";
t=0;
}
void src(node *tree,int sr)
{
if(tree!=NULL)
{
if(tree->data==sr)
{
t=1;
}
src(tree->left,sr);
src(tree->right,sr);
}
}
Saturday, November 13, 2010
Search in linked list
#include"iostream.h"
#include"conio.h"
class tree
{
public:
int num,l,c;
struct node
{
int info;
node *next;
}*head,*nn,*temp,*p;
tree()
{
head=NULL;
c=0;
}
void allo()
{
nn=new node;
cout<<"\n\t\tENTER THE NUMBER TO INSERT =";
cin>>num;
nn->info=num;
nn->next=NULL;
}
void create()
{
allo();
if(head==NULL)
{
head=nn;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=nn;
}
}
void search()
{
int f=0;
if(head==NULL)
{
cout<<"\n\t\tLIST IS EMPTY";
}
else
{
cout<<"\n\t\tENTER THE NUMBER TO SEARCH =";
cin>>num;
temp=head;
do
{
if(temp->info==num)
{
f=1;
goto k;
}
else
{
temp=temp->next;
}
}while(temp->next!=NULL);
}
if(temp->info==num)
{
f=1;
}
k:
if(f==1)
{
cout<<"\n\t\tTHE GIVEN ITEM IS FOUND";
}
else
{
cout<<"\n\t\tTHE GIVEN ITEM IS NOT FOUND";
}
}
};
void main()
{
clrscr();
int ch;
tree t;
do
{
cout<<"\n\t\t\tSINGLE LISTED OPERATION";
cout<<"\n\t\t\t~~~~~~ ~~~~~~ ~~~~~~~~~";
cout<<"\n\t\tOPTION";
cout<<"\n\t\t1-CREATING";
cout<<"\n\t\t2-SEARCH";
cout<<"\n\t\t3-EXIT";
cout<<"\n\t\tENTER YOUR CHOICE =";
cin>>ch;
switch(ch)
{
case 1:
t.create();
break;
case 2:
t.search();
break;
}
}while(ch<3);
getch();
}
#include"conio.h"
class tree
{
public:
int num,l,c;
struct node
{
int info;
node *next;
}*head,*nn,*temp,*p;
tree()
{
head=NULL;
c=0;
}
void allo()
{
nn=new node;
cout<<"\n\t\tENTER THE NUMBER TO INSERT =";
cin>>num;
nn->info=num;
nn->next=NULL;
}
void create()
{
allo();
if(head==NULL)
{
head=nn;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=nn;
}
}
void search()
{
int f=0;
if(head==NULL)
{
cout<<"\n\t\tLIST IS EMPTY";
}
else
{
cout<<"\n\t\tENTER THE NUMBER TO SEARCH =";
cin>>num;
temp=head;
do
{
if(temp->info==num)
{
f=1;
goto k;
}
else
{
temp=temp->next;
}
}while(temp->next!=NULL);
}
if(temp->info==num)
{
f=1;
}
k:
if(f==1)
{
cout<<"\n\t\tTHE GIVEN ITEM IS FOUND";
}
else
{
cout<<"\n\t\tTHE GIVEN ITEM IS NOT FOUND";
}
}
};
void main()
{
clrscr();
int ch;
tree t;
do
{
cout<<"\n\t\t\tSINGLE LISTED OPERATION";
cout<<"\n\t\t\t~~~~~~ ~~~~~~ ~~~~~~~~~";
cout<<"\n\t\tOPTION";
cout<<"\n\t\t1-CREATING";
cout<<"\n\t\t2-SEARCH";
cout<<"\n\t\t3-EXIT";
cout<<"\n\t\tENTER YOUR CHOICE =";
cin>>ch;
switch(ch)
{
case 1:
t.create();
break;
case 2:
t.search();
break;
}
}while(ch<3);
getch();
}
Queue Operation
#include<conio.h>
#include<iostream.h>
#include<process.h>
# define MAX_SIZE 150
class queues
{
int front,rear;
int queue[MAX_SIZE];
public:
queues() // Constructor
{
front=(-1);
rear=(-1);
}
void insert_rear(int);
void delete_front();
void display();
};
void queues::insert_rear(int item)
{
if((front==0 && rear==MAX_SIZE) || (front==(rear+1)))
{
cout<<"Queue is full!\nOverflow";
getch();
exit(0);
}
else
{
if(front==(-1) && rear==(-1))
{
rear=0;
front=0;
}
else if(front!=0 && rear==MAX_SIZE)
rear=0;
else
rear=rear+1;
queue[rear]=item;
}
}
void queues::delete_front()
{
if(front==(-1) && rear==(-1))
{
cout<<"Queue is empty!\nUnderflow";
return;
}
else
{
if(front==rear)
front=rear=(-1);
else if(front==MAX_SIZE && rear==MAX_SIZE)
front=0;
else
front=front+1;
}
cout<<"\nItem deleted.";
getch();
}
void queues:: display()
{
int ptr;
if(front==(-1) && rear==(-1))
{
cout<<"\nQueue is empty";
getch();
}
/*if(front<=MAX_SIZE && rear<=front)
{
for(ptr=front;ptr<MAX_SIZE;ptr++)
cout<<queue[ptr]<<"\n";
for(ptr=0;ptr<=rear;ptr++)
cout<<queue[ptr]<<"\n";
} */
else
{
cout<<"\nThe queue is\n";
for(ptr=front;ptr<=rear;ptr++)
cout<<queue[ptr]<<" ";
}
}
int main()
{
clrscr();
int length,i,element,choice;
queues q1;
while(1)
{
clrscr();
cout<<"1: Insert an item.\n2: Delete an item.";
cout<<"\n3: Display elements\n4: Exit";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"How many elements are in the queue: ";
cin>>length;
cout<<"Enter "<<length<<" elements: ";
for(i=0;i<length;i++)
{
cin>>element;
q1.insert_rear(element);
}
q1.display();
getch();
break;
case 2:
q1.delete_front();
q1.display();
getch();
break;
case 3:
q1.display();
getch();
break;
case 4:
exit(0);
break;
default:
cout<<"Please re-enter tour choice.";
getch();
break;
}
}
getch();
}
#include<iostream.h>
#include<process.h>
# define MAX_SIZE 150
class queues
{
int front,rear;
int queue[MAX_SIZE];
public:
queues() // Constructor
{
front=(-1);
rear=(-1);
}
void insert_rear(int);
void delete_front();
void display();
};
void queues::insert_rear(int item)
{
if((front==0 && rear==MAX_SIZE) || (front==(rear+1)))
{
cout<<"Queue is full!\nOverflow";
getch();
exit(0);
}
else
{
if(front==(-1) && rear==(-1))
{
rear=0;
front=0;
}
else if(front!=0 && rear==MAX_SIZE)
rear=0;
else
rear=rear+1;
queue[rear]=item;
}
}
void queues::delete_front()
{
if(front==(-1) && rear==(-1))
{
cout<<"Queue is empty!\nUnderflow";
return;
}
else
{
if(front==rear)
front=rear=(-1);
else if(front==MAX_SIZE && rear==MAX_SIZE)
front=0;
else
front=front+1;
}
cout<<"\nItem deleted.";
getch();
}
void queues:: display()
{
int ptr;
if(front==(-1) && rear==(-1))
{
cout<<"\nQueue is empty";
getch();
}
/*if(front<=MAX_SIZE && rear<=front)
{
for(ptr=front;ptr<MAX_SIZE;ptr++)
cout<<queue[ptr]<<"\n";
for(ptr=0;ptr<=rear;ptr++)
cout<<queue[ptr]<<"\n";
} */
else
{
cout<<"\nThe queue is\n";
for(ptr=front;ptr<=rear;ptr++)
cout<<queue[ptr]<<" ";
}
}
int main()
{
clrscr();
int length,i,element,choice;
queues q1;
while(1)
{
clrscr();
cout<<"1: Insert an item.\n2: Delete an item.";
cout<<"\n3: Display elements\n4: Exit";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"How many elements are in the queue: ";
cin>>length;
cout<<"Enter "<<length<<" elements: ";
for(i=0;i<length;i++)
{
cin>>element;
q1.insert_rear(element);
}
q1.display();
getch();
break;
case 2:
q1.delete_front();
q1.display();
getch();
break;
case 3:
q1.display();
getch();
break;
case 4:
exit(0);
break;
default:
cout<<"Please re-enter tour choice.";
getch();
break;
}
}
getch();
}
Subscribe to:
Posts (Atom)