new、delete、以及queue类

本来以为很容易的,结果还是写了我两个小时。
用指针模拟queue类,再加上类,各种错误,总算是解决掉了--
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
class Item
{
private:
    int time;
    int cost;
public:
    Item():time(0),cost(0){}
    Item(int k):time(k)
    {
        cost=rand()%3;
    }
    Item (const Item &st)
    {
        time=st.time;
        cost=st.cost;
    }
    Item &operator=(const Item &st)
    {
        time=st.time;
        cost=st.cost;
        return *this;
    }
    int dealt()
    {
        return time;
    }
    int dealc()
    {
        //cout<<"------"<<cost<<endl;
        return cost;

    }
    friend ostream &operator<<(ostream &os,Item &st)
    {
        os<<st.time<<endl<<st.cost;
        return os;
    }
};
struct ss
{
    Item num;
    struct ss *next;
};
class Queue
{
private:
    struct ss *beg,*end;
    int cnt;
    int tolt;
    int size;
    int xx;
public:
    Queue():beg(NULL),end(NULL),cnt(0),tolt(0),size(10),xx(0){};
    Queue(const Queue &st)
    {
        cnt=st.cnt;
        tolt=st.tolt;
        size=st.size;
        while(beg!=NULL)
        {
            ss *p=beg->next;
            delete beg;
            beg=p;
        }
        end=NULL;
        ss *p=st.beg;
        beg=new ss;
        beg->next=NULL;
        beg->num=p->num;
        end=beg;
        while(p->next!=NULL)
        {
            p=p->next;
            ss *p1=new ss;
            p1->num=p->num;
            p1->next=NULL;
            end->next=p1;
            end=p1;
        }
        //return *this;
    }
    Queue &operator=(const Queue &st)
    {
        cnt=st.cnt;
        tolt=st.tolt;
        size=st.size;
        while(beg!=NULL)
        {
            ss *p=beg->next;
            delete beg;
            beg=p;
        }
        end=NULL;
        ss *p=st.beg;
        beg=new ss;
        beg->next=NULL;
        beg->num=p->num;
        end=beg;
        while(p->next!=NULL)
        {
            p=p->next;
            ss *p1=new ss;
            p1->num=p->num;
            p1->next=NULL;
            end->next=p1;
            end=p1;
        }
        return *this;
    }
    bool empty()
    {
        if(cnt==0) return true;
        else return false;
    }
    bool full()
    {
        if(cnt==size) return true;
        return false;
    }
    bool push(Item &st)
    {
        if(full()) return false;
        cnt++;
        if(beg==NULL)
        {
            ss *p=new ss;
            p->num=st;
            p->next=NULL;
            beg=end=p;
        }
        else
        {
            ss *p=new ss;
            p->num=st;
            p->next=NULL;
            beg->next=p;
            end=p;
        }
        //cout<<beg->num<<endl;
        return true;
    }
    bool pop()
    {
        if(empty()) return false;
        cnt--;
        xx++;
        if(tolt<beg->num.dealt())
        {
            tolt=beg->num.dealt()+beg->num.dealc();
        }
        else
        {
            tolt+=beg->num.dealc();
        }
        ss *p=beg->next;
        delete beg;
        beg=p;
        return true;
    }
    int top(int w)
    {
        int tmp=beg->num.dealt()+beg->num.dealc();
        //cout<<"---"<<beg->num.dealt()<<"   "<<beg->num.dealc()<<endl;
        if(tmp<=w) pop();
    }
    void deal(int n)
    {
        tolt-=n;
    }
    ~Queue()
    {
        while(beg!=NULL)
        {
            ss *p=beg->next;
            delete beg;
            beg=p;
        }
    }
    friend ostream &operator<<(ostream &os,const Queue &st)
    {
        os<<"处理花费的总时间(分钟):"<<st.tolt<<endl<<"处理了多少人:"<<st.xx<<endl;
        os<<"队列里面还有多少人:"<<st.cnt<<endl;
        return os;
    }
};
int main()
{
    Queue q;
    //int sum=1235;
    int n,m;
    cout<<"请输入n,m:";
    cin>>n>>m;
    if(n>m)
    {
        int tmp=n;
        n=m;
        m=n;
    }
    for(int i=n;i<=m;i++)
    {
        Item p(i);
        q.push(p);
        if(!q.empty())
        {
            q.top(i);
        }
    }
    q.deal(n);
    cout<<q;
    return 0;
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值