首先是三元组稀疏矩阵的加法:
代码如下:
#include<iostream>
using namespace std;
enum error_codes { overflow,success,underflow };
struct tri_elem_group{
int i;
int j;
int v;
};//三元组数据类型,相当于int;
struct node{
tri_elem_group date;
node* next;
};//节点,就是node;
class sparse_martix{//稀疏矩阵类。
public:
sparse_martix() {//构造函数。
count = 0;
head = new node;
head->next = nullptr;
}
~sparse_martix() {//析构函数。
while (count > 0) {
dele_node();
}
delete head;
}
int showcount() {//查看count的大小。
return count;
}
sparse_martix(const sparse_martix &a) {//拷贝构造函数。
head = new node;
head->next = nullptr;
node* s = a.head->next;
while (s != nullptr) {
node* s1 = new node;
s1->date.i = s->date.i;
s1->date.j = s->date.j;
s1->date.v = s->date.v;
s1->next = head->next;
head->next = s1;
s = s->next;
}
count = a.count;
}
error_codes insert_node(const tri_elem_group &a) {//向稀疏矩阵中添加三元组。
node* s = new node;
s->date.i = a.i;
s->date.j = a.j;
s->date.v = a.v;
s->next = head->next;
head->next = s;
count++;
return success;
}
error_codes dele_node() {//删除稀疏矩阵中的三元组。
if (count == 0) {
return underflow;
}
node* s = head;
s = s->next;
if (s != nullptr) {
head->next = s->next;
count--;
delete s;//
return success;
}
return overflow;
}
sparse_martix add_up(sparse_martix &x) {//两个稀疏矩阵求和。
if (x.count == 0) {
sparse_martix s(*this);
return s;
}
if (count == 0) {
sparse_martix s(x);
return s;
}
sparse_martix s;
node* s1 = head->next;
while (s1 != nullptr) {
bool flag = false;
node* s2 = x.head->next;
while (s2 != nullptr) {
if (s1->date.i == s2->date.i) {
if (s2->date.j == s1->date.j) {
tri_elem_group t;
t.i = s1->date.i;
t.j = s1->date.j;
t.v = s1->date.v + s2->date.v;
s.insert_node(t);
flag = true;
}
}
s2 = s2->next;
}
if (flag == false) {
tri_elem_group t;
t.i = s1->date.i;
t.j = s1->date.j;
t.v = s1->date.v;
s.insert_node(t);
}
s1 = s1->next;
}
node* s22 = x.head->next;
while (s22 != nullptr) {
bool flag = false;
node* s11 = head->next;
while (s11 != nullptr) {
if (s22->date.i == s11->date.i) {
if (s11->date.j == s22->date.j) {
flag = true;
}
}
s11 = s11->next;
}
if (flag == false) {
tri_elem_group t;
t.i = s22->date.i;
t.j = s22->date.j;
t.v = s22->date.v;
s.insert_node(t);
}
s22 = s22->next;
}
return s;
}
void show() {//打印出所有三元组。
node* s = head->next;
cout << "含有元素:";
while (s != nullptr) {
cout << s->date.i << " " << s->date.j << " " << s->date.v << endl;
s = s->next;
}
}
private:
node* head;
int count;
};
int main() {
sparse_martix spa, spa1;
tri_elem_group t;
int n2, n1;
cin >> n2 >> n1;
for (int i = 0; i < n2; i++) {//向稀疏阵1添加三元组
int k, n, m;
cin >> m >> n >> k;
t.i = m;
t.j = n;
t.v = k;
spa.insert_node(t);
}
for (int i = 0; i < n1; i++) {//向三元组2添加三元组
int k, n, m;
cin >> m >> n >> k;
t.i = m;
t.j = n;
t.v = k;
spa1.insert_node(t);
}
sparse_martix spa2 (spa.add_up(spa1));
spa2.show();
return 0;
}
然后就是令人头疼的广义表,网上参考的大佬代码,网址如下:其中他用来表示数据类型的标志值得我们学习。
附上使用的主函数:
int main() {
char a[100];
cin >> a;
GeneralizedList g(a);
g.Print();
cout << endl;
cout << g.Depth() << endl;
cout << g.Size() << endl;
return 0;
}
补全head()和tail()函数:
以下代码写到类的public部分即可
void Head() {//取表头
assert(_head);
Node* cur = _head;
while (cur) {
cur = cur->_next;
if (cur->_type == VALUE_TYPE)
{
cout << cur->_value;
return;
}
if (cur->_type == SUB_TYPE) {
_print(cur->_sublink);
return;
}
}
}
void Tail() {//去表头
assert(_head);
Node* cur = _head;
if (cur) {
Node* del = cur;
cur = cur->_next;
if (cur) {
cur->_type = HEAD_TYPE;
_head = cur;
if (del->_type == SUB_TYPE) {
Destory(del->_sublink);
return;
}
delete[] del;
return;
}
}
}
当然,网络上还有另一个版本,来自另一位大佬:
这是另一位大佬的链接,他的线性表,做了一个替换头部的函数,貌似高级了很多!
当然,我就不在这里献丑了,我自己的线性表,是充分参考了两位大佬的代码敲才出来的垃圾代码,再次感到自己的手脑不协调,学习之路还漫漫无期。
此时已经是深夜,将这篇文章发完,还有无数个ddl在等着我,也不知看到这篇文章的你情况如何,但总之,为自己拼搏,永远是正确的。希望我们都能在这条正确的道路上愈行愈远!