我有 自己写的/************************************************************************/
/* 2015年11月7日11:53:57
功能:实现拉丁方阵
目的:由于是自己写的,那么就是练习一下自己的循环链表的操作与创建.*/
/************************************************************************/
# include<stdio.h>
# include<stdlib.h>
# include<stdbool.h>
typedef struct node
{
int data;
struct node * pNext;
}Node,*pNode;
pNode create_loop_list(int n);
void traverse_loop_list(pNode);
int main(void)
{
//pNode pHead;
int i = 0;
int n = 0;
//int *a = 0;
printf("请输入拉丁方阵的边数:n = ");
scanf("%d", &n);
printf("\n");
pNode pRear = create_loop_list(n);
//traverse_loop_list(pRear);
printf("拉丁方阵如下:\n");
for (i = 0; i < n ; ++i)
{
pRear = pRear->pNext;
traverse_loop_list(pRear);
}
return 0;
}
pNode create_loop_list(int n)
{
//int n = 0;
int i = 0;
int cunt = 0;
pNode pHead = (pNode)malloc(sizeof(Node));
if (NULL == pHead)
{
printf("\a内存分配失败!!");
exit(-1);
}
pNode pTail = pHead;
pTail->pNext = NULL;
//a = &n;
for (i = 0; i < n; ++i)
{
pNode pNew = (pNode)malloc(sizeof(Node));
if (NULL == pNew)
{
printf("\a内存分配失败!!");
exit(-1);
}
pNew->data = i+1 ;
pTail->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew;
}
pNode p = pHead;
pTail->pNext = p->pNext;
//pTail = p->pNext;
free(p);
p = NULL;
return pTail;
}
void traverse_loop_list(pNode pRear)
{
pNode p = pRear;
pNode q = pRear->pNext;
printf("%2d", p->data);
while (p != q)
{
printf("%3d",q->data);
q = q->pNext;
}
printf("\n");
}
/************************************************************************/
/* 在VS2013中程序运行如下:
请输入拉丁方阵的边数:n = 9
拉丁方阵如下:
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8
请按任意键继续. . . */
/************************************************************************/
|