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

📄 memorysaveanddestory.txt

📁 基本上实现了内存的分配与释放
💻 TXT
字号:
#include <stdio.h>
#include<stddef.h>
#include <malloc.h>

//#define NULL 0
#define LEN sizeof(struct MemAssign)


void menu();    //meun    
int Quit();    //exit


struct MemAssign...{
    int num;
    int startID;
    int size;
    struct MemAssign *next;    
};

int m,n,i=0;

  //----------------------------------------------------------------//

struct    MemAssign *creat1(void)...{

    struct    MemAssign *head1;
    struct    MemAssign *p1,*p2;
    p1=p2=(struct MemAssign *)malloc(LEN);
    n=0;        
    head1=NULL;
    return (head1);
}
//---------------------------------------------------------//
struct    MemAssign *creat2(void)...{
        
    struct    MemAssign *head2;
    struct    MemAssign *q0,*q1,*q2;
    q0=q1=q2=(struct MemAssign *)malloc(LEN);
    m=1;
    q0->startID=0;
    q0->size=600;
    q0->next=NULL;
    head2=q0;
    return (head2);        
}

//---------------------------------------------------------//


void print(struct    MemAssign *head1)...{

    struct    MemAssign * p;

    printf(" The number of allco Mem is : %d  ",n);
    printf(" Num   StartID   Size ");
    p=head1;
    if(head1!=NULL)
        do...{
            printf("  %d     %d   %d ",p->num,p->startID,p->size);
              p=p->next;
             }while(p!=NULL);
}
//---------------------------------------------------------//
void empty(struct    MemAssign *head2)...{

    struct    MemAssign * q;

    printf(" The number of free Mem is : %d  ",m);
    printf("   StartID   Size ");
    q=head2;
    if(head2!=NULL)
        do...{
            printf("     %d  %d ",q->startID,q->size);
            q=q->next;
            }while(q!=NULL);
}
//---------------------------------------------------------//
    

struct MemAssign *Free(struct MemAssign *head1,struct MemAssign *head2)
  ...{
    struct    MemAssign *p0,*p1,*p2;
    struct    MemAssign *q0,*q1,*q2;
    int num;
    printf(" Please input the num of Mem :");
    scanf("%d",&num);

    if(head1==NULL)...{
    printf(" There is no zuoye!!! ");
    return (head2);     }

    p1=head1;
    while(num!=p1->num&&p1->next!=NULL)...{p2=p1;p1=p1->next;}
    p0=p1->next;
    if(num==p1->num)...{
        char ch;
        printf("Are you sure to free (Y/N) ");
        scanf("%s",&ch);
   //---------------------------------------------------------//
            if(ch=='Y'||ch=='y')...{
                q0=p1;q1=head2;    
                if(q0->startID+q0->size==q1->startID&&q1->next==NULL)
                    ...{q1->size+= q0->size;q1->startID=q0->startID;goto end;}                    
                while(q1->next!=NULL)...{    
                    if(q1->startID+q1->size==q0->startID&&q0->startID+q0->size==q1->next->startID)...{
                        q1->size+=q0->size+q1->next->size;
                        q1->next=q1->next->next;
                        m=m-1;
                        goto end;
                    }
                    if(q1->startID+q1->size==q0->startID) ...{
                            q1->size+=q0->size;    
                            if(q1==head2)...{  head2=q1; }
                            else  q2->next=q1;
                    goto end;
                    }
                    else
                        if(q0->startID+q0->size==q1->startID) ...{
                                q1->startID=q0->startID;
                                q1->size+= q0->size;    
                                if(q1==head2)...{  head2=q1;     }
                                else  q2->next=q1;
                        goto end;
                    }                        
                else    q2=q1;q1=q1->next;                                        }
                q0=p1;q1=head2;    
                while(q1->startID<q0->startID)...{q2=q1;q1=q1->next;}                                
                if(head2==q1)...{    head2=q0;}
                else
                    q2->next=q0;q0->next=q1;
                m=m+1;
        end:        
                empty(head2);
        //---------------------------END------------------------//
                if(head1==p1)...{
                    if(p1->next==NULL)...{head1=NULL;i++;}
                    else
                        head1=p0; }    
                else
                    p2->next=p0;
                    n=n-1;
                    print(head1);    
            }else
                return (head2);
        }
    else...{        printf("The num of  %d  doesn't Exist",num);    }
    return(head2);
   }
//---------------------------------------------------------//
struct MemAssign *Alloc(struct MemAssign *head1,struct MemAssign *head2)
  ...{
    struct    MemAssign *p0,*p1,*p2;
    struct    MemAssign *q1,*q2;
    struct MemAssign *newMA;
    q1=newMA=(struct MemAssign *)malloc(LEN);

    printf(" Please Input the num  :");
    scanf("%d",&newMA->num);
    if (newMA->num<0)...{
    printf(" Please Input positive integer :");return Alloc(head1,head2);}
        
    printf(" Please input the size  :");
    scanf("%d",&newMA->size);
                
    p1=head1;
    p0=newMA;
    q1=head2;
        if(i==1&&p1->next==NULL)...{head1=NULL;i--;}
    if(q1->startID==0&&q1->next!=NULL&&p1->startID==0)
        ...{    head1=p1->next;    }
    p1=head1;
    while((q1->size<p0->size)&&(q1->next!=NULL))
        ...{    q2=q1;    q1=q1->next;    }
    while((q1->size<p0->size)&&(q1->next==NULL))
        ...{  printf(" Sorry! The size of you allco dosen't fit");  return (head1);}
        p0->startID=q1->startID;
        q1->startID+=p0->size;
        q1->size-=p0->size;
        if(head2==q1)  head2=q1;
        else  q2->next=q1;    
        if(q1->size==0)...{
            if(head2==q1)head2=q1->next;
            else  q2->next=q1->next;    
        m=m-1;
            }
        empty(head2);                
    if(head1==NULL)...{head1=p0;    head1->next=NULL;}
    else...{
        while(p1->next!=NULL)...{    p2=p1;    p1=p1->next;    }
            p1->next=p0;    p0->next=NULL;
            }
        n=n+1;
        print(head1);
        return (head1);
}

//---------------------------------------------------------//
        
    int Quit()...{    //exit
        printf(" ========= Thanks for using!========bye-bye!======== ");
        return 0;
        }

//---------------------------------------------------------//
        
    void menu()...{    //menu    
        printf(" ==================  menu  ================== ");
        printf("==================> A  Allco  ");
        printf("==================> F  Free  ");
        printf("==================> Q  Quit  ");
        printf(" Please select : ");
        }

//---------------------------------------------------------//

  
    int main()...{
        
        printf(" ===========  Initialize memory allocation operation  ========= ");
        struct    MemAssign *head1,*head2;
        head1=creat1();
        head2=creat2();
        print(head1);
        empty(head2);
        char m;
        while(1) 
            ...{    
            menu();     
            scanf("%s",&m);
        if (m=='Q')...{
                Quit();
                break;
                }
        switch(m)...{
        case 'A':    head1=Alloc(head1,head2);    break;
        case 'F':    head2=Free(head1,head2);    break;
        default:    printf(" ======> Error!! ======> Please click you operation "); break;
                }
            }        
        }

⌨️ 快捷键说明

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