C + + Programming Practice Guide 1.10 two-dimensional array element transposition rewriting requirements implementation

Source: Internet
Author: User

Rewrite requirement 1: Rewrite to store two-dimensional arrays in single-linked lists and doubly-linked lists

Rewrite requirement 2: Add function Singlelinkprocess () implements the maximum node and head node position in the interchange single-linked list, minimum and trailing node locations

Rewrite requirement 3: Add function Doublelinkprocess () implements the maximum node and head node position in the interchange doubly linked list, the minimum and trailing node locations.

#include <cstdlib>#include<iostream>using namespacestd;#defineM 3#defineN 4structsinglelink{intRow; intColumn; intData; Singlelink*next;};structdoublelink{intRow; intColumn; intData; Doublelink*Prior; Doublelink*next;};classmatrix{intA[m][n];  Public: Singlelink* Creatsingle (intA[][n]); Doublelink* Creatdouble (intA[][n]); voidSinglelinkprocess (singlelink*shead); voidDoublelinkprocess (doublelink*dhead); voidShowsingle (singlelink*shead) {Singlelink* p=shead->Next; intI=0;  while(p) {cout<<p->Data<<'\ t'; I++; if(i%4==0) cout<<Endl; P=p->Next; } cout<<Endl; }             voidShowdouble (doublelink*dhead) {Doublelink* p=dhead->Next; intI=0;  while(p) {cout<<p->Data<<'\ t'; I++; if(i%4==0) cout<<Endl; P=p->Next; } cout<<Endl; }             voidShowdoubleprior (doublelink*dhead) {Doublelink* p=dhead->Next; Doublelink*T;  while(p) {T=p; P=p->Next; }                  intI=0;  while(t) {cout<<t->Data<<'\ t'; I++; if(i%4==0) cout<<Endl; T=t->Prior; } cout<<Endl; }}; Singlelink* Matrix::creatsingle (intA[][n]) {Singlelink* shead=NewSinglelink; Shead->next=NULL; Singlelink*p; P=Shead; inti,j;  for(i=0; i<m;i++)           for(j=0; j<n;j++) {Singlelink* newsinglelink=NewSinglelink; Newsinglelink->next=NULL; Newsinglelink->data=A[i][j]; Newsinglelink->row=i; Newsinglelink->column=J; P->next=Newsinglelink; P=Newsinglelink; }            returnShead;}voidMatrix::singlelinkprocess (singlelink*shead) {Singlelink* max,*maxpre,*maxtail,*hpre,*htail,*head,*Ppre; Singlelink* min,*minpre,*mintail,*tpre,*tail; Singlelink* p,*P; P=shead->Next; Max=p; Head=p; Hpre=Shead; Htail=head->Next; Ppre=p;  while(p) {if(max->data<p->Data) {Max=p; Maxpre=Ppre; Maxtail=p->Next; } ppre=p; P=p->Next; } hpre->next=Max; Maxpre->next=Head; Head->next=Maxtail; Max->next=Htail; P=shead->Next; inti=m*n-1;  while(i) {Tpre=p; P=p->Next; Tail=Q; I--; }P=shead->Next; Min=p; Ppre=p;  while(p) {if(min->data>p->Data) {min=p; Minpre=Ppre; Mintail=p->Next; } ppre=p; P=p->Next; } minpre->next=tail; Tpre->next=min; Tail->next=min->Next; Min->next=NULL;} Doublelink* Matrix::creatdouble (intA[][n]) {Doublelink* dhead=NewDoublelink; Dhead->next=NULL; Dhead->prior=NULL; Doublelink* p=Dhead; inti,j;  for(i=0; i<m;i++)      for(j=0; j<n;j++) {Doublelink* newdoublelink=NewDoublelink; Newdoublelink->prior=NULL; Newdoublelink->next=NULL; Newdoublelink->data=A[i][j]; Newdoublelink->row=i; Newdoublelink->column=J; P->next=Newdoublelink; Newdoublelink->prior=p; P=Newdoublelink; }     returnDhead;}voidMatrix::D oublelinkprocess (doublelink*dhead) {Doublelink* max,*maxpre,*maxtail,*hpre,*htail,*Head; Doublelink* min,*minpre,*mintail,*tail,*Tpre; Doublelink*p; P=dhead->Next; Head=p; Hpre=Dhead; Htail=head->Next; Max=p;  while(p) {if(max->data<p->Data) {Max=p; Maxpre=p->Prior; Maxtail=p->Next; } P=p->Next; } hpre->next=Max; Max->prior=Hpre; Max->next=Htail; Htail->prior=Max; Maxpre->next=Head; Head->prior=Maxpre; Head->next=Maxtail; Maxtail->prior=Head; P=dhead->Next;  while(p) {Tail=p; P=p->Next; } P=dhead->Next; Min=p; Tpre=tail->Prior;  while(p) {if(min->data>p->Data) {min=p; Minpre=p->Prior; Mintail=p->Next; } P=p->Next; }    if(tpre==min)//node is adjacent to be processed separately {Minpre->next=tail; Tail->prior=Minpre; Tail->next=min; Min->prior=tail; Min->next=NULL; }    Else{Minpre->next=tail; Tail->prior=Minpre; Tail->next=Mintail; Mintail->prior=tail; Tpre->next=min; Min->prior=Tpre; Min->next=NULL; }     }intMainintargcChar*argv[]) {    intb[m][n]={{1,4,5,6},{7,2,Ten, One},{8,9, A,3}}; Singlelink*Shead; Doublelink*Dhead;    Matrix Ma; Shead=Ma.creatsingle (b);    Ma.showsingle (Shead); Ma.    Singlelinkprocess (Shead);        Ma.showsingle (Shead); Dhead=ma.creatdouble (b);    Ma.showdouble (Dhead); Ma.    Doublelinkprocess (Dhead);    Ma.showdouble (Dhead); System ("PAUSE"); returnexit_success;}

C + + Programming Practice Guide 1.10 two-dimensional array element transposition 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.