拉丁方阵问题
#include<stdio.h>#include<stdlib.h>
typedef struct Node
{int data;
struct Node *next;
}node;
node *creat(int n)
{int i=1;
node *head=(node *)malloc(sizeof(node));
node *s,*p;
s=head;
node *rare=(node *)malloc(sizeof(node));
rare->next=head;
rare->data=n;
for(i=1;i<=n-1;i++)
{p=(node *)malloc(sizeof(node));
s->next=p;
p->next=rare;
p->data=i;
s=p;
}
rare->next=head->next;
free(head);
return(rare);
}
void printlist(node* rare,int n)
{int i=1;
rare=rare->next;
for(;i<n;i++)
{printf("%d ",rare->data);
rare=rare->next;
}
printf("\n");
}
int main()
{int n,i;
scanf("%d",&n);
node * rare=creat(n);
for(i=0;i<n;i++)
printlist(rare,n);
rare=rare->next;
}
C:\Documents and Settings\Administrator\桌面\1RFTO$7DL_ZIR4F8WOH04CB.png 依然若可 发表于 2016-1-23 11:50
。。。怎么发图。。 输出不对啊,求助啊 路过,学习一下! 这是我写的Latin方阵问题代码,给你参考下咯
include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}sqlist,*linklist;
linklist createlist(int n)
{
linklist head=NULL;
linklist r,s;
int i;
r=head;
for(i=1;i<=n;i++)
{
s=(linklist)malloc(sizeof(sqlist));
s->data=i;
if(head==NULL)
{
head=s;
}
else r->next=s;
r=s;
}
r->next=head;
return head;
}
void distroylist(linklist *list,int n)
{
linklist ptr=*list;
linklist buff;
int i=0;
while(i<n)
{
buff=ptr;
ptr=ptr->next;
}
for(i=0;i<n;i++)
{
free(buff);
}
*list=0;
}
void main()
{
linklist p;
int i,n;
while(1)
{
int j=0;
printf("please input number(limit 2~9) n:");
scanf("%d",&n);
printf("\n");
if(n<1||n>9)exit(0);
if(n==1)
{
printf("End of the program.\n");
break;
}
p=createlist(n);
while(j<n)
{
for(i=0;i<n;i++)
{
printf("%d\t",p->data);
p=p->next;
}
printf("\n");
p=p->next;
j++;
}
distroylist(&p,n);
}
}
页:
[1]