Latin Square, Latin Square
The Latin square matrix is a matrix of n × n. There are EXACTLY n different elements in the square matrix, each of which has n elements, and each element exactly appears in one row and one column. The famous mathematician and physicist Euler used Latin letters as symbols of elements in the Latin square matrix, hence the name of the Latin square matrix.
Code Introduction: Use a single-loop linked list to implement the output of the Latin matrix.
When the first row is output, the first element is output to the last element of the cyclic single-chain table;
When the second row is output, the output starts from the second element and is output to the last element of the cyclic single-chain table, the first element in the output loop single-chain table (because each row has n elements );
Until the nth row is output, the last element is output first, and then the first element of the cyclic single-chain table is output to n-1 elements.
1 # include <stdio. h> 2 # include <stdlib. h> 3 4 typedef struct Node {5 int data; 6 struct Node * next; 7} Node, * LinkList; 8 9 void createsimpleincluelist_tail (LinkList * L, int number) {10/* create a single-cycle linked list with no header node. The tail Pointer Points to the first node. 11 **/12 int count; 13 LinkList new, temp; 14 * L = (LinkList) malloc (sizeof (struct Node); 15 if (! (* L) {16 printf ("Error: malloc \ n"); 17 exit (1); 18} 19 (* L)-> next = * L; // The linked list 20 for (count = 1; count <= number; count ++) {21 new = (LinkList) malloc (sizeof (struct Node) is initialized )); 22 if (! New) {23 printf ("Error: malloc \ n"); 24 exit (1); 25} 26 new-> data = count; 27 new-> next = (* L)-> next; 28 (* L)-> next = new; 29 * L = new; 30} // a single-cycle linked list is created, header node 31 temp = (* L)-> next; 32 (* L)-> next = temp-> next; 33 * L = temp-> next; 34 free (temp); // Delete the header node 35} 36 void ShowLatinSquare (LinkList L, int number) {37/* 38 * output the Latin matrix: count_Out is the total number of external cycle counts (number is the length of a single-chain table), and 39 * is the number of rows that control the Latin matrix. Count_In is the number of inner cycles, which is a total of number. Each row is output. 40 **/41 int count_Out = 1, count_In; 42 LinkList temp = L; 43 while (count_Out <= number) {44 count_In = 1; 45 while (count_In <= number) {46 printf ("% d", L-> data); 47 count_In ++; 48 L = L-> next; 49} 50 printf ("\ n "); 51 L = L-> next; // after a row is output, L needs to move two locations 52 // But 48 lines of code have been moved one by one. 54 count_Out ++; 55} 56} 57 int main () {58 int order; 59 LinkList L; 60 printf ("please enter the order of Latin Square :"); 61 scanf ("% 3d", & order); 62 createsimpletailelist_tail (& L, order); 63 ShowLatinSquare (L, order); 64 return 0; 65}