Wednesday, November 23, 2011

CAUTION!!!

வணக்கம்!!!

Just Before now on I have Posted all of the Ten programs in the Data Structures Particles. So there will be nine Blog posts from now on: Numbered. All of them are in CPP. I have Compiled the Programs and Checked them. They are Error Free. But I don't Guarantee it. 


It is my Humble Opinion it will be of Great use to You. If U have any comments or suggestions or Errors.... Please put it up as Comments.


Dont Forget to Share it with your Friends.


Anyway ThankU for visiting my Blog :-)


நன்றி!!!


UPDATE:
Date: 26th November 2011
Time: 21:20::30


I admit that there were Lot of Errors in the Programs, which were posted before in this BLOG. I am deeply SORRY.


But now there is no Problem. I have Compiled all the PROGRAMS and all the Errors have been neatly Patched in the POSTS itself.


If U find any more errors kindly COMMENT upon!!! 


:-)))))))

10 All Pair Shortest Path


#include<iostream>

using namespace std;

int min(int a,int b);
int cost[10][10],a[10][10],i,j,k,c;

int main()
{
  int n,m;

  cout <<"enter no of vertices";
  cin >> n;

  cout <<"enter no of edges";
  cin >> m;

  cout<<"enter the\nEDGE Cost\n";
   for(k=1;k<=m;k++)
  {
  cin>>i>>j>>c;
  a[i][j]=cost[i][j]=c;
  }

 for(i=1;i<=n;i++)
 for(j=1;j<=n;j++)
{
if(a[i][j]== 0 && i !=j)
a[i][j]=31999;
}

for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=min(a[i][j],a[i][k]+a[k][j]);

cout <<"Resultant adj matrix\n";
 for(i=1;i<=n;i++)
 {
  for( j=1;j<=n;j++)
  {
  if(a[i][j] !=31999)
  cout << a[i][j] <<" ";
  }
cout <<"\n";
}

return 0;
}

int min(int a,int b)
{
if(a<b)
return a;
else
return b;
} 

Tuesday, November 22, 2011

9 Merge Sort


#include<iostream>

using namespace std ;


int merge(int[],int,int[],int,int[]) ;

int main()
{
int A[50], B[50], C[50], mn=0, m, n,i ;

cout<<"\nEnter the no. of elements of 1st array: ";
cin>> m ;

cout<<"\nEnter the 1st array: ";
 for(i=0 ; i<m ; i++)
 cin>> A[i] ;

cout<<"\nEnter the no. of elements of 2nd array: ";
cin>> n ;
 
mn=m+n ;

cout<<"\nEnter the 2nd array: ";
 for(i=0 ; i<n ; i++)
 cin>> B[i] ;
 
merge(A,m,B,n,C) ;
   
cout<<"\nThe merged array is: " ;
 for(i=0 ; i<mn ; i++)
 cout<<C[i]<<" " ;

return 0;


}

int merge(int A[],int m,int B[],int n,int C[])
{
int a, b, c;

 for(a=0, b=0, c=0; a<m && b<n; )
 {
 if(A[a]<=B[b])
 C[c++]=A[a++] ;
 else
 C[c++]= B[b++] ;
 }

 if(a<m)
 {
 while(a<m)
  C[c++] = A[a++] ;
 }
 else
 {
 while(b<n)
  C[c++] = B[b++] ;
 }

return 0;

}

7 Binary Transversal

Preorder Inorder Postorder (Using RECURSION)


#include<iostream>
#include<cmath>

using namespace std;

void getdata();
void inorder(int);
void preorder(int);
void postorder(int);

int t[50];
int n,i,l;

int main()
{
getdata();

cout<<"\nINORDER";
inorder(1);

cout<<"\nPREORDER";
preorder(1);

cout<<"\nPOSTORDER";
postorder(1);
cout<<"\n";

return 0;

}

void getdata()
 {
 cout<<"\nEnter the no.of levels of the binary tree:";
 cin>>l;

 n=(pow(2,l))-1;
 cout<<"\nThe maximum no.of the nodes for the given level of the binary tree is"<<" "<<n<<"\n";
 cout<<"\nEnter the nodes of the tree:";
 for(i=1;i<=n;i++)
 cin>>t[i];
 }

 void inorder(int i)
 {
  if(i<=n)
  {
  inorder(i*2);
  if(t[i]!=0)
  cout<<"\t"<<t[i];
  inorder(i*2+1);
  }
 }

 void preorder(int i)
 {
  if(i<=n)
  {
  if(t[i]!=0)
  cout<<"\t"<<t[i];
  preorder(i*2);
  preorder(i*2+1);
  }
 }

 void postorder(int i)
 {
  if(i<=n)
  {
  postorder(i*2);
  postorder(i*2+1);
  if(t[i]!=0)
  cout<<"\t"<<t[i];
  }
 }

