猫吃老鼠问题

来源:互联网 发布:淘宝买家法律 编辑:程序博客网 时间:2024/04/29 00:33

#define len sizeof(struct catmouse)
struct catmouse
{int num;
struct catmouse *next;
};
void main()
{struct catmouse *head,*p,*q;
 int n,m,i,j;
 int a[7];
 printf("/nenter the an integer:");
 scanf("%d",&n);
 p=q=(struct catmouse*)malloc(len);
 p->num=1;
 head=p;
 for(i=2;i<=n;i++)      //建立循环链表
  { p=(struct catmouse*)malloc(len);
    p->num=i;
    q->next=p;
    q=p;
  }
  q->next=head;
  p=q=head;           //指向头节点,每个一个节点依次删去吃掉的老鼠
  for(i=0;i<n;i++)
   {
      p=q->next;
      q=p;
      p=q->next;
      q->next=p->next;
      free(p);
    }
   printf("/n最后一个号码是:%d",p->num);   //结果
   getch();
 }