1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#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!=p1&&p1->right->col<m)
{ p1->right=p1;
pnew->right=p1->right;
p1->right=pnew;
}
crosslist p2=p[m];
while(p2->down!=p2&&p2->down->row<n)
{ p2->down=p2;
pnew->down=p2->down;
p2->down=pnew;
}
}
void getval( )//insert value into the down and the right linklist
{
crosslist p,q;
p=new node;
int i,j;
for(i=0;i<=3;i++)
{for(j=0;j<=3;j++)
{
if(a[i][j]!=0)
{
p->row=i;
p->col=j;
p->data=a[i][j];
p->right=p;
p->down=p;
buildlist(p);
}
}
}
}
void output()//print the value by collum
{ int i,m,n,k;
crosslist p3;
p3=p[i];
for(i=1;i<=4;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;
}
|