8 Quick Sort


#include<iostream>
#include <stdlib.h>

using namespace std;

void exit(int status);
void qusort(int numbers[], int array_size);
void qsort(int numbers[], int left, int right);

int numbers[150];

int main()
{
   int i,n;
   cout<<"How many numbers you want to sort: ";
   cin>>n;
   cout<<"Enter "<<n<<" numbers.\n";
   for (i = 0; i<n; i++)
      cin>>numbers[i];
//perform quick sort on array
   qsort(numbers,0,n-1);

   cout<<"Numbers are sorted\n";
   for (i = 0; i<n; i++)
      cout<<numbers[i]<<"   ";
   return(0);
}

// Function to sort
void qsort(int numbers[], int left, int right)
{
   int pivot, l_hold, r_hold;
   l_hold = left;
   r_hold = right;
   pivot = numbers[left];
   while (left < right)
   {
      while ((numbers[right] >= pivot) && (left < right))
      right--;
      if (left != right)
      {
     numbers[left] = numbers[right];
     left++;
      }
      while ((numbers[left] <= pivot) && (left < right))
     left++;
      if (left != right)
      {
      numbers[right] = numbers[left];
      right--;
      }
   }
   numbers[left] = pivot;
   pivot = left;
   left = l_hold;
   right = r_hold;
   if (left < pivot)
      qsort(numbers, left, pivot-1);
   if (right > pivot)
      qsort(numbers, pivot+1, right);
}

6 Dijkstra's Algorithm


#include<iostream>
using namespace std;

int cost[20][20];

int main()
{
int i,j,c[10][10],dist[10],s[10];
int temp,tempcost,n,src,minm;

cout<<"\n\nEnter the no. of vertices:";
cin>>n;

cout<<"\nGive the cost:";

for(i=1;i<=n;i++)
 {
 for(j=1;j<=n;j++)
 {
  if(i==j)
  {
  c[i][j]=0;
  }
  else
  {
  cout<<"Enter the cost:"<<i<<"to"<<j<<"::";
  cin>>c[i][j];
  }
 }
}

cout<<"\nEnter the vertex number to find the shortest path for all paths:";
cin>>src;

cout<<"\nThe adjacency matrix given is:";
for(i=1;i<=n;i++)
{
 for(j=1;j<=n;j++)
 {
 cout<<c[i][j]<<"\t";
 }
 cout<<"\n";
}

for(i=1;i<=n;i++)
{
s[i]=0;
dist[i]=c[src][i];
}

s[src]=1;dist[src]=0;

for(i=1;i<=n;i++)
{
 for(j=1;j<=n;j++)
 {
  if(s[j]==0)
  {
  minm=dist[j];
  temp=j;
  break;
  }
 }
}

for(j=1;j<=n;j++)
{
 if(s[j]==0 && minm>dist[j])
 {
 minm=dist[j];
 temp=j;
 }
}

s[temp]=1;

for(j=1;j<=n;j++)
{
 if(s[j]==0)
 {
 tempcost=dist[temp]+c[temp][j];
  if(dist[j]>tempcost)
  {
  dist[j]=tempcost;
  }
 }
}

cout<<"\n\nThe vertices are:"<<"\n";
for(i=1;i<=n;i++)
cout<<"\t"<<i;

cout<<"\nThe distances are:"<<"\n";
for(i=1;i<=n;i++)
cout<<dist[i]<<"\t";

return 0;
}

5 Binary - Insertion Search & Deletion


#include<iostream>
#include<stdlib.h>

using namespace std;

struct node
{
int d;
struct node *left;
struct node *right;
};

struct node *root;


void init();
void empty();
int insert(int);
void search(int);
void remove(int);
 
void init()
{
root=(struct node *)malloc(sizeof(struct node));
root=NULL;
}

