Tennis ground management

本文介绍了一个基于链表实现的网球馆场地管理系统。该系统能够处理场地的分配与回收操作,支持用户申请连续的场地并归还场地。文章详细介绍了系统的数据结构设计与核心功能函数的实现,包括场地分配算法及场地归还后的合并策略。

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

有一个网球馆,有1到100个场地,设计一个管理系统,用于管理场地的分配与回收,每次可以申请一定长度的连续场地,如果有满足条件的场地则予以分配,如果没有则给予提示,归还场地的时候要提供当时分配的场地号和场地长度。

设计:使用链表来管理可分配的单位,每个链表包括两个数据项:一块连续的空闲场地的第一块场地的号码,该连续场地的长度。指针指向下一块连续的空闲场地。

编写了函数以提供以下操作:

creat:返回创建的链表的头指针。功能:输入数据项,创建一个链表。

inst:参数为空闲场地的号码和长度,返回链表的头指针。功能:将一块空闲的场地插入链表。

sot:参数为和返回值均为链表的头指针。功能:根据场地的长度,对链表进行排序,使用插入排序方法。

prnt:参数为链表的投递至。功能:将链表中的项打印出来。

TG_allocate:参数为要申请的场地的长度,返回链表的头指针。功能:从链表中查找符合条件的空闲场地,如果存在空闲场地的长度大于等于要申请的场地的长度,则予以分配,并提示分配成功,否则提示没有符合条件的场地。

TG_free:参数为链表的头指针,要归还的场地块的头号码,和场地块的长度。功能:将归还的场地块重新放入链表中,将链表中连续的空闲场地块进行合并,使之成为更大的场地块。

代码:

TennisGround.h

#ifndef TENNISGROUND_H_INCLUDED
#define TENNISGROUND_H_INCLUDED
typedef class TennisGround
{
public:
    TennisGround():n(1),l(100),next(NULL) {}
    int n,l;
    TennisGround *next;
}TG;

#endif // TENNISGROUND_H_INCLUDED

main.h

#include<iostream>
#include <stdlib.h>
#include<stdio.h>
#include "TennisGround.h"

using namespace std;
int start;

TG *TG_allocate(TG *p,int l)
{
    //int start;
    TG *h=p,*prev=p;
    while(p!=NULL)
    {
        if(l<p->l)
        {
            start=p->n;
            p->n=p->n+l;
            p->l=p->l-l;
            cout<<"allocated successfully"<<endl;
            cout<<"allocated num:   "<<start<<endl<<"length:   "<<l<<endl;
            return h;//return start;
        }
        else if(l==p->l)
        {
            start=p->n;
            if(h==p)
            {
                h=p->next;
               //delete p;//不可以使用delete原因未知。
                cout<<"allocated successfully"<<endl;
                cout<<"allocated num:   "<<start<<endl<<" length:  "<<l<<endl;
                return h;
            }
            else
            {
                prev->next=p->next;
                //delete p;
                cout<<"allocated successfully"<<endl;
                cout<<"allocated num:   "<<start<<endl<<" length:   "<<l<<endl;
                return h;
            }
        }
        else
            {prev=p;
            p=p->next;}
    }
        start=-1;
        cout<<"NO item matched"<<endl;
        return h;
}
TG *inst(TG *h,int num_bk,int l_bk)
{
    TG *p=new TG[1];
    p->l=l_bk;
    p->n=num_bk;
    p->next=h;
    h=p;
    return h;
}

TG *TG_free(TG *p, int num_bk,int l_bk)
{
    TG *h=p,*p1,*p2,*prev1=p,*prev=p;
    int s_old,e_old,s_new=num_bk,e_new=num_bk+l_bk-1;
    int flag=0;
    while(p!=NULL)
    {
        s_old=p->n;
        e_old=p->n+p->l-1;
        if (s_old==e_new+1)
        {
            flag+=1;
            p1=p;
            prev1=prev;
//            p->l+=l_bk;
//            p->n=num_bk;
        }
        if(e_old==s_new-1)
        {
            flag+=2;
            p2=p;
            //prev2=prev;
//            p->l+=l_bk;
        }
        prev=p;
        p=p->next;
    }
    if (flag==0)
    {
        h=inst(h,num_bk,l_bk);
    }
    if(flag==1)
    {
        p1->l+=l_bk;
        p1->n=num_bk;
    }
    if(flag==2)
    {
        p2->l+=l_bk;
    }
    if(flag==3)
    {
        p2->l+=p1->l+l_bk;
        if(p1==h)
            h=p1->next;
        else
            prev1->next=p1->next;
        //delete p1;//无法释放内存,是因为p1在该作用域中并非动态分配的吗?
    }
    cout<<"freed sucessfully"<<endl;
    return h;
}


