[Data structures]Orthogonal link
#include <stdio.h>
#include <stdlib.h>
#define MAX 1024
typedef struct _OLNode{
int i,j;
int e;
struct _OLNode *right, *down;
}OLNode;
typedef struct _CrossList{
OLNode **rhead;
OLNode **chead;
int m,n,t; //m rows, n cols, t none zero
}CrossList;
int CreateMatrix(CrossList *M)
{
int i,j,k,e;
OLNode *p,*q;
scanf(" %d %d %d", &M->m, &M->n, &M->t);
M->rhead = (OLNode **)malloc( sizeof(OLNode *) * (M->m + 1) );
M->chead = (OLNode **)malloc( sizeof(OLNode *) * (M->n + 1) );
for(i=1; i<=M->m; i++){
M->rhead[i] = NULL;
}
for(i=1; i<=M->n; i++){
M->chead[i] = NULL;
}
for(k=1; k<=M->t; k++){
scanf(" %d %d %d", &i, &j, &e);
p = (OLNode *)malloc( sizeof(OLNode) );
p->i = i;
p->j = j;
p->e = e;
if(M->rhead[i] == NULL || M->rhead[i]->j > j){
p->right = M->rhead[i];
M->rhead[i] = p;
}else{
for(q = M->rhead[i]; q->right && q->right->j < j;
q = q->right);
p->right = q->right;
q->right = p;
}
if(M->chead[j] == NULL || M->chead[j]->i > i){
p->down = M->chead[j];
M->chead[j] = p;
}else{
for(q = M->chead[j]; q->down && q->down->i < i;
q = q->down);
p->down = q->down;
q->down = p;
}
}
return 1;
}
void PrintMatrix(CrossList M)
{
int i,j,k;
OLNode *pc;
for(i=1; i<=M.m; i++){
pc = M.chead[i];
for(j=1; j<=M.n; j++){
if(pc && pc->i == i && pc->j == j){
printf(" %2d", pc->e);
pc = pc->right;
}else{
printf(" %2d", 0);
}
}
printf("\n");
}
}
void main()
{
CrossList M;
printf("log.anycle.com\n\n");
printf("Create matrix M:\n");
CreateMatrix(&M);
PrintMatrix(M);
printf("\n");
}
日历
最新微语
- 有的时候,会站在分叉路口,不知道向左还是右
2023-12-26 15:34
- 繁花乱开,鸟雀逐风。心自宁静,纷扰不闻。
2023-03-14 09:56
- 对于不可控的事,我们保持乐观,对于可控的事情,我们保持谨慎。
2023-02-09 11:03
- 小时候,
暑假意味着无忧无虑地玩很长一段时间,
节假意味着好吃好喝还有很多长期不见的小朋友来玩...
长大后,
这是女儿第一个暑假,
一个半月...
2022-07-11 08:54
- Watching the autumn leaves falling as you grow older together
2018-10-25 09:45
分类
最新评论
- Goonog
i get it now :) - 萧
@Fluzak:The web host... - Fluzak
Nice blog here! Also... - Albertarive
In my opinion you co... - ChesterHep
What does it plan? - ChesterHep
No, opposite. - mojoheadz
Everything is OK!... - Josephmaigh
I just want to say t... - ChesterHep
What good topic - AnthonyBub
Certainly, never it ...
发表评论: