#include <iostream> using namespace std; class sparse{ int row; int column; int data; sparse *row_link; sparse *column_link; public: sparse * create(sparse*); void display(sparse*); }; sparse * sparse::create(sparse*h) { cout<<"\nEnter the no. of rows ::"; cin>>row; cout<<"Enter the no. of columns ::"; cin>>column; // create the head node sparse *columnheader[column]; sparse *rowheader[row]; //create head node h=new sparse; h->row=row; h->column=column; h->data=0; h->row_link=columnheader[column]; h->column_link=rowheader[row]; //create rowheader sparse *ptr; int i,j,d; ptr=h; for (i=1;i<=row ; i++) { rowheader[i]=new sparse; rowheader[i]->row=i; rowheader[i]->column=0; rowheader[i]->row_link=h; rowheader[i]->column_link=rowheader[i]; ptr->column_link=rowheader[i]; ptr=rowheader[i]; } //create columnheader ptr=h; for (i=1;i<=column ; i++) { columnheader[i]=new sparse; columnheader[i]->row=0; columnheader[i]->column=i; columheader[i]->row_link=columheader[i]; columnheader[i]->column_link=h; ptr->row_link=columnheader[i]; ptr=columnheader[i]; } cout<<"\n now enter the non zero elements " <<"one by one\n"; cout<<"\nEnter row number,column number,data\n"; cout<<"Enter (0 0 0) to stop ::"; cin>>i>>j>>d; if(i>row || j>column ||i<1 ||j<1) { cout<<" error input"; exit(1); } while(i&&j&&d) { sparse * row_header=h->column_link; sparse * column_header=h->row_link; // find the correct row header and column header while(row_header->row<i) row_header=row_header->column_link; while(column_header->column<j) column_header=column_header->row_link; sparse *ptr1; sparse *ptr2; // find the correct position to insert sparse*row_ptr=row_header; while(row_ptr->column<j) { ptr1=row_ptr; row_ptr=row_ptr->row_link; if(row_ptr==row_header) break; } sparse*column_ptr=column_header; while(column_ptr->row<i) { ptr2=column_ptr; column_ptr=column_ptr->column_link; if(column_ptr==column_header) break; } sparse *node; node=new sparse; node->row=i; node->column=j; node->data=d; ptr1->row_link=node; ptr2->column_link=node; node->row_link=row_ptr; node->column_link=column_ptr; cout<<"\nEnter row number,column number,data\n"; cout<<"Enter (0 0 0) to stop ::"; cin>>i>>j>>d; if(i>row || j>column ) { cout<<" error inpu"; exit(1); } } return h; } void sparse::display(sparse*h) { sparse *right=h->column_link; while(right->row!=h->column) { sparse *r=right; right=right->row_link; while(r->column !=h->row) { cout<<right->row <<'\t'<<right->column <<'\t'<<right->data<<endl; right=right->row_link; } right=right->column_link; } } int main() { sparse s; sparse *h1=NULL; cout<<"Enter the values for sparse matrix \n"; h1=s.create(h1); s.display(h1); }

This post has been edited by **r.stiltskin**: 13 March 2012 - 06:38 PM

Reason for edit:: Added missing Code Tags, Please learn to use them.