Rewrite requirement 1: Rewrite to represent a two-dimensional array in a single-linked list
#include <cstdlib> #include <iostream>using namespace std; struct Linknode {int Row; int Column; int Data; Linknode *next; };class matrix{int m; int sum; Public:struct linknode* creat (int x[][40],int k) {m=k; Linknode *phead = new Linknode; Phead->next = NULL; linknode* p = phead; for (int i=0;i<k;i++) for (int j=0;j<k;j++) { linknode* pnewnode = new Linknode; Pnewnode->row = i; Pnewnode->column = j; Pnewnode->data = X[i][j]; Pnewnode->next = NULL; P->next = Pnewnode; p = pnewnode; } sum=0; return phead; } void Process (linknode* phead); void Show (linknode* phead) {int i=0; linknode* p = phead; p=p->next; while (p) {cout<<p->data<< "\ t"; p=p->next; i++; if (i%4==0) cout<<endl; } cout<< "sum=" <<sum<<endl; }};void MATRIX::p rocess (linknode* phead) {linknode* p = phead; p=p->next; while (p) {if ((p->row==p->column) | | | (p->row+p->column==m-1)) Continue sum+=p->data; }}int Main (int argc, char *argv[]) {int b[40][40]={{1,1,1,1},{1,2,2,1},{1,2,2,1},{1,1,1,1}}; Linknode *phead=new Linknode; MATRIX MTX; Phead=mtx.creat (b,4); Mtx.process (Phead); Mtx.show (Phead); System ("PAUSE"); return exit_success;}
C + + Programming practice Guidance 1.2 operation of two-dimensional array rewriting requirements implementation