用十字链表构造一个row行、line列的矩阵。加入cnt个元素,删除某个元素,输出矩阵内元素。
程序中设计成了循环链表,具体循环链表有什么优势我还不知道。
#ifndef MATRIX_H
#define MATRIX_H
#include <stdbool.h>
typedef struct node Node;
typedef struct node{ //声明矩阵节点的结构体,包含行、列、值、右和下指针域
int row;
int line;
union{ //用联合union来存放数值或者指针,头结点为指针
int value;
Node *next;
}matter;
Node *right; //指向右边节点的指针
Node *down; //指向下面节点的指针
}Node;
typedef struct matrix{ //声明矩阵入口的结构体,包含总行、总列、总元素个数和指向头结点的指针
int row;
int line;
int cnt;
Node *phead;
}Matrix;
#endif
bool creat_matrix(Matrix *pM); //构造一个包含头结点的矩阵
bool insert_matrix(Matrix *pM,Node *pNew); //向矩阵内插入元素
bool dele_matrix(Matrix *pM,int row,int line,int *value); //删除指定位置的元素
void show_matrix(Matrix *pM); //输出矩阵内的元素
void clear_matrix(Matrix *pM); //清空矩阵
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "Matrix.h"
bool creat_matrix(Matrix *pM){ //构造矩阵
bool res=true;
for(int i=1;i<=pM->row||i<=pM->line;i++){ //初始化矩阵的头结点
Node *p=pM->phead;
Node *pNew=(Node*)malloc(sizeof(Node));
if(!pNew){
printf("内存申请不成功,矩阵初始化失败");
res=false;
}else{
pNew->row=pNew->line=0;
pNew->matter.next=pNew->right=pNew->down=NULL;
if(i>pM->row){
pNew->row=0;
}else{
pNew->row=i; //记录头结点的行
}
if(i>pM->line){
pNew->line=0;
}else{
pNew->line=i; //记录头结点的列
}
if(!p){ //p为空,添加第一个头结点
pM->phead=pNew;
pNew->matter.next=pM->phead; //循环
}else{
p=pM->phead;
Node *q=p;
for(q=p;q->matter.next!=p;q=q->matter.next);//找到最后一个头结点
q->matter.next=pNew; //新头结点接入最后一个头结点后面
pNew->matter.next=p; //循环
}
}
}
// {
// Node *p=pM->phead;
// int i=1;
// for(p=pM->phead,i=1;p!=pM->phead||i==1;p=p->matter.next,i++){
// printf("head%d 行%d 列%d\n",i,p->row,p->line);
// }
// }
return res;
}
bool insert_matrix(Matrix *pM,Node *pNew){ //插入元素
bool res=true;
if(pNew->row<0||pNew->row>pM->row||pNew->line<0||pNew->line>pM->line){
res=false;
}else{
Node *p=pM->phead;
for(int i=1;i<pNew->row;i++,p=p->matter.next);
if(p->right==NULL){
p->right=pNew;
pNew->right=p;
}else{
Node *q=p->right,*temp=p;
for(q=p->right;q->line<pNew->line&&q->right!=p;temp=q,q=q->right);
if(q->line==pNew->line){
printf("此位置已存在数据\n");
res=false;
}else if(q->line>pNew->line){
pNew->right=temp->right;
temp->right=pNew;
}else{
q->right=pNew;
pNew->right=p;
}
}
p=pM->phead;
for(int i=1;i<pNew->line;i++,p=p->matter.next);
if(p->down==NULL){
p->down=pNew;
pNew->down=p;
}else{
Node *q=p->down,*temp=p;
for(q=p->down;q->row<pNew->row&&q->down!=p;temp=q,q=q->down);
if(q->row==pNew->row){
printf("此位置已存在数据\n");
res=false;
}else if(q->row>pNew->row){
pNew->down=temp->down;
temp->down=pNew;
}else{
q->down=pNew;
pNew->down=p;
}
}
}
return res;
}
bool dele_matrix(Matrix *pM,int row,int line,int *value){ //删除元素
bool res=true;
if(row<0||row>pM->row||line<0||line>pM->line){
printf("位置不合法\n");
res=false;
}else{
Node *p=pM->phead;
for(int i=1;i<row;i++,p=p->matter.next);
Node *q=p->right,*temp=p;
if(p->right){
for(q=p->right;q->line<line;temp=q,q=q->right);
if(q->line>line){
printf("此位置无元素\n");
res=false;
}else{
temp->right=q->right;
for(temp=q->down;temp->down!=q;temp=temp->down);
temp->down=q->down;
*value=q->matter.value;
printf("%d\n",*value);
free(q);
}
}else{
printf("此位置无元素\n");
res=false;
}
}
return res;
}
void show_matrix(Matrix *pM){
Node *p=pM->phead;
int cnt=0;
do{
if(p->right!=p&&p->right){
cnt++;
for(Node *q=p->right;q!=p;q=q->right){
printf("%d行 %d列 %d ",q->row,q->line,q->matter.value);
}
printf("\n");
}
p=p->matter.next;
}while(p!=pM->phead);
if(cnt==0){
printf("矩阵为空");
}
}
void clear_matrix(Matrix *pM){
Node *p=pM->phead;
do{
if(p->right){
for(Node *q=p->right;q!=p;q=p->right){
p->right=q->right;
free(q);
}
p->right=NULL;
printf("矩阵第%d行已空\n",p->row);
}
p=p->matter.next;
}while(p!=pM->phead);
}
#ifndef MATRIX_H
#define MATRIX_H
#include <stdbool.h>
typedef struct node Node;
typedef struct node{
int row;
int line;
union{
int value;
Node *next;
}matter;
Node *right;
Node *down;
}Node;
typedef struct matrix{
int row;
int line;
int cnt;
Node *phead;
}Matrix;
#endif
bool creat_matrix(Matrix *pM);
bool insert_matrix(Matrix *pM,Node *pNew);
bool dele_matrix(Matrix *pM,int row,int line,int *value);
void show_matrix(Matrix *pM);
void clear_matrix(Matrix *pM);