This article uses the classic magician licensing problem and the Latin array to explain the use of the circular linked list and one-way linked list respectively. as a classic in the algorithm, it is of great help to learn and understand the linked list, take a look. Magician licensing problem description: The magician uses 13 black cards in a deck to arrange them in advance and stack them together, with the cards facing down. Said to the audience: "I don't watch a card. I can guess what each card is. I can count it out loud. do you believe it? On-site demonstration this article uses the classic magician licensing problem and the Latin array to explain the use of the circular linked list and one-way linked list respectively, as the classic in the algorithm, it is of great help to learn and understand linked lists.
Magician licensing issues
Problem description:
The magician uses 13 black cards in a deck to arrange them in advance and stack them together. Said to the audience: "I don't watch a card. I can guess what each card is. I can count it out loud. do you believe it? On-site demonstration ." The magician turned the card number at the top to 1. he turned it to black peach A and put black peach A on the table. the second time, 1, and 2, he put the first card under these cards, turn over the second card, which is exactly the peach 2, and put it on the table so that all 13 cards will be turned out in turn, accurate.
Q: How is the starting order of cards arranged?
The code for the classic cyclic linked list is as follows:
# Include
Using namespace std; # define CardNumber 13 struct node {int data; struct node * next;} * linklist; void CreateLinkList () {linklist = NULL; node * p, * q; int I; p = linklist; for (I = 0; I
Data = 0; if (linklist = NULL) linklist = p; elseq-> next = p; q = p;} q-> next = linklist;} void Magician () {node * p = linklist; int j; int count = 2; p-> data = 1; while (1) {for (j = 0; j
Next; if (p-> data! = 0) {j -- ;}} if (p-> data = 0) {p-> data = count; count ++; if (count = 14) break ;}} void print () {node * p = linklist; while (p-> next! = Linklist) {cout <
Data <
Next;} cout <
Data <
Latin square matrix problems
Problem description:
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.
For example:
1 2 3
2 3 1
3 1 2
Q: How to construct the N-order Latin matrix? The general code is as follows: (all the Latin squares of level N)
# Include
Using namespace std; # define N 3/* determine N values */int main () {int I, j, k, t; printf ("The possble Latin Squares of order % d are: \ n", N); for (j = 0; j
Code for single-chain table implementation: (standard type of the Latin square matrix. Other types can be obtained using a single-chain table method similar to this)
# Include
# Include
Typedef struct node {int data; struct node * next;} linklist; linklist * CreateList (int n); // create the linked list linklist * CreateList (int n) {linklist * head, * s, * r; head = NULL; r = head; for (int I = 1; I <= n; I ++) {s = (linklist *) malloc (sizeof (node); s-> data = I; if (head = NULL) head = s; else r-> next = s; r = s ;} r-> next = head; return head;} int main () {linklist * L, * head; int n; printf ("Enter the order n of the Latin square matrix: "); sca Nf ("% d", & n); L = CreateList (n); head = L; for (int I = 1; I <= n; I ++) {L = head; for (int j = 1; j <I; j ++) {L = L-> next;} for (int k = 1; k <= n & L-> next! = NULL; k ++) {printf ("% 4d", L-> data); L = L-> next;} printf ("\ n ");} return 0 ;}