C ++ Programming Practice Guidance 1.10 two-dimensional array element transposition rewrite requirements implementation, Program Design Practice 1.10

Source: Internet
Author: User

C ++ Programming Practice Guidance 1.10 two-dimensional array element transposition rewrite requirements implementation, Program Design Practice 1.10

Rewrite requirement 1: rewrite to store two-dimensional arrays with single-chain tables and two-way linked lists

Rewrite requirement 2: add the function SingleLinkProcess () to swap the maximum node and header node locations in a single-link table, and the minimum and tail node locations.

Rewrite requirement 3: add the function DoubleLinkProcess () to implement interchange between the maximum node and the header node location in the two-way linked list.

# Include <cstdlib> # include <iostream> using namespace std; # define M 3 # define N 4 struct SingleLink {int Row; int Column; int Data; SingleLink * next ;}; struct DoubleLink {int Row; int Column; int Data; DoubleLink * prior; DoubleLink * next ;}; class Matrix {int a [M] [N]; public: singleLink * creatSingle (int a [] [N]); DoubleLink * creatDouble (int a [] [N]); void SingleLinkProcess (SingleLink * Shead ); void DoubleLinkProcess (DoubleLink * Dhead); void showSingle (SingleLink * Shead) {SingleLink * p = Shead-> next; int I = 0; while (p) {cout <p-> Data <'\ T'; I ++; if (I % 4 = 0) cout <endl; p = p-> next;} cout <endl;} void showDouble (DoubleLink * Dhead) {DoubleLink * p = Dhead-> next; int I = 0; while (p) {cout <p-> Data <'\ T'; I ++; if (I % 4 = 0) cout <endl; p = p-> next;} cout <endl;} void showDoublePrior (DoubleLink * Dhead) {DoubleLink * p = Dhead-> next; DoubleLink * t; while (p) {t = p; p = p-> next;} int I = 0; while (t) {cout <t-> Data <'\ T '; I ++; if (I % 4 = 0) cout <endl; t = t-> prior;} cout <endl ;}}; SingleLink * Matrix :: creatSingle (int a [] [N]) {SingleLink * Shead = new SingleLink; Shead-> next = NULL; SingleLink * p; p = Shead; int I, j; for (I = 0; I <M; I ++) for (j = 0; j <N; j ++) {SingleLink * newSingleLink = new SingleLink; newSingleLink-> next = NULL; newSingleLink-> Data = a [I] [j]; newSingleLink-> Row = I; newSingleLink-> Column = j; p-> next = newSingleLink; p = newSingleLink;} return Shead;} void Matrix: SingleLinkProcess (SingleLink * Shead) {SingleLink * max, * maxpre, * maxtail, * hpre, * htail, * head, * ppre; SingleLink * min, * minpre, * mintail, * tpre, * tail; SingleLink * p, * q; 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; int I = M * N-1; while (I) {tpre = p; p = p-> next; tail = p; 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 (int a [] [N]) {DoubleLink * Dhead = new DoubleLink; Dhead-> next = NULL; Dhead-> prior = NULL; doubleLink * p = Dhead; int I, j; for (I = 0; I <M; I ++) for (j = 0; j <N; j ++) {DoubleLink * newDoubleLink = new DoubleLink; 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;} return Dhead;} void Matrix:: DoubleLinkProcess (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) // determine whether the nodes are adjacent. {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 ;}} int main (int argc, char * argv []) {int B [M] [N] =, 12,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"); return EXIT_SUCCESS ;}

 

Related Article

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.