Data
structure C++ Programs
//Binary search program
#include<iostream.h>
#include<conio.h>
void main()
{
int list[100],n,i,x,ll,ul,mid,flag=0;
clrscr();
cout<<"Enter the size of the list"<<endl;
cin>>n;
cout<<"Enter the list elements"<<endl;
for(i=0;i<n;i++)
cin>>list[i];
cout<<"Enter search element"<<endl;
cin>>x;
ll=0;
ul=n-1;
while(ll<=ul)
{
mid=(ll+ul)/2;
if(x==list[mid])
{
flag=1;
break;
}
else
{
if(x<list[mid])
ul=mid-1;
else
ll=mid+1;
}
}
if(flag==0)
cout<<x<<" Not Found"<<endl;
else
cout<<x<<" Found"<<endl;
getch();
}
double linked list
=========================================================================
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct node
{
int item;
struct node *left;
struct node *right;
};
typedef struct node node;
#define NULL 0
node* tree(node* root);
node* insert(node *root,int item);
node* merge(node *r1,node* r2);
void insertnode(node *root,node *temp);
void display(node *root);
void mer(node *r1,node* r2);
node* merge(node* r1,node* r2)
{
if(r1!=NULL&&r2!=NULL)
mer(r1,r2);
else
{
if(r1==NULL)
r1=r2;
}
return r1;
}
void mer(node *r1,node* r2)
{
node *temp=(node*)malloc(sizeof(node));
node *p=r2;
if(p->left!=NULL)
merge(r1,p->left);
temp->item=p->item;
temp->left=NULL;
temp->right=NULL;
insertnode(r1,temp);
if(p->right!=NULL)
merge(r1,p->right);
}
node* insert(node* root,int item)
{
node* p=root;
node* temp;
temp=(node*)malloc(sizeof(node));
temp->item=item;
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
root=temp;
else
{
if(p->item>temp->item&&p->left==NULL)
p->left=temp;
else
{
if(p->item<=temp->item&&p->right==NULL)
p->right=temp;
else
{
if(p->item<temp->item)
insertnode(root->right,temp);
else
insertnode(root->left,temp);
}
}
}
return root;
}
void insertnode(node *p,node *temp)
{
if(p->item>temp->item&&p->left==NULL)
p->left=temp;
else
{
if(p->item<=temp->item&&p->right==NULL)
p->right=temp;
else
{
if(p->item<temp->item)
insertnode(p->right,temp);
else
insertnode(p->left,temp);
}
}
}
void display(node *r)
{
node *p;
p=r;
if(p->left!=NULL)
display(p->left);
printf("%d\t",p->item);
if(p->right!=NULL)
display(p->right);
}
main()
{
node *root1=NULL;
node *root2=NULL;
node *r3=NULL;
int ch;
clrscr();
do
{
puts("\n1.fist tree\n2.second tree\n3.merging trees\n");
puts("Select tree");
scanf("%d",&ch);
switch(ch)
{
case 1:
root1=tree(root1);
break;
case 2:
root2=tree(root2);
break;
case 3:
r3=merge(root1,root2);
if(r3==NULL)
puts("Tree is empty");
else
display(r3);
break;
case 4:
exit(1);
}
}while(ch<=3);
getch();
}
node *tree(node *root)
{
int ch,item;
do
{
puts("\n1.insert\n2.display\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter item");
scanf("%d",&item);
root=insert(root,item);
break;
case 2:
if(root==NULL)
puts("Tree is empty");
else
display(root);
break;
}
}while(ch<3);
return root;
}
MERGE SORT
=========================================================================
#include<iostream.h>
#include<conio.h>
void sort(int *list,int n)
{
int i,j,k,temp;
for(i=1;i<n;i++)
{
for(j=0;j<i;j++)
{
if(list[j]>list[i])
{
temp=list[i];
for(k=i;k>j;k--)
{
list[k]=list[k-1];
}
list[j]=temp;
}
}
}
}
void merge(int *list1,int *list2,int *list3,int n1,int n2)
{
int i=0,j=0,k=0;
while(i<n1&&j<n2)
{
if(list1[i]<list2[j])
list3[k++]=list1[i++];
else
list3[k++]=list2[j++];
}
while(i<n1)
list3[k++]=list1[i++];
while(j<n2)
list3[k++]=list2[j++];
}
void main()
{
int list1[100],list2[100],list3[100],n1,n2,n3,i,j;
clrscr();
cout<<"Enter the two list sizes (n1 and n2)"<<endl;
cin>>n1>>n2;
cout<<"Enter the first list elements"<<endl;
for(i=0;i<n1;i++)
cin>>list1[i];
cout<<"Enter the second list elements"<<endl;
for(i=0;i<n2;i++)
cin>>list2[i];
sort(list1,n1);
sort(list2,n2);
merge(list1,list2,list3,n1,n2);
cout<<"\nResult List elements after sorting"<<endl;
n3=n1+n2;
for(i=0;i<n3;i++)
cout<<"\t"<<list3[i];
getch();
}
//Bubble ssort program
=========================================================================
#include<iostream.h>
#include<conio.h>
void main()
{
int list[100],n,i,j,temp;
clrscr();
cout<<"Enter the size of the list"<<endl;
cin>>n;
cout<<"Enter the list elements"<<endl;
for(i=0;i<n;i++)
cin>>list[i];
cout<<"The list elements before sorting"<<endl;
for(i=0;i<n;i++)
cout<<"\t"<<list[i];
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i;j++)
{
if(list[j]>list[j+1])
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
}
cout<<"\n\nThe list elements after sorting"<<endl;
for(i=0;i<n;i++)
cout<<"\t"<<list[i];
getch();
}
SELECTION SORT
=========================================================================
#include<iostream.h>
#include<conio.h>
void main()
{
int list[100],n,i,j,temp;
clrscr();
cout<<"Enter the n values"<<endl;
cin>>n;
cout<<"Enter the list elements"<<endl;
for(i=0;i<n;i++)
cin>>list[i];
cout<<"List elements before sorting"<<endl;
for(i=0;i<n;i++)
cout<<"\t"<<list[i];
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(list[i]>list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
cout<<"\nList elements after sorting"<<endl;
for(i=0;i<n;i++)
cout<<"\t"<<list[i];
getch();
}
quick sort
========================================================================
#include<iostream.h>
#include<conio.h>
void quick(int *a,int m,int n)
{
int i,j,k,temp;
if(m<n)
{
i=m;j=n+1;
k=a[m];
do
{
do
{
i++;
}while(a[i]<k&&i<=n);
do
{
j--;
}while(a[j]>k&&j>=m);
if(i<j)
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}while(i<j);
temp=a[m];
a[m]=a[j];
a[j]=temp;
quick(a,m,j-1);
quick(a,j+1,n);
}
}
void main()
{
int list[100],i,n;
clrscr();
cout<<"Enter the list of values"<<endl;
cin>>n;
cout<<"Enter the list elements"<<endl;
for(i=0;i<n;i++)
cin>>list[i];
quick(list,0,n-1);
cout<<"The list elements are"<<endl;
for(i=0;i<n;i++)
cout<<list[i]<<"\t";
getch();
}
Stack wit Array
==========================================================================
#include<iostream.h>
#include<conio.h>
class stack
{
int list[10];
int top;
public:
stack();
void push(int item);
int pop();
void display();
};
void stack::push(int item)
{
if(top>9)
{
cout<<"stack is full"<<endl;
}
else
{
list[++top]=item;
}
}
int stack::pop()
{
int item=-1;
if(top==-1)
cout<<"stack is empty"<<endl;
else
item=list[top--];
return item;
}
void stack::display()
{
int i;
if(top==-1)
cout<<"stack is empty"<<endl;
else
{
cout<<"Stack elements are\n"<<endl;
for(i=top;i>-1;i--)
cout<<"\t"<<list[i]<<endl;
}
}
stack::stack()
{
int i;
for(i=0;i<10;i++)
list[i]=0;
top=-1;
}
void main()
{
stack st;
clrscr();
int ch,item;
do
{
cout<<"\n1.push\n2.pop\n3.display\n4.Exit\nEnter choice"
<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the item"<<endl;
cin>>item;
st.push(item);
break;
case 2:
item=st.pop();
cout<<"deleted item is\t"<<item<<endl;
break;
case 3:
st.display();
}
}while(ch<4);
}
Queue with Array
========================================================================
#include<iostream.h>
#include<conio.h>
#include<process.h>
class queue
{
int list[10];
int front;
int rear;
public:
queue();
void insert(int item);
int delete1();
void display();
};
void queue::insert(int item)
{
if(rear>=9)
{
cout<<"Queue is full"<<endl;
}
else
{
if(rear==-1)
{
front=0;
list[++rear]=item;
}
else
{
list[++rear]=item;
}
cout<<"insertion successful"<<endl;
}
}
int queue::delete1()
{
int item=-1;
if(rear==-1)
cout<<"Queue is empty"<<endl;
else
{
item=list[front++];
if(front-1==rear)
{
front=-1;
rear=-1;
}
}
return item;
}
void queue::display()
{
int i;
if(front==-1)
cout<<"Queue is empty"<<endl;
else
{
cout<<"Queue elements are\n"<<endl;
for(i=front;i<=rear;i++)
cout<<"\t"<<list[i];
}
}
queue::queue()
{
int i;
for(i=0;i<10;i++)
list[i]=0;
rear=-1;
front=-1;
}
void main()
{
queue qu;
clrscr();
int ch,item;
do
{
cout<<"\n1.Insert\n2.Delete\n3.display\n4.Exit\nEnter choice"
<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the item"<<endl;
cin>>item;
qu.insert(item);
break;
case 2:
item=qu.delete1();
cout<<"deleted item is\t"<<item<<endl;
break;
case 3:
qu.display();
break;
case 4:
exit(1);
}
}while(ch<4);
}
Circular queue
==========================================================================
#include<iostream.h>
#include<conio.h>
class cqueue
{
int list[10];
int size;
int front;
int rear;
public:
cqueue()
{
front=-1;
rear=-1;
size=5;
}
void insert(int item);
void delete1();
void display();
};
void cqueue::insert(int item)
{
if(front==-1)
{
list[0]=item;
front=0;
rear=0;
}
else
{
if((front==rear+1)||((front==0)&&(rear==size-1)))
cout<<"Circular queue is full"<<endl;
else
{
if(rear==size-1)
{
rear=0;
list[rear]=item;
}
else
list[++rear]=item;
}
}
}
void cqueue::delete1()
{
if(front==-1)
cout<<"There is no item in the circular queue"<<endl;
else
{
cout<<"Deleted item is\t"<<list[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
if(front==size-1)
front=0;
else
front++;
}
}
}
void cqueue::display()
{
int i;
for(i=front;i!=rear;)
{
cout<<list[i]<<"\t";
if(i==size-1)
i=0;
else
i++;
}
cout<<list[i]<<endl;
}
void main()
{
cqueue cq1;
int item,ch;
clrscr();
do
{
cout<<"\n\t1.INSERT\n\t2.DELETE\n\t3.DISPLAY\n\tENTER CHOICE"
<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"ENTER ITEM"<<endl;
cin>>item;
cq1.insert(item);
break;
case 2:
cq1.delete1();
break;
case 3:
cq1.display();
break;
default:
cout<<"NO MATCH FOUND"<<endl;
}
}while(ch<4);
}
INSERTION SORT
==========================================================================
#include<iostream.h>
#include<conio.h>
void main()
{
int list[100],n,i,j,k,temp;
clrscr();
cout<<"Enter the n values"<<endl;
cin>>n;
cout<<"Enter the list elements"<<endl;
for(i=0;i<n;i++)
cin>>list[i];
cout<<"List elements before sorting"<<endl;
for(i=0;i<n;i++)
cout<<"\t"<<list[i];
for(i=1;i<n;i++)
{
for(j=0;j<i;j++)
{
if(list[j]>list[i])
{
temp=list[i];
for(k=i;k>j;k--)
{
list[k]=list[k-1];
}
list[j]=temp;
}
}
}
cout<<"\nList elements after sorting"<<endl;
for(i=0;i<n;i++)
cout<<"\t"<<list[i];
getch();
}
linear search program
=========================================================================
#include<iostream.h>
#include<conio.h>
void main()
{
int list[100],n,i,x,flag=0;
clrscr();
cout<<"Enter the size of the list"<<endl;
cin>>n;
cout<<"Enter the list elements"<<endl;
for(i=0;i<n;i++)
cin>>list[i];
cout<<"Enter search element"<<endl;
cin>>x;
for(i=0;i<n;i++)
if(x==list[i])
{
flag=1;
break;
}
if(flag==0)
cout<<x<<" Not Found"<<endl;
else
cout<<x<<" Found"<<endl;
getch();
}
Linked list
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
#define NULL 0
struct node
{
int item;
struct node *next;
public:
node()
{
item=-1;
next=NULL;
}
~node()
{
delete(this);
}
};
typedef struct node node;
class llist
{
node *head;
public:
llist()
{
head=NULL;
}
void insertlast(int item);
void insertmiddle(int item,int pos);
void insertfirst(int item);
void deletefirst();
void deletemiddle(int pos);
void deletelast();
void display();
~llist()
{
delete(head);
}
};
void llist::insertfirst(int item)
{
node *temp;
temp=new node();
temp->item=item;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head=temp;
}
}
void llist::insertmiddle(int item,int pos)
{
node *p,*temp;
int ls=0;
temp=new node();
temp->item=item;
temp->next=NULL;
if(pos==1)
insertfirst(item);
else
{
p=head;
while(p)
{
ls++;
p=p->next;
}
if(pos>ls+1)
cout<<"insertion not possible"<<endl;
else
{
if(pos==ls+1)
insertlast(item);
else
{
p=head;
int i=1;
while(i<pos-1)
{
p=p->next;
i++;
}
temp->next=p->next;
p->next=temp;
}
}
}
}
void llist::insertlast(int item)
{
node *p,*temp;
temp=new node();
temp->item=item;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
p=head;
while(p->next!=NULL)
p=p->next;
p->next=temp;
}
}
void llist::deletefirst()
{
node *p;
if(head==NULL)
cout<<"There is no item in the list"<<endl;
else
{
int item=head->item;
head=head->next;
cout<<"Deleted item is\t"<<item<<endl;
}
}
void llist::deletemiddle(int pos)
{
node *p;
if(head==NULL)
cout<<"There is no items in the list"<<endl;
else
{
if(pos==1)
deletefirst();
else
{
p=head;
int ls=0,i=1,item;
while(p)
{
ls++;
p=p->next;
}
if(pos>ls)
cout<<"Deletion not possible"<<endl;
else
{
if(pos==ls)
deletelast();
else
{
p=head;
while(i<pos-1)
{
p=p->next;
i++;
}
item=p->next->item;
p->next=p->next->next;
cout<<"Deleted item is\t"<<item<<endl;
}
}
}
}
}
void llist::deletelast()
{
node *p;
int item;
if(head==NULL)
cout<<"There is no item in the list"<<endl;
else
{
p=head;
if(p->next==NULL)
{
item=head->item;
head=NULL;
}
else
{
while(p->next->next)
p=p->next;
item=p->next->item;
p->next=NULL;
}
cout<<"Deleted item is\t"<<item<<endl;
}
}
void llist::display()
{
node *p;
if(head==NULL)
cout<<"List is empty"<<endl;
else
{
p=head;
while(p)
{
cout<<p->item<<"\t";
p=p->next;
}
}
}
void main()
{
llist l1;
int ch,item,pos;
clrscr();
do
{
cout<<"\n\t1.INSERT FIRST\n\t2.INSERT MIDDLE\n\t"
<<"3.INSERT LAST\n\t4.DELETE FIRST\n\t"
<<"5.DELETE MIDDLE\n\t6.DELETE LAST\n\t"
<<"7.DISPLAY\n\tENTER CHOICE\n\n"
<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"\tENTER ITEM"<<endl;
cin>>item;
l1.insertfirst(item);
break;
case 2:
cout<<"\tENTER ITEM AND INDEX"<<endl;
cin>>item>>pos;
l1.insertmiddle(item,pos);
break;
case 3:
cout<<"\TENTER ITEM"<<endl;
cin>>item;
l1.insertlast(item);
break;
case 4:
l1.deletefirst();
break;
case 5:
cout<<"Enter position"<<endl;
cin>>pos;
l1.deletemiddle(pos);
break;
case 6:
l1.deletelast();
break;
case 7:
l1.display();
break;
default:
cout<<"NO MATCH FOUND"<<endl;
exit(1);
}
}while(ch<8);
}
Linked List Queue
=========================================================================
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
#define NULL 0
struct node
{
int item;
struct node *next;
public:
node()
{
item=-1;
next=NULL;
}
~node()
{
delete(this);
}
};
typedef struct node node;
class queue
{
node *front;
node *rear;
public:
queue()
{
front=NULL;
rear=NULL;
}
void insert(int item);
void delete1();
void display();
~queue()
{
delete(this);
}
};
void queue::insert(int item)
{
node *temp;
temp=new node();
temp->item=item;
temp->next=NULL;
if(rear==NULL)
{
front=temp;
rear=temp;
rear->next=front;
}
else
{
rear->next=temp;
rear=temp;
rear->next=front;
}
}
void queue::delete1()
{
node *p;
if(front==NULL)
cout<<"There is no item in the Queue"<<endl;
else
{
int item=front->item;
if(front->next==front)
{
front=NULL;
rear=NULL;
}
front=front->next;
rear->next=front;
cout<<"Deleted item is\t"<<item<<endl;
}
}
void queue::display()
{
node *p;
if(front==NULL)
cout<<"Queue is empty"<<endl;
else
{
p=front;
while(p->next!=front)
{
cout<<p->item<<"\t";
p=p->next;
}
cout<<p->item<<endl;
}
}
void main()
{
queue qu;
int ch,item,pos;
clrscr();
do
{
cout<<"\n\t1.INSERT\n\t\n\t2.DELETE\n\t"
<<"3.DISPLAY\n\tENTER CHOICE\n\n"
<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"\TENTER ITEM"<<endl;
cin>>item;
qu.insert(item);
break;
case 2:
qu.delete1();
break;
case 3:
qu.display();
break;
default:
cout<<"NO MATCH FOUND"<<endl;
exit(1);
}
}while(ch<4);
}
Linked List Stack
=========================================================================
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
#define NULL 0
struct node
{
int item;
struct node *prev;
public:
node()
{
item=-1;
prev=NULL;
}
~node()
{
delete(this);
}
};
typedef struct node node;
class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void push(int item);
void pop();
void display();
~stack()
{
delete(this);
}
};
void stack::push(int item)
{
node *temp;
temp=new node();
temp->item=item;
temp->prev=NULL;
if(top==NULL)
{
top=temp;
}
else
{
temp->prev=top;
top=temp;
}
}
void stack::pop()
{
node *p;
if(top==NULL)
cout<<"There is no item in the stack"<<endl;
else
{
int item=top->item;
top=top->prev;
cout<<"Deleted item is\t"<<item<<endl;
}
}
void stack::display()
{
node *p;
if(top==NULL)
cout<<"Stack is empty"<<endl;
else
{
p=top;
while(p)
{
cout<<"\t"<<p->item<<endl;
p=p->prev;
}
}
}
void main()
{
stack st;
int ch,item;
clrscr();
do
{
cout<<"\n\t1.PUSH\n\t2.POP\n\t"
<<"3.DISPLAY\n\tENTER CHOICE\n\n"
<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"\tENTER ITEM"<<endl;
cin>>item;
st.push(item);
break;
case 2:
st.pop();
break;
case 3:
st.display();
break;
default:
cout<<"NO MATCH FOUND"<<endl;
exit(1);
}
}while(ch<4);
}
RADIX
#include<iostream.h>
#include<conio.h>
void place(int a[],int buc[][20],int bc[],int n,int r)
{
int i,row,col;
for(i=0;i<n;i++)
{
row=(a[i]/r)%10;
col=bc[row];
buc[row][col]=a[i];
bc[row]++;
}
}
void retrive(int a[],int buc[][20],int bc[])
{
int i,j,k=0;
for(i=0;i<10;i++)
for(j=0;j<bc[i];j++)
a[k++]=buc[i][j];
}
void initilize(int bc[])
{
int i;
for(i=0;i<10;i++)
bc[i]=0;
}
void main()
{
int list[100],n,big,r,i,pass=0,buc[10][20],bc[10];
clrscr();
cout<<"Enter the list size"<<endl;
cin>>n;
cout<<"Enter the list elements"<<endl;
for(i=0;i<n;i++)
cin>>list[i];
big=list[0];
for(i=1;i<n;i++)
if(big<list[i])
big=list[i];
while(big>0)
{
big/=10;
pass++;
}
r=1;
initilize(bc);
for(i=0;i<pass;i++)
{
place(list,buc,bc,n,r);
retrive(list,buc,bc);
initilize(bc);
r*=10;
}
cout<<"The list after sorting"<<endl;
for(i=0;i<n;i++)
cout<<list[i]<<"\t";
getch();
}
No comments:
Post a Comment