jingsg-1

本文介绍了一个基于C语言实现的循环链表游戏程序。通过随机生成数字并构建循环链表,玩家可以使用a键向右移动、b键向左移动,并通过空格键删除当前选中的数字。该程序涉及链表的创建、遍历与删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include"stdio.h"
#include"stdlib.h"
#include"time.h"
int count;//定义发的数字个数
//定义循环链表
struct numSave
{
 int num;//要参与游戏的数字
    struct numSave *left;//指向当前数字的左边的数字
    struct numSave *right;//指向当前数字的右边的数字
};
//初始化要参与的游戏数字,及建立链表
struct numSave * inintp()
{
 struct numSave *l,*c,*head;//l指向左边的指针,r指向右边的指针,head返回的指针
    int i=0;//循环变量
 srand((unsigned)time(NULL));//初始化随机的函数rand();
 for(i = 0 ; i < count; i++)
 {
     c= (struct numSave *)malloc(sizeof(struct numSave *));//分配资源,在C++中mallow相当于new
  if(c==NULL)//判断分配内存是否成功
  {
   printf("初始化不成功");
   return NULL;
  }
 
  c->num = rand()%100;//随机产生1-100的数字
  if(i == 0)
  {
      l=head=c;//初始化head
  }else
  {
   l->right=c;
   c->left = l;
  }
  if(i==count-1)
  {
   c->right = head;
   head->left = c;
   return head;
  }
  l = c;
 }
 return NULL;
}
//向右移动(按a键)
void printR(struct numSave *lTemp,struct numSave *temp)
{
   struct numSave *head = temp;//head获取原始链表的值
       int i;
    printf("/n");
   for( i=0; i < count; i++)
   {
    if(lTemp == head)
    {
     if(i==count-1)
     {
      printf("(%d)",head->num);
     }else
     {
      printf("(%d)----",head->num);
     }
    }else
    {
     if(i==count-1)
     {
                  printf("%d",head->num);
     }else
     {
      printf("%d----",head->num);
     }
    }
    head = head->right;
   }
}
//向右移动(按b键)
void printL(struct numSave *lTemp,struct numSave *temp)
{
   struct numSave *head = temp;//head获取原始链表的值
       int i;
    printf("/n");
   for( i=0; i < count; i++)
   {
    if(lTemp == head)
    {
     if(i==count-1)
     {
      printf("(%d)",head->num);
     }else
     {
      printf("(%d)----",head->num);
     }
    }else
    {
     if(i==count-1)
     {
                  printf("%d",head->num);
     }else
     {
      printf("%d----",head->num);
     }
    }
    head = head->right;
   }
}
//删除(space)
struct numSave * deleteNum(struct numSave *temp,struct numSave *p)
{
    struct numSave *head,*rep;
 int i = 0;
 if(temp == p)
 {
        rep = p->right;
  rep->left = p->left;
  (p->left)->right = rep;
  //free(p); 这里应该加这一句,我加了这句老出错,你去找一下原因,相当于C++中的delete
  count--;
  return rep;
 }
 head = p;
 for(i ; i< count; i++)
 {
  if(head = temp)
  {
   (head->left)->right = head->right;
   (head->right)->left = head->left;
   //free(head)这里应该加这一句,我加了这句老出错,你去找一下原因,相当于C++中的delete
   count--;
   break;
  }
  head = head->right;
 }
 return p;
}
//打印出初始化的数字
void print(struct numSave *p)
{
 int i = 0;
 printf("/n");
 for(i=0 ;  i < count; i++)
 {
  if(i == 0 && count!=1)
  {
   printf("(%d)----",p->num);
  }else
  {
   if(i==count-1)
   {
    printf("%d",p->num);
   }else
   {
    printf("%d----",p->num);
   }
  }
  p = p->right;
 }
}


//mian()
void main()
{
  struct numSave *p,*temp;
  int i = 0;
  char intputChar;
  p=NULL;
  temp = NULL;
  printf("/n一需要多少数字参与:");
  scanf("%d",&count);
  p = inintp();
  temp = p;
     print(temp);
  printf("/n");
  temp = p;
  while(i < count)
  {
  
          switch (intputChar =getch())
    {
    case 'a':
     temp = temp->right;
              printR(temp,p);
     break;
    case 'b':
     temp = temp->left;
              printL(temp,p);
     break;
    case ' ':
   
     p=deleteNum(temp,p);
     temp = p;
              print(temp);
     break;
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值