11.10作业

该篇博客介绍了数据结构中队列和栈的实现,包括它们的插入、删除操作以及满和空的判断。通过具体实例展示了如何使用队列和栈,如在主函数中创建并操作队列和栈,并创建了一个学生领导类,继承了学生和活动类,包含了姓名、年龄、分数、活动和专业等属性。
摘要由CSDN通过智能技术生成

1.队列

2.栈

#include<iostream>
#define MAX 20
typedef int datatype;
using namespace std;

class Queue
{
private:
    datatype data[MAX];
    int front;
    int tail;
public:
    Queue()
    {
        front=0;
        tail=0;
    }
    bool empty();
    bool full();
    int size();
    void push(datatype e);
    void pop();
    void show();

};

bool Queue::empty()
{
    return front==tail?true:false;
}
bool Queue::full()
{
    return (tail+1)%MAX==front ? true:false;
}
int Queue::size()
{
    return ((tail-front)+MAX)%MAX;
}
void Queue::push(datatype e)
{
    if(full())
    {
        cout<<"has full"<<endl;
        return ;
    }
    else
    {
        data[tail]=e;
        tail=(tail+1)%MAX;
    }
}
void Queue::pop()
{
    if(empty())
    {
        cout<<"was empty"<<endl;
        return;
    }
    else
    {
        front=(front+1)%MAX;
    }
}
void Queue::show()
{
    if(empty())
    {
        return ;
    }
    for(int i=front;i!=tail;i=(i+1)%MAX)
    {
        cout<<data[i]<<" ";
    }
    cout<<endl;
}
class Stack
{
private:
    datatype data[MAX];
    int top;
public:
    Stack()
    {
        top=-1;
    }
    bool empty();
    bool full();
    int size();
    void push(datatype e);
    void pop();
    void show();

};
bool Stack::empty()
{
    return top==-1?true:false;
}
bool Stack::full()
{
    return top>=MAX?true:false;
}
int Stack::size()
{
    return top+1;
}
void Stack::push(datatype e)
{
    if(full()){cout<<"has full"<<endl;return;}
    top++;
    data[top]=e;
    if(top>MAX){cout<<"already full"<<endl;}
}
void Stack::pop()
{
    if(empty()){cout<<"empty"<<endl;return;}
    top--;
}
void Stack::show()
{
    if(empty()){cout<<"empty"<<endl;return;}
    for(int i=0;i<top;i++)
    {
        cout<<data[i]<<" ";
    }
    cout<<endl;
}
class student
{
private:
    string m_name;
    int m_age;
    double m_score;
public:
    student(){}
    student(string name,int age,double score):m_name(name),m_age(age),m_score(score){}
    void show()
    {
        cout<<"name ="<<m_name<<" age= "<<m_age<<" score= "<<m_score<<endl;
    }

};
class party
{
private:
    string m_party_activty;
    string m_org;
public:
    party(){}
    party(string party_act,string org):m_party_activty(party_act),m_org(org){}
    void show()
    {
        cout<<"party_activty ="<<m_party_activty<<" org= "<<m_org<<endl;
    }
};
class student_lea: public party,public student
{
private:
    string m_pro;
public:
    student_lea(){}
    student_lea(string name,int age,double score,string party_activty,string org,string pro)
   :student(name,age,score),party(party_activty,org),m_pro(pro){}
    void show()
    {
      student::show();
      party::show();
      cout<<"pro= "<<m_pro<<endl;
    }
};

int main()
{
    Queue q1;
    q1.push(10);
    q1.push(9);
    q1.push(8);
    q1.push(7);
    q1.pop();
    q1.push(6);
    q1.show();
    Stack s1;
    s1.push(10);
    s1.push(10);
    s1.push(10);
    s1.push(10);
    s1.push(10);
    s1.push(10);
    s1.push(10);
    s1.show();
    student_lea s5("hah",18,96.3,"11","22","33");
    s5.show();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值