|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#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;
}
这是我写的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[n];
int i=0;
while(i<n)
{
buff[i++]=ptr;
ptr=ptr->next;
}
for(i=0;i<n;i++)
{
free(buff[i]);
}
*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);
}
}
|
|