⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sparsematrix.c

📁 实现用三元组表示的稀疏数组的输入、转置、相加、输出。可以直接输入三元组
💻 C
字号:
#include <stdio.h>#define item inttypedef struct elem{  item value;  int row;  int col;  struct elem* next;};struct elem* getSpareMatrix(void){  struct elem* head=NULL,*rear=NULL;  head=rear=(struct elem*)malloc(sizeof(struct elem));  int value,row,col;  do{    printf("please input the data,(value,row,col)\n");    scanf("%d,%d,%d",value,row,col);    if(row>=0&&col>=0){      rear->next=(struct elem*)malloc(sizeof(struct elem));      rear=rear->next;      rear->value=value;      rear->row=row;      rear->col=col;    }  }while(row>=0&&col>=0);  struct elem*temp=head;  head=head->next;rear->next=NULL;free(temp);}struct elem* printSparseMatrix(struct elem* ptr){  while(ptr){    printf("%d\t",ptr->value);    printf("%d\t",ptr->row);    printf("%d\n",ptr->col);    ptr=ptr->next;  }}void transpose(struct elem* ptr){  int temp;  while(ptr){    temp=ptr->col;ptr->col=ptr->row;ptr->row=temp;    ptr=ptr->next;  }}void attach(item value, int row, int col, struct elem** ptr){  struct elem* temp;  temp=(struct elem*)malloc(sizeof(struct elem));  if(NULL==temp){    printf("malloc error!\n");    exit(1);  }  temp->value=value;  temp->row=row;  temp->col=col;  temp->next=NULL;  (*ptr)->next=temp;  *ptr=temp;}struct elem* addSparseMatrix(struct elem* a, struct elem* b){  int sum;  struct elem* front, *rear, *temp;  rear=(struct elem*)malloc(sizeof(struct elem));  if(NULL==rear){    printf("malloc error!\n");    exit(1);  }  front=rear;  for(;a;a=a->next)    attach(b->value,b->row,b->col,&rear);  rear->next=NULL;  temp=front;front=front->next;free(temp);  for(;b;b=b->next){    for(temp=front;temp;temp=temp->next){      if(b->row==temp->row && b->col==temp->col){        temp->value+=b->value;        break;      }    }    attach(b->value,b->row,b->col,&rear);  }  return front;}intmain(int argc, char* argv[]){  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -