Saturday 12 October 2013

CIRCULAR LINK LIST

                   CIRCULAR  LINK LIST                
               INSERTION AND DELETION         

#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *link;
} *head;

void insbeg()
{
node *temp;
temp= new node;
if(temp==0)
cout<<"\nOverflow";
else
{
int data;
cout<<"Enter the data you want to insert: ";
cin>>data;
temp->data=data;
node *temp1;
if(head==0)
{
temp->link=temp;
head=temp;
}
else
{
temp->link=head;
temp1=head;
while(temp1->link!=head)
{
temp1=temp1->link;
}
head=temp;
temp1->link=head;
}
}
}
void display()
{
node *temp;
temp=head;
if(head==0)
cout<<"\nNo linked list";
else
{
cout<<"\nLinked List is : ";
while(temp->link!=head)
{
cout<<temp->data<<" ";
temp=temp->link;
}
cout<<temp->data;
}
}
void insend()
{
node *temp,*temp1;
temp=new node;
if(temp==0)
cout<<"\nOverflow";
else
{
int data;
cout<<"Enter the data you want to insert: ";
cin>>data;
temp->data=data;
if(head==0)
{
temp->link=temp;
head=temp;
}
else
{
temp->link=head;
temp1=head;
while(temp1->link!=head)
{
temp1=temp1->link;
}
temp1->link=temp;
}
}
}
void insbet()
{
node *temp,*temp1;
int count=1,tnode=1;
temp=new node;
if(temp==0)
cout<<"\nOverflow";
else
{
int data,pos;
cout<<"Enter the data you want to insert: ";
cin>>data;
cout<<"Enter the position";
cin>>pos;
temp->data=data;
temp1=head;
while(temp1->link!=head)
{
tnode++;
temp1=temp1->link;
}
if(pos==1)
{
temp->link=head;
head=temp;
temp1->link=head;
}
else
{
if(tnode<pos)
cout<<"\nPosition does not exist";
else
{
temp1=head;
while(count!=pos-1)
{
count++;
temp1=temp1->link;
}
temp->link=temp1->link;
temp1->link=temp;
}
}
}
}
void delbeg()
{
node *temp,*temp1;
if(head==0)
cout<<"\nLink List does not exist";
else
{
temp=head;
if(temp->link==head)
{
head=0;
delete temp;
}
else
{
temp1=head;
while(temp1->link!=head)
temp1=temp1->link;
head=temp->link;
temp1->link=head;
delete temp;
}
}
}
void delend()
{
node *temp,*temp1;
if(head==0)
cout<<"No Link List";
else
{
temp=head;
if(temp->link==head)
{
head=0;
delete temp;
}
else
{
while(temp->link!=head)
{
temp1=temp;
temp=temp->link;
}
temp1->link=head;
delete temp;
}
}
}
void delbet()
{
int pos;
cout<<"Enter the position";
cin>>pos;
node *temp,*temp1;
int count=1,tnode=1;
if(head==0)
cout<<"\nNo Link Iist";
else
{
temp=head;
while(temp->link!=head)
{
tnode++;
temp=temp->link;
}
if(tnode<pos)
cout<<"\nPosition does not exist";
else
{
temp1=head;
if(pos==1)
{
head=temp1->link;
temp->link=head;
delete temp1;
}
else
{
temp=head;
while(count!=pos)
{
count++;
temp1=temp;
temp=temp->link;
}
temp1->link=temp->link;
delete temp;
}
}
}
}
void main()
{
clrscr();
int ch;
char ans;
cout<<"\nMENU";
cout<<"\n1).Insert in the Begining";
cout<<"\n2).Insert in the End";
cout<<"\n3).Insert in between ";
cout<<"\n4).Delete from the Begining";
cout<<"\n5).Delete from the End";
cout<<"\n6).Delete from the between";
do
{
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:insbeg();
       display();
       break;
case 2:insend();
       display();
       break;
case 3:insbet();
       display();
       break;
case 4:delbeg();
       display();
       break;
case 5:delend();
       display();
       break;
case 6:delbet();
       display();
       break;
default :cout<<"\n Wrong choice";
}
cout<<"\nDo you want more press y for yes: ";
cin>>ans;
}while(ans=='y');
getch();
}

 

