改写要求1:改写为以单链表表示二维数组
#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::process(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; }
时间: 2024-11-04 05:18:46