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

#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;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值