int insert(int x)
{
struct node *p,*previous,*current;
p=(struct node *)malloc(sizeof(struct node));
 if (p==NULL)
 {
 cout<<"\nOut of Memory";
 return 0;
 }
p->d=x;
p->left=NULL;
p->right=NULL;
 if(root==NULL)
 {
 root=p;
 return 1;
 }
previous=NULL;
current=root;

 while (current!=NULL)
 {
 previous=current;
  if (p->d<current->d)
  current=current->left;
  else
  current=current->right;
 }

 if(p->d<previous->d)
 previous->left=p;
 else
 previous->right=p;

return 1;
}

void remove(int x)
{
struct node *ptr=root, *parent = NULL, *t1, *t2, *temp;
 while(ptr!=NULL && ptr->d!=x)
 {
 parent=ptr;
 if(x<ptr->d)
  ptr=ptr->left;
 else
  ptr=ptr->right;
 }

 if(ptr==NULL)
 {
 cout<<"\nDelete Element Not found";
 return;
 }
 
 if(ptr->left==NULL)
  t1=ptr->right;
 else if(ptr->right==NULL)
  t1=ptr->left;
 else
 {
 t2=ptr;
 t1=ptr->right;
 temp=t1->left;
 
  while(temp!=NULL)
  {
  t2=t1;
  t1=temp;
  temp=t1->left;
  }
 
  if(t2!=ptr)
  {
  t2->left=t1->right;
  t1->right=ptr->right;
  }

  t1->left=ptr->left;
 }

 if(parent==NULL)
  root=t1;
 else
 {
  if(parent->left==ptr)
  parent->left=t1;
  else
  parent->right=t1;
 }

free(ptr);
}


void search(int sno)
{
struct node*t;
t=(struct node *)malloc(sizeof(struct node));
t=root;
 while(t!=NULL && t->d !=sno)
 {
  if(t->d>sno)
  { t=t->left; }
  else
  { t=t->right; }
 }

 if(t)
 cout<<"\nThe Search Element is Present";
 else
 cout<<"\nElement not Present";
}

int main()
{
int c,no,x;
init();

 do
 {
 cout<<"\n1.Insert";
 cout<<"\n2.Deletion";
 cout<<"\n3.Search";
 cout<<"\n4.Exit";
 cout<<"\nEnter Your Choice:\t";
 cin>>c;
 
  switch(c)
  {
  case 1:cout<<"\nEnter the Element to Insert:\t";
         cin>>no;
         x=insert(no);
          if(x==1)
          cout<<"\nsucessful Insertion";
          else
          cout<<"\nError in insertion";
         break;
  case 2:cout<<"\nEnter the Element to Delete:";
         cin>>x;
         remove(no);
         break;
  case 3:cout<<"\nEnter the Element to Search: \t";
         cin>>no;
         search(no);
         break;
  case 4: exit(0); break;
  default: cout<<"\nEnter Any No. Between 1-7"; break;
  }
 }while(c!=4);
return 0;
}

4 Queue - Linked List


#include<iostream>
#include<stdlib.h>

using namespace std;

struct node
{
int data;
struct node *link;
};
struct node *cur,*first,*last;

void insert();
void delte();
void display();

void insert()
{
 if(first==NULL)
 {
 cout<<"\nENTER THE FIRST ELEMENT: ";
 cur=(struct node *)malloc(sizeof(struct node));
 cin>>cur->data;
 cur->link=NULL;
 first=cur;
 last=cur;
 }
 else
 {
 cout<<"\nENTER THE NEXT ELEMENT: ";
 cur=(struct node *)malloc(sizeof(struct node));
 cin>>cur->data;
 cur->link=NULL;
 last->link=cur;
 last=cur;
 }
}

void delte()
{
 if(first==NULL)
 {
 cout<<"\t\nQUEUE IS EMPTY\n";
 }
 else
 {
 cur=first;
 first=first->link;
 cur->link=NULL;
 cout<<"\n DELETED ELEMENT IS:";
 cout<<cur->data;
 free(cur);
 }
}

void display()
{
 if(first==NULL)
 cout<<"\nQueue Empty";
 else
 {
 cur=first;
 cout<<"\n";
  while(cur!=NULL)
  {
  cout<<"\t"<<cur->data;
  cur=cur->link;
  }
 }
}

int main()
{
 int ch;
 do
 {
 cout<<"\n 1.INSERT \n 2.DELETE  \n 3.Display \n 4.EXIT ";
 cout<<"\nENTER YOUR CHOICE : ";
 cin>>ch;

  switch(ch)
  {
  case 1: insert(); break;
  case 2: delte(); break;
  case 3: display(); break;
  case 4: exit(0);
  default:cout<<"\nInvalid Choice";
  }
 }while(ch!=4);
return 0;
}

