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

📄 space.c

📁 主存的分配和回收的实现与主存储器的管理方式有关的
💻 C
字号:
#include "stdio.h"
#include "conio.h"
#include "malloc.h"
#define NULL 0
#define NAME 0
#define TRUE 1
#define FALSE 0
#define LEN sizeof(space)
typedef struct space
{
    int name;
    int first;
    int size;
    int flag;
    struct space *next;
}space;
/*创建一个内存空间*/
space * create()
{  space *head,*p;
    int size;
   p=(space *)malloc(LEN);
   printf("please input the size of your space:");
   scanf("%d",&p->size);
   p->name=NAME;
   p->first=0;
   p->flag=FALSE;
   p->next=NULL;
   head=p;
   return head;
}
/*插入作业*/
space * insert(space *head)
{
  space *p,*sp;
  sp=(space *)malloc(LEN);
  p=head;
  printf("input your WorkSpace:");
  scanf("%d %d",&sp->name,&sp->size);
  sp->flag=TRUE;
  for(p;p!=NULL;p=(p->next))
  {
     if(p->size==sp->size && p->flag==FALSE)
    {
        p->name=sp->name;
        p->flag=TRUE;
        return head;
    }
    if((p->size)>(sp->size) && p->flag==FALSE)
    {
        int size=p->size;

         p->name=sp->name;
        p->size=sp->size;
        p->flag=TRUE;

        sp->name=NAME;
        sp->first=p->first+sp->size;
        sp->size=size-(sp->size);
        sp->flag=FALSE;
        sp->next=p->next;
        p->next=sp;
        return head;
    }
  }
  printf("there is no space for this work;");
  return head;
}
/*查看是否能合并并合并*/
space * dealBlack(space *head)
{
    space *p,*temp;
    p=head;
    if(head->flag==FALSE && (head->next)->flag==FALSE)
    {
        (head->next)->first=0;
        (head->next)->size=head->size + (head->next)->size;
         head=head->next;
     return head;
    }

    for(p;p!=NULL;p=(p->next))
    {
           if(p->flag==FALSE && (p->next)->flag==FALSE)
             {
               (p->next)->first=p->first;
               (p->next)->size=p->size + (p->next)->size;
               temp->next=p->next;
               return head;
             }
             temp=p;
    }
}

/*释放空间*/
space * freeSpace(space *head)
{
    int name;
    space *p;
    p=head;
    printf("please input the name of freeSpace:");
    scanf("%d",&name);
        for(p;p!=NULL;p=p->next)
        {
           if(p->name==name)
           {
                p->name=NAME;
                p->flag=FALSE;
                return head;
           }

        }

        printf("there is no this name work;");
        return head ;
}

/*输出结果状态*/
void output(space *head)
{
   space *p;
   p=head;
   /*if(head!=NULL)
   {
    do
    {
      printf("Name:%d First:%d Size:%d Flag:%d\n",p->name,p->first,p->size,p->flag);
      p=p->next;
    }while(p!=NULL);
   }  */
   while(p!=NULL)
   {
      printf("Name:%d First:%d Size:%d Flag:%d\n",p->name,p->first,p->size,p->flag);
      p=p->next;
   }
}
/*主函数*/
main()
{
    space *head;
    char choice;
    head=create();
    printf("Please input your choice,\n(Insert:I FreeSpace:F Output:O Exit: E):\n");
    while (TRUE)
    {
        scanf("%c",&choice);
       if(choice=='I')
       {
         head=insert(head);
         head=dealBlack(head);

       }
       else if (choice=='F')
       {
          head=freeSpace(head);
          head=dealBlack(head);
          head=dealBlack(head);
       }
        else if (choice=='O')
       {

          output(head);
       }
        else if (choice=='E')
       {
          break;
       }
       else
       {
         printf("********************");
       }
    }
}

⌨️ 快捷键说明

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