我的一个模拟内存分配的小程序,还存在些Bug,希望高手能够指点!

#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;
                }

            }
        
        }

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chandra

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值