#ifndef LINK_TABLE_H
#define LINK_TABLE_H
#include"stdafx.h"
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>
using namespace std;
struct Tnode
{
int row,col,val;
struct Tnode *right,*down;
};
void install(Tnode* head);
//void rand_num(int* &head);
void wait(int wait_time);
void insert(Tnode* head);
void output( Tnode* head);
//void search(Tnode* head);
//void destory(Tnode* head);
//void add(Tnode* head);
//void sub(Tnode* head);
//void mult(Tnode* head);
//void plus(Tnode* head);
//void destory(Tnode* &head);
#endif
/////实现
#include"stdafx.h"
#include"link_table.h"
void install(Tnode* head)
{
cout<<"请输入十字链表的头链表<列表>和<行表> : ";
int c,r;
cin>>c>>r;
if (r!=0&&c!=0)
{
if(r>=65535||c>=65535)
{
cout<<"超出整数能表示的最大数"<<endl;
wait(5);
exit(EXIT_FAILURE);
}
else
{
Tnode* rhead=new Tnode[r];
Tnode* chead=new Tnode[c];
for(int i=0;i<r;++i)
{
rhead[i].down=rhead[i].right=NULL;
rhead[i].val=0;
rhead[i].col=0;
rhead[i].row=i;
}//f
for(int j=0;j<c;++j)
{
chead[j].right=chead[j].down=NULL;
chead[j].val=0;
chead[j].row=0;
chead[j].col=j;
}//for
head->row=r;
head->col=c;
head->val=0;//count none_zero number
head->down=&rhead[0];
head->right=&chead[0];
}//else
}//if
return ;
}
void insert(Tnode* head)
{
Tnode* pr=head->down;
Tnode* pc=head->right;
int r,c,val;
cout<<"输入三元组的行,列,及非零数值"<<endl;
for(cin>>r>>c>>val;val!=0 ;cin>>r>>c>>val)
{
if(r>head->row||c>head->col||val==0)
{
if(r>head->row)
{
cout<<"行值不能大于"<<head->row<<endl;
cout<<"请再次输入 : ";
continue;
}//if
if(c>head->col)
{
cout<<"列值不能大于"<<head->col<<endl;
cout<<"请再次输入 : ";
continue;
}//if
}// if(r>head->row||c>head->col||val==0)
///////////////////////////////华丽分割线/////////////////////////////
else//////////核心实现过程//////////////
{
Tnode* tmp=new Tnode;
tmp->row=r;tmp->col=c;tmp->val=val;
tmp->right=tmp->down=NULL;
if (pr[r].right==NULL) pr[r].right=tmp;
else
{
Tnode* pr_t=&pr[r];
do{
pr_t=pr_t->right;
}while ( pr_t->right !=NULL||pr_t->col<c); //
tmp->right=pr_t->right;
pr_t->right=tmp;
}//else
if (pc[c].down==NULL) pc[c].down=tmp;
else
{
Tnode* pc_t=&pc[c];
do{
pc_t=pc_t->down;
}while ( pc_t->down!=NULL||pc_t->row<r);
tmp->down=pc_t->down;
pc_t->down=tmp;
}//
}//////////else
//////////////////////////华丽分割线////////////////////////////////////////
head->val=head->val+1;
}//for
return ;
}
void wait(int wait_time)///延时
{
clock_t delay=wait_time*CLOCKS_PER_SEC;
cout<<"计时5秒后,强制退出";
clock_t start=clock();
while(clock()-start<delay)
;
return;
}
void output(Tnode* head) /// 输出函数
{
Tnode* item=head->down;
int row=head->row;
for(int i=0;i<row;++i)
{
Tnode* pf=&item[i];
while(pf->right!=NULL)
{
pf=pf->right;
cout<<"( "<<pf->row<<" ,"<<pf->col<<" ,"
<<pf->val<<" )"<<"\t";
}
}//for
return ;
}
/*
void destory(Tnode* &head)
{
int row=head->row;
Tnode* pr=head->down;
for(int r=0;r<row;++r)
{
Tnode* pr_t=&pr[r];
Tnode* tmp=NULL;
while (pr_t->right)
{
tmp=pr_t;
pr_t=pr_t->right;
}
}//for
delete [] head->down;
delete [] head->right;
num=sizeof(head);
cout<<"after delete sizeof(head)= "<<num;
return ;
}
*/
/*void rand_num(int* &head)
{
srand((unsigned int ) time(NULL));
cout<<"请输入数组的大小 : ";
int row, col;
cin>>row>>col;
int rand_array=new int[row][col];
for(int r=1;r<=row;++r)
{for(int c=1;c<=col-1;++c)
{
rand_array[r][c]=rand()%10;
}
rand_array[r][c+1]=NULL;
}
head=&rand_array[1];
}*/
// cross_link_table.cpp: 主项目文件。
#include "stdafx.h"
#include"link_table.h"
int main()
{
Tnode* head=new Tnode;
head->row=head->col=head->val=0;
head->right=head->down=NULL;
install(head);
insert(head);
output(head);
// destory(head);
system("pause");
return 0;
}
http://topic.youkuaiyun.com/u/20110602/23/0e84907d-f8f9-41ec-ae68-6a76269d5f71.html