Thursday 10 October 2013

PROGRAM OF DOUBLE ENDED QUEUE IN C++

               DOUBLE ENDED QUEUE              

#include<iostream.h>
#include<conio.h>
#include<process.h>
int arr[10];
int max=10,left=-1,right=-1;
void inr()
{
int data;
cout<<"Enter the data you want to insert : ";
cin>>data;
if(left==0 && right==max-1)
{
cout<<"\noverload";
exit(0);
}
else if(left==-1)
{
left=0;
right=0;
}
else if(right==max-1)
right=0;
else
right=right+1;
arr[right]=data;
}
void inl()
{
int data;
cout<<"Enter the data you want to insert : ";
cin>>data;
if(left==0 && right==max-1)
{
cout<<"overflow";
exit(0);
}
if(left==-1)
{
left=0;
right=0;
}
else if(left==0)
left=max-1;
else
left=left-1;
arr[left]=data;
}
void view()
{
if(left!=-1)
{
cout<<"Queue is :";
for(int i=0;i<max;i++)
{
if(arr[i])
cout<<arr[i]<<endl;
}
}
}
void der()
{
if(left==-1)
{
cout<<"underflow";
exit(0);
}
arr[right]=0;
if(left==right)
{
left=-1;
right=-1;
}
else if(right==0)
right=max-1;
else
right=right+1;
}
void del()
{
if(left==-1)
{
cout<<"Underflow";
exit(0);
}
arr[left]=0;
if(left==right)
{
left=-1;
right=-1;
}
else if(left==max-1)
left=0;
else
left=left+1;
}
void main()
{
clrscr();
char ans;
int ch;
cout<<"MENU";
cout<<"\n1).Insert in right";
cout<<"\n2).Insert in left";
cout<<"\n3).Delete from right";
cout<<"\n4).Delete from left";
do
{
cout<<"\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
      inr();
      view();
      break;
case 2:
       inl();
       view();
       break;
case 3:
       der();
       view();
       break;
case 4:
       del();
       view();
       break;
default: cout<<"\n you made a wrong choice";
}
cout<<"Do you want more y for yes : ";
cin>>ans;
}while(ans=='y');
getch();
}



 

PROGRAM OF BINARY SEARCH THROUGH C++

           PROGRAM OF  BINARY SEARCH                         THROUGH C++.                            

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[5];
int max=5;
int n;
cout<<" Enter the elements of array";
for(int i=0;i<max;i++)
{
cin>>arr[i];
}
cout<<"Enter the elements to search";
cin>>n;
int lb=0,ub=max-1;
int mid=(lb+ub)/2;
while(lb<=ub && arr[mid]!=n)
{
if(n<arr[mid])
ub=mid-1;
else
lb=mid+1;
mid=(lb+ub)/2;
}
if(arr[mid]==n)
cout<<"\nSearch is Successful. "<<n<<" is found at "<<mid+1;
else
cout<<"\nUnSuccessful.";
getch();
}
 

Tuesday 14 May 2013

 
SUM OF 2 COMPLEX NUMBER 
  #include <stdio.h>
  #include <conio.h>
  #include <stdlib.h>
  struct complex
  {
      int real;
      int img;
  };
  int main()
  {
      struct complex a, b, c;
      printf(" Enter a and b where a + ib is the first complex number. ");
      printf("\n a = ");
      scanf(" %d ", &a.real);
      printf(" b = ");
      scanf(" %d ", &a.img);
      printf(" Enter c and d where c + id is the second complex number. ");
      printf("\n c = ");
      scanf(" %d ", &b.real);
      printf(" d = ");
      scanf(" %d ", &b.img);
      c.real = a.real + b.real;
      c.img = a.img + b.img;
      if ( c.img >= 0 )
          printf(" Sum of two complex numbers = %d + %di ", c.real, c.img);
      else
          printf(" Sum of two complex numbers = %d %di ", c.real, c.img);
      getch();
      return 0;
  }