马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 pwnmelife 于 2019-1-6 16:57 编辑 #include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#define ElementType 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;
}
|