拉丁方阵(单循环链表实现)

#include<stdio.h>
#include<stdlib.h>

typedef struct Node{
	int data;
	struct Node* next;
}Node,*LinkList;

void CreateSimpleCycleList_tail(LinkList *L,int number){ 
/* 创建一个单循环链表,没有头结点,尾指针指向第一个节点。
 * */
	int count;
	LinkList new,temp;
	*L = (LinkList)malloc(sizeof(struct Node));
	if(!(*L)){
		printf("Error:malloc\n");
		exit(1);
	}
	(*L)->next = *L;        	//初始化了链表
	for(count = 1; count <= number; count++ ){
		new = (LinkList)malloc(sizeof(struct Node));
		if(!new){
			printf("Error:malloc\n");
			exit(1);
		}
		new->data = count;
		new->next = (*L)->next;
		(*L)->next = new;
		*L = new;
	}				//创建了单循环链表,有头结点
	temp = (*L)->next;
	(*L)->next = temp->next;
	*L = temp->next;
	free(temp);			//将头结点删除	
}
void ShowLatinSquare(LinkList L,int number){
/*
 * 输出拉丁方阵:count_Out是外循环计数共number次(number是单链表的长度),
 * 是控制拉丁方阵的行数。count_In是内循环的次数,共number次,输出每一行。
 * */
	int count_Out = 1,count_In;
	LinkList temp = L;
	while(count_Out <= number){
		count_In = 1;
		while(count_In <= number){
			printf("%d ",L->data);
			count_In++;
			L = L->next;
		}
		printf("\n");
		L = L->next;		//输出完一行后,L要后移两个位置
					//但是48行代码已经移动一个,在这
					//后移一个即可。
		count_Out++;
	}
}
int main(){
	int order;
	LinkList L;
	printf("please enter the order of Latin Square: ");
	scanf("%3d",&order);
	CreateSimpleCycleList_tail(&L,order);
	ShowLatinSquare(L,order);
	return 0;
}

    原文作者:拉丁方阵问题
    原文地址: https://blog.csdn.net/robin_Xu_shuai/article/details/50859320
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