⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maochilaoshu3.cpp

📁 这是一个猫吃老鼠算法问题。
💻 CPP
字号:
#include "iostream.h"

#include "malloc.h"
#include "conio.h"
#include "stdio.h"
#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();
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -