#include <iostream>
#include <fstream>
#define Rownum 4
#define Colnum 4
int a[4][4]={{1,3,0,3},
{4,0,3,2},
{5,0,2,1},
{3,3,1,4}};
using namespace std;
typedef struct node
{
int data; //the data on each node
node*right;
node*down;//define the direction vector
node*next;
int col;
int row;
}*crosslist;
crosslist p[5];
void initialization( )
{
p[0]=new node;
crosslist q=new node;
p[0]->data=0;
p[0]->row= Rownum;
p[0]->col=Colnum;
p[0]->next=p[0];
p[0]->right= p[0];
p[0]->down=p[0];
q= p[0];
for(int i=1;i<=4;i++)
{
p[i]=new node;
p[i]->col=0;
p[i]->row=0;
p[i]->data=0;
p[i]->next=q->next;
q->next=p[i];
q=p[i];
p[i]->right=p[i];
p[i]->down=p[i];
}
}
void buildlist(crosslist pnew)//start the cross linklist
{
int m, n;
m= pnew->col;
n=pnew->row;
crosslist p1=p[n];
while (p1->right!=p[n]&&p1->right->col<m)
{ p1=p1->right;
}
pnew->right=p1->right;
p1->right=pnew;
crosslist p2=p[m];
while(p2->down!=p[m]&&p2->down->row<n)
{
p2=p2->down;
}
pnew->down=p2->down;
p2->down=pnew;
}
void getval( )//insert value into the down and the right linklist
{
crosslist p0,q;
int i,j;
for(i=0;i<=3;i++)
{for(j=0;j<=3;j++)
{
if(a[i][j]!=0)
{ p0=new node;
p0->row=i;
p0->col=j;
p0->data=a[i][j];
p0->right=p0;
p0->down=p0;
buildlist(p0);
}
}
}
}
void output()//print the value by collum
{ int i,m,n,k;
crosslist p3;
for(i=1;i<=4;i++)
{
p3=p[i];
while(p3->down!=p[i])
{ m=p3->down->data;
n=p3->down->row;
k=p3->down->col;
cout<<m<<","<<n<<","<<k<<endl;
cout<<endl;
p3=p3->down;
}
}
}
int main()
{
initialization( );
getval();
output();
system("pause");
return 0;
}
orthogonal list
最新推荐文章于 2022-07-02 14:53:00 发布