拉丁方针-循环链表解法
本帖最后由 pwnmelife 于 2019-1-6 16:57 编辑#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#defineElementType int
typedef struct Node {
ElementType data;
struct Node* next;
}Node, *LinkList;
void InitLoopLinkList(LinkList*, int);
void CreateLoopLinkList(LinkList* L, int n);
void OutputLatinSquare(LinkList* L, int n);
int main()
{
printf("Please input the number n\n");
int n;
scanf("%d", &n);
LinkList* L = (LinkList*)malloc(sizeof(LinkList));
InitLoopLinkList(L, n);
CreateLoopLinkList(L, n);
OutputLatinSquare(L, n);
return 0;
}
void InitLoopLinkList(LinkList* L, int n) {
(*L) = (LinkList)malloc(sizeof(Node));
(*L)->data = n;
(*L)->next = (*L);
}
void CreateLoopLinkList(LinkList* L, int n) {
if (n <= 0) {
return ;
}
LinkList p, r;
p = (*L);
for (int i = 1; i <= n; i++) {
r = (LinkList)malloc(sizeof(Node));
r->data = i;
r->next = p->next;
p->next = r;
p = r;
}
}
void OutputLatinSquare(LinkList* L, int n) {
LinkList p, q, r;
for (int i = 0; i < n; i++) {
r = (*L);
p = (*L)->next;
while (r != p) {
printf("%c ", p->data + '0');
q = p;
p = p->next;
}
printf("\n");
q->next = (*L)->next;
p = (*L)->next; // first node
r = p->next; // second node
p->next = (*L);
(*L)->next = r;
}
return;
}
页:
[1]