十字链表的语言描述
基本运算的算法——建立稀疏矩阵的十字链表的算法、输出稀疏矩阵十字链表的算法
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 |
#include<iostream> using
namespace std; template
< class
T> class
shizi { private : struct
Node { int
i; int
j; T data; Node * right; Node * down; Node():right(NULL),down(NULL) {} Node( int
x, int
y , T a) { i = x; j = y; data = a; right = NULL; down = NULL; } }; Node *rhead; Node *chead; Node *rcur; Node *ccur; int
row,col,number; public : shizi( int
x, int
y , int
num) { row = x; Node * head1 = new
Node(); rhead = head1; rcur = head1; for ( int
i = 0 ; i < row ; i++) //建立一列空的行节点 { Node * p = new
Node(); rcur->down = p; rcur = rcur -> down; } col = y; Node * head2 = new
Node(); chead = head2; ccur = head2; for ( int
i = 0 ; i < col ; i++) //建立一行空的列节点 { Node * p = new
Node(); ccur -> right = p; ccur = ccur -> right; } number = num; } int
returnx() //返回x值 { return
rcur->i; } int
returny() //返回y值 { return
rcur->j; } T returndata() //返回data值 { return
rcur->data; } void
charu() //插入 { for ( int
i = 0 ; i < number ; i++) { cout<< "please input x y data" <<endl; int
x1,y1; T data1; cin>>x1>>y1>>data1; rcur = rhead; ccur = chead; //行---建立节点 Node * p = new
Node(x1,y1,data1); for ( int
i = 0 ; i < x1 ; i++ ) //找到要插入的行 { rcur = rcur -> down; } for ( int
i = 0 ; i < y1 ; i++) //找到要插入的列 { if (rcur -> right == NULL) //如果列的元素为空 { rcur -> right = p; break ; } else { rcur = rcur -> right; } } //列指针的连接 for ( int
i = 0 ; i < y1 ; i++) { ccur = ccur -> right; } for ( int
i = 0 ; i < x1 ; i++) { if (ccur -> down == NULL ) { ccur -> down = p; break ; } else { ccur = ccur -> down; } } } } void
print() //打印 { Node * tem = rhead; rcur = rhead; tem = tem->down; rcur = rcur->down; for ( int
i = 0 ; i < row ; i++) //按行打印,遍历一行所有元素,然后移到下一行 { while (rcur!=NULL) { if (returnx()!=0&&returny()!=0) cout<<returnx()<< " " <<returny()<< " " <<returndata()<<endl; rcur = rcur -> right; } rcur = tem -> down; tem = tem -> down; } } }; int
main() { int
x,y,n; cout<< "please input row col number" <<endl; cin>>x>>y>>n; shizi < int
> dusk(x,y,n); dusk.charu(); dusk.print(); } |
原文地址:http://www.cnblogs.com/Duskcl/p/3768554.html