TG *sot(TG *h)
{//使用插入排序。关键点:下一个待排序元素的确定 和 下一个待排序元素的前驱的确定。
    int n=0,key,i;
    TG *p=h,*p1=h->next,*prev1=h,*prev=h,*p1t;
    while(p!=NULL)
    {
        n++;
        p=p->next;
    }
    for(int j=2;j<=n;j++)
    {   p=h;
        key=p1->l;
        p1t=p1->next;//这里要先将 待排序元素 的后继(也就是下一个 待排序元素)保存,因为如果待排序元素 插入到前面的序列中,
        //它的后继不是下一个 待排序元素。p1->next为比p1大的那个元素。
        i=1;
        while(i<j&&(p->l<=key))
        {
            i++;
            prev=p;
            p=p->next;
        }
        if(p->l>key)
            {
                if(p==h)
                   {
                       h=p1;
                       prev1->next=p1->next;
                       p1->next=p;
                   }
                   else
                   {
                       prev->next=p1;
                       prev1->next=p1->next;
                       p1->next=p;
                   }
            }
            else
            {
                prev1=p1;//如果 待排序元素 的位置变化,则下个 待排序元素 的前驱 为 该元素的前驱。
                //如果待排序元素的位置不变,则下个 待排序元素 的前驱就是该元素。
            }
        p1=p1t;
    }
    return h;
}


void prnt(TG *h)
{
    if(h==NULL)
    {
        cout<<"NO item left"<<endl;
        return;
    }
    TG *p=h;
    while(p!=NULL)
    {
        cout<<p->n<<' '<<p->l<<endl;
        p=p->next;
    }
}

TG *creat()
{
    TG *h=new TG[1],*pt;
    int l,n;
    cout<<"please enter element(Ctrl+Z to finish)"<<endl;
    cin>>h->n>>h->l;
    h->next=NULL;
    pt=h;
    cout<<"please enter element(Ctrl+Z to finish)"<<endl;
    while(cin>>n>>l)
    {
        TG *p=new TG[1];
        p->n=n;
        p->l=l;
        pt->next=p;
        p->next=NULL;
        pt=p;
        cout<<"please enter element(Ctrl+Z to finish)"<<endl;
    }
    return h;
}
int main()
{//TG_allocate,TG_free,inst,sot,prnt,creat.
    int op,l,n;
    cout<<"please enter items to create a list"<<endl;
    TG *h=creat();
    cout<<"create successfully,the free items are:"<<endl;
    prnt(h);
    cout<<'\n'<<endl;
    system("pause");
    system("cls");
    while(1)
    {
        cin.clear();
        cout<<"------------------------------------------------------"<<endl;
        cout<<"the free items are"<<endl;
        prnt(h);
        cout<<"------------------------------------------------------"<<endl;
        cout<<"please choose the operation you want,enter the corresponding number"<<endl;
        cout<<"1 allcate    2 free    3 print    4 exit"<<endl;
        cin>>op;
        if(op==1)
        {
            cout<<"please enter the length you want to allocate"<<endl;
            cin>>l;
            h=TG_allocate(h,l);
        }
        else if(op==2)
        {
            cout<<"please enter the num and length you want to free"<<endl;
            cin>>n>>l;
            h=TG_free(h,n,l);
        }
        else if(op==3)
        {
            cout<<"the free items are:"<<endl;
            prnt(h);
        }
        else if(op==4)
        {
            return 1;
        }
        else
        {
            cout<<"please enter the right number"<<endl;
        }
        cout<<'\n'<<endl;
        system("pause");
        system("cls");
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值