3 Stack - Linked List


# include<iostream>
#include<stdlib.h>

using namespace std;

void push();
void pop();
void display();


struct node
{
int info;
struct node *link;
} *top=NULL;

int main()
{
int choice;

 do
 {
 cout<<"\n\n1.Push\n";
 cout<<"2.Pop\n";
 cout<<"3.Display\n";
 cout<<"4.Quit\n";
 cout<<"Enter your choice : ";
 cin>>choice;

  switch(choice)
  {
  case 1:push();break;
  case 2:pop();break;
  case 3:display();break;
  case 4:exit(1);
  default :cout<<"Wrong choice\n";
  }
 }while(choice!=4);
return 0;
}

void push()
{
struct node *tmp;
int pushed_item;

tmp = (struct node *)malloc(sizeof(struct node));

cout<<"Value to Push:";
cin>>pushed_item;

tmp->info=pushed_item;
tmp->link=top;
top=tmp;
}

void pop()
{
struct node *tmp;
 if(top == NULL)
 cout<<"Stack is empty\n";
 else
 {
 tmp=top;
 cout<<"Popped item is "<<tmp->info;
 top=top->link;
 free(tmp);
 }
}

void display()
{
struct node *ptr;
ptr=top;
 if(top==NULL)
 cout<<"Stack is empty\n";
 else
 {
 cout<<"Stack elements :\n";
  while(ptr!= NULL)
  {
  cout<<"\n"<<ptr->info;
  ptr = ptr->link;
  }
 }
}

2 Queue - Array


# include<iostream>

# define MAX 5



using namespace std;

int qu[MAX];

int r = -1;

int f = -1;

void insert();
void del();
void disply();



int main()

{

int choice;


 do

 {

 cout<<"1.Insert\n";

 cout<<"2.Delete\n";

 cout<<"3.Display\n";

 cout<<"4.Quit\n";

 cout<<"Enter your choice : ";

 cin>>choice;



  switch(choice)

  {

  case 1 :insert();break;

  case 2 :del();break;

  case 3:display();break;

  case 4:exit(1);

  default:
cout<<"Wrong choice\n";

  }

 }while(choice!=4);

return 0;
}



void insert()

{

int added_item;


 if (r==MAX-1)

 cout<<"Queue Overflow\n";

 else

 {

  if (f==-1)
  f=0;

  cout<<"Input the element for adding in queue : ";

  cin>>added_item;

  r=r+1;

  qu[r] = added_item ;

 }

}



void del()

{

 if (f == -1 || f > r)

 {

 cout<<Queue Underflow\n";

 return ;

 }

 else

 {

 cout<<"Element deleted from queue is :"
 cout<<"\n"<<qu[f];

 f=f+1;

 }

}



void display()

{

int i;

 if (f ==-1 )

 cout<<Queue is empty\n";

 else

 {

 cout<<"Queue is :\n";

 for(i=f;i<= r;i++)

 cout<<qu[i]<<"\n";

 }

}

1 Stack - Array


#include<iostream>

#define MAX 5



using namespace std;

int top = -1;

int stack[MAX];



void push();
void pop();
void display();


int main()

{

int choice;


 do

 {

 cout<<"\n\n1.Push\n";

 cout<<"2.Pop\n";

 cout<<"3.Display\n";

 cout<<"4.Quit\n";

 cout<<"Enter your choice : ";

 cin>>choice;



  switch(choice)

  {

  case 1 :push();
break;

  case 2:pop();
break;

  case 3:display();
break;

  case 4:exit(1);


  default:cout<<"Wrong choice\n";

  }
 }while(choice!=4);
return 0;
}



void push()

{

int p;


if(top == (MAX-1))

cout<<"Stack Overflow\n";

 else

 {

 cout<<"Enter the item to be pushed in stack : ";

 cin>>p;
 top=top+1;

 stack[top] = p;

 }

}



void pop()

{

 if(top == -1)

 cout<<Stack Underflow\n";

 else

 {

 cout<<"Popped element is : "
 cin>>stack[top];

 top=top-1;

 }

}



void display()

{

int i;

 if(top == -1)

 cout<<"Stack is empty\n";

 else

 {

 cout<<"Stack elements :\n";

 for(i = top; i >=0; i--)

 cout<<"\n"
 cout<<stack[i];

 }

}