java 十字链表
結(jié)點(diǎn)
package crossList;public class OLNode {//非零元素的行和列下標(biāo) int row, col; int value; //右邊節(jié)點(diǎn)指針 OLNode right; //下方節(jié)點(diǎn)指針 OLNode down; }
package crossList; import crossList.OLNode; import java.util.Scanner;public class CrossList {//十字行鏈表的頭指針 OLNode row_head[]; //十字列鏈表的頭指針 OLNode col_head[]; //稀疏矩陣的行數(shù)、列數(shù)和非零元素的個(gè)數(shù) int m, n, len; //建立十字鏈表 public CrossList() { int m, n, t; int i, j, e; OLNode p=new OLNode(); OLNode q=new OLNode(); //采用十字鏈表存儲(chǔ)結(jié)構(gòu),創(chuàng)建稀疏矩陣M Scanner sc=new Scanner(System.in);m=sc.nextInt();n=sc.nextInt();t=sc.nextInt();this.m = m; this.n = n; this.len = t; //初始化行頭指針向量,各行鏈表為空的鏈表 for(int h=0; h<m+1; h++) { this.row_head[h] = null; } //初始化列頭指針向量,各列鏈表為空的鏈表 for(int k=0; k<n+1; k++) { this.col_head[k] = null; } while(true) { i=sc.nextInt();j=sc.nextInt();e=sc.nextInt();if(i==0)break;//生成節(jié)點(diǎn) p.row = i; p.col = j; p.value = e; if(this.row_head[i] == null) { this.row_head[i] = p; } else { //尋找行表中的插入位置 for(q=this.row_head[i]; q.right!=null&& q.right.col < j; q=q.right); p.right = q.right; q.right = p; } if(this.col_head[j] == null) { this.col_head[j] = p; } else { //尋找列表中的插入位置 for(q=this.col_head[j]; q.down!=null&& q.down.row < i; q=q.down); p.down = q.down; q.down = p; } } sc.close();} }
package crossList; import crossList.OLNode; import java.util.Scanner;public class CrossList {//十字行鏈表的頭指針 OLNode row_head[]; //十字列鏈表的頭指針 OLNode col_head[]; //稀疏矩陣的行數(shù)、列數(shù)和非零元素的個(gè)數(shù) int m, n, len; //建立十字鏈表 public CrossList() { int m, n, t; int i, j, e; OLNode p=new OLNode(); OLNode q=new OLNode(); //采用十字鏈表存儲(chǔ)結(jié)構(gòu),創(chuàng)建稀疏矩陣M Scanner sc=new Scanner(System.in);m=sc.nextInt();n=sc.nextInt();t=sc.nextInt();this.m = m; this.n = n; this.len = t; //初始化行頭指針向量,各行鏈表為空的鏈表 for(int h=0; h<m+1; h++) { this.row_head[h] = null; } //初始化列頭指針向量,各列鏈表為空的鏈表 for(int k=0; k<n+1; k++) { this.col_head[k] = null; } while(true) { i=sc.nextInt();j=sc.nextInt();e=sc.nextInt();if(i==0)break;//生成節(jié)點(diǎn) p.row = i; p.col = j; p.value = e; if(this.row_head[i] == null) { this.row_head[i] = p; } else { //尋找行表中的插入位置 for(q=this.row_head[i]; q.right!=null&& q.right.col < j; q=q.right); p.right = q.right; q.right = p; } if(this.col_head[j] == null) { this.col_head[j] = p; } else { //尋找列表中的插入位置 for(q=this.col_head[j]; q.down!=null&& q.down.row < i; q=q.down); p.down = q.down; q.down = p; } } sc.close();} }
總結(jié)
- 上一篇: java AC自动机
- 下一篇: 程序调试的利器日志