Powered By Blogger

Friday, July 15, 2011

CIRCULAR LINKED LIST:

#include<iostream.h>
#include<conio.h>
class LinkedList
{ struct node
  {int data;
   node *link;
  }*front,*rear;
  int size;
  public:
    LinkedList() { front=rear=NULL;size=0;}
    void addNodeAtEnd(int item);
    void addNodeAtBegin(int item);
    void addNodeAtPos(int pos,int item);
    void display();
    void reverse();
    ~LinkedList();
};
void LinkedList:: addNodeAtEnd(int item)
{
  node *temp=new node;
  temp->data=item;
  if ((front==NULL)&&(rear==NULL))
{front=temp;
front->link=front;
}
  else
{ temp->link=front;
  rear->link=temp;
}
  rear=temp;++size;
}
void LinkedList::addNodeAtBegin(int item)
{node *temp=new node;
 temp->data=item;
 if ((front==NULL)&&(rear==NULL))
{front=temp;
front->link=front;
}
  else
{ temp->link=front;
  rear->link=temp;
  front = temp;
}
  ++size;
}


void LinkedList::addNodeAtPos(int pos,int item)
{ node *r=front;node *temp;
  if ( (pos>1)&&(pos<size)&&(size>1) )
  { for (int i=1;i<pos-1;i++) r=r->link;
    temp=new node;temp->data=item;
    temp->link=r->link;r->link=temp;++size;
  }
  else cout<<"\nOut of Boundary...\n";
}
void LinkedList:: display()
{


   node *r=front;
   while (r!=rear){  cout.width(5);
    cout<<r->data;
    r=r->link;
  }
   cout.width(5);
   cout<<r->data;
}


LinkedList::~LinkedList()
{  node *temp;
while(front!=rear)
{ temp=front;
  front=front->link;
  rear->link=front;
  delete temp;
 }
 delete front;
}
void main()
{
  clrscr();
  LinkedList s;
  int n,m,p;
  do
  {
  cout<<"\n";
  cout<<"\n1. Add Node At End\n";
  cout<<"\n2. Add Node At Begin\n";
  cout<<"\n3. Add Node Between Front and Rear\n";
  cout<<"\n4. Display\n";
  cout<<"\n5. Exit\n";
  cout<<"\n Enter Your Choice...";cin>>n;




  switch (n)
  { case 1: {   cout<<"\n Enter data to be add ";cin>>m;
s.addNodeAtEnd(m);break;}
    case 2: {    cout<<"\n Enter data to be add ";cin>>m;
s.addNodeAtBegin(m);break;}
    case 3: {cout<<"\n Enter Position";cin>>p;
     cout<<"\n Enter data to be add ";cin>>m;
    s.addNodeAtPos(p,m);break;}
    case 4: {s.display();break;}
    case 5: {return;}
    default: {cout<<"\n Sorry...\n";break;}
  }
 clrscr();
 cout<<"Output: ";
 s.display();
 }while (n<=5);
}

Friday, July 8, 2011

datasructures programs

1) LINKED LIST PROGRAM...
 
#include<iostream.h>
#include<conio.h>
class LinkedList
{ struct node
{int data;
node *link;
}*p;
public:
LinkedList() { p=NULL;}
void addNodeAtEnd(int item);
void display();
void reverse();
~LinkedList();
};
void LinkedList:: addNodeAtEnd(int item)
{ node *r=p;
node *temp=new node;
temp->data=item;temp->link = NULL;
if (p==NULL) p=temp;
else
{ while (r->link != NULL) r=r->link;
r->link=temp;
}
}
void LinkedList:: display()
{

node *r=p;
while (r !=NULL){ cout.width(5);//every time reset to default.
cout<<r->data<<" ";
r=r->link;
}
}

void LinkedList::reverse()
{ node *q,*r,*s;
q=p;r=NULL;s=NULL;
while( q!=NULL)
{ s=r;
r=q;
q=q->link;
r->link = s;
}
p=r;
}
LinkedList::~LinkedList()
{ node *temp;
while(p!=NULL)
{ temp=p->link;
delete p;
p=temp;
}
}
void main()
{
clrscr();
LinkedList s;
s.addNodeAtEnd(6);
s.addNodeAtEnd(7);
s.addNodeAtEnd(4);
s.addNodeAtEnd(-9);
cout <<"\nThe List is ..\n"<<endl;
s.display();
cout<<"\n The List in Reverse is..\n"<<endl;
s.reverse();
s.display();
}




2) OPERATIONS ON MATRIX..

#include<iostream.h>
#include<conio.h>
class Matrix
{ private:
int mat[10][10];
int row;
int col;
public:
Matrix();
void create();
void display();
void matAdd(Matrix &m1, Matrix &m2);
void matMul(Matrix &m1, Matrix &m2);
void matrixTran(Matrix &m1);
};

Matrix::Matrix()
{ row=0;col=0;
for( int i=0; i<10; i++)
for( int j=0; j<10; j++)
mat[i][j]=0;
}

void Matrix::create()
{ cout<<"Enter Row and Col sizes of matrix.. :";
cin>>row>>col;
cout<<"Enter Elements of Matrix"<<endl;
for( int i=0; i<row; i++)
for( int j=0; j<col; j++)
cin>>mat[i][j];
}

void Matrix::display()
{ for( int i=0; i<row; i++)
{ cout<<endl;
for( int j=0; j<col; j++)
cout<<mat[i][j]<<" ";
}
}

void Matrix::matAdd(Matrix &m1, Matrix &m2)
{ cout<<"\nThe Result of Addition is"<<endl;
if (m1.row==m2.row && m1.col==m2.col)
{ for( int i=0; i<m1.row; i++)
for( int j=0; j<m1.col; j++)
mat[i][j] = m1.mat[i][j]+m2.mat[i][j];
row=m1.row;col=m1.col;
}
else cout<<"not Possible"<<endl;
}

void Matrix::matMul(Matrix &m1, Matrix &m2)
{ cout<<"\nThe Result of Multiplication is"<<endl;
if (m1.col==m2.row)
{
for( int i=0; i<m1.row; i++)
for( int j=0; j<m2.col; j++)
for( int k=0; k<m1.col; k++)
mat[i][j]=mat[i][j]+m1.mat[i][k]*m2.mat[k][j];
row=m1.row;col=m2.col;
}
else cout<<"not Possible"<<endl;
}

void Matrix::matrixTran(Matrix &m1)
{ cout<<"\nThe Result of Transpose of Matrix1 is "<<endl;
for( int i=0; i<m1.row; i++)
for( int j=0; j<m1.col; j++)
mat[j][i]=m1.mat[i][j];
row=m1.col;col=m1.row;
}

void main()
{
clrscr();
Matrix A;
A.create();
Matrix B;
B.create();
clrscr();
cout<<"\nMatris1\n";
A.display();
cout<<"\nMatrix2\n";
B.display();
Matrix C;
C.matAdd(A,B);
C.display();
Matrix D;
D.matMul(A,B);
D.display();
Matrix E;
E.matrixTran(A);
E.display();
getch();
}


3) ADDITION OF TWO SPARSE MATRICES..

#include<iostream.h>

#include<conio.h>
class Sparse
{ private:
int row;
int spa[10][3];
public:
void getsparse();
void sparseAdd(Sparse &s1,Sparse &s2);
// void sparseSub(Sparse &s1,Sparse &s2);
// void sparseTran();
void display();
};
void Sparse::getsparse()
{ cout<<"Enter No. of rows in sparse(<10) :";
cin>>row;
for(int i=0;i<row;i++)
{for(int j=0;j<3;j++) cin>>spa[i][j];}
}
void Sparse::sparseAdd(Sparse &s1,Sparse &s2)
{ row=0;
cout<<"\n The Addition on two Sparse is "<<endl;
if (s1.spa[0][0]==s2.spa[0][0] && s1.spa[0][1]==s2.spa[0][1])
{ spa[0][0]=s1.spa[0][0];spa[0][1]=s2.spa[0][1];row=1;
int i=1;int j=1;
while(i<s1.row && j<s2.row)
{ if (s1.spa[i][0] < s2.spa[j][0])
{spa[row][0]=s1.spa[i][0];
spa[row][1]=s1.spa[i][1];
spa[row][2]=s1.spa[i][2];
i++;row++;
}
else if (s1.spa[i][0] > s2.spa[j][0])
{spa[row][0]=s2.spa[j][0];
spa[row][1]=s2.spa[j][1];
spa[row][2]=s2.spa[j][2];
j++;row++;
}
else if(s1.spa[i][1]<s2.spa[j][1])
{spa[row][0]=s1.spa[i][0];
spa[row][1]=s1.spa[i][1];
spa[row][2]=s1.spa[i][2];
i++;row++;
}
else if (s1.spa[i][1] > s2.spa[j][1])
{spa[row][0]=s2.spa[j][0];
spa[row][1]=s2.spa[j][1];
spa[row][2]=s2.spa[j][2];
j++;row++;
}
else
{spa[row][0]=s2.spa[j][0];
spa[row][1]=s2.spa[j][1];
spa[row][2]=s1.spa[i][2]+s2.spa[j][2];
i++;j++;row++;
}
}
for(;i<s1.row;i++){ spa[row][0]=s1.spa[i][0];
spa[row][1]=s1.spa[i][1];
spa[row][2]=s1.spa[i][2];
row++;
}
for(;j<s2.row;j++){ spa[row][0]=s2.spa[j][0];
spa[row][1]=s2.spa[j][1];
spa[row][2]=s2.spa[j][2];
row++;
}
spa[0][2]=row-1;
}
else cout<<" Not possible\n";
}




void Sparse::display()
{
for(int i=0;i<row;i++)
{ cout<<endl;
for(int j=0;j<=2;j++) cout<<spa[i][j]<<" ";
}
}
void main()
{
clrscr();
Sparse X,Y,Z;
X.getsparse();
Y.getsparse();
clrscr();
cout<<" \n Sparse 1\n";
X.display();
cout<<"\n Sparse 2\n";
Y.display();
Z.sparseAdd(X,Y);
Z.display();
getch();
}


4) REPRESENTATION OF SPARSE..

#include<iostream.h>
#include<conio.h>
class ConIntoSpa
{ private:
int row;
int r;
int col;
int val;
int m[10][10],s[10][3];
public:
void getmatrix();
void sparsecreation();
void display();
};
void ConIntoSpa::getmatrix()
{ cout<<"Enter row and col sizes lessthan 10 :";
cin>>row>>col;
val=0;r = 0;
for(int i=0;i<row;i++)
{for(int j=0;j<col;j++) cin>>m[i][j];}
}
void ConIntoSpa::sparsecreation()
{
s[0][0]=row,s[0][1]=col;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
if (m[i][j] !=0) { val++;r++;
s[r][0]=i;
s[r][1]=j;
s[r][2]=m[i][j];
}

s[0][2]=r;
}

void ConIntoSpa::display()
{ cout<<"The Sparse Matrix of M is.."<<r<<endl;
for(int i=0;i<=r;i++)
{ cout<<endl;
for(int j=0;j<=2;j++) cout<<s[i][j]<<" ";
}
}
void main()
{
clrscr();
ConIntoSpa O;
O.getmatrix();
O.sparsecreation();
O.display();
getch();
}