C + + Programming practice Guidance 1.2 operation of two-dimensional array rewriting requirements implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.