日出东方,唯我不败! 做任何东西都是耐得住寂寞,任何一个行业要有十年以上的积累才能成为专家
用循环链表实现
/*有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面m个数,*/
#include<stdio.h>
#include<stdlib.h>
struct link
{ int data;
struct link *next,*nextt;
};
struct link *creatlink(); /*申请动态内存*/
void dislink(struct link *head);/*给链表赋值*/
void printlink(struct link *head);/*打印链表*/
main()
{
struct link *head,*p,*pr;
int i,j,n,m,c;
head=creatlink();
p=head;
printf("Please enter the length of the link:\n");
scanf("%d",&n);
for(i=1;i<n;i++)
{
p->next=creatlink();
p=p->next;
if(i==n-1)
p->next=head;
}
dislink(head); /*建立链表并赋值*/
printlink(head);
printf("\n\n\n");
printf("Please enter m:\n");
scanf("%d",&m);
p=head;
for(i=0;i<m;i++)
p=p->next;
head=p;
printlink(head);
printf("\n\n\n");
getch();
}
struct link *creatlink()/*申请动态内存*/
{
struct link *p;
p=(struct link *)malloc(sizeof(struct link));
if(p==NULL)
{
printf("Not enough room!");
exit(0);
}
else
{
p->data=20;
p->next=NULL;
}
return(p);
}
void dislink(struct link *head)/*给链表赋值*/
{
struct link *p=NULL;
int j=2;
p=head;
do
{
p=p->next;
if(j%2==0)
{ p->data=j/2;
goto m; }
p->data=j;
m: j=j+1;
}while(p!=head);
}
void printlink(struct link *head) /*打印链表*/
{
struct link *p;
int i=0;
p=head;
do
{
printf("%d: %d -> ",i,p->data);
p=p->next;
i++;
}while(p!=head);
}