2024.9.3

#include <iostream>
#include <cstring>
using namespace std;

class Stack
{
private:
    int len;
    int count = 0;
    int *stack;
public:
    Stack():len(10)             //无参构造
    {
        stack = new int[len];
        stack[len] = {0};
    }
    Stack(int len):len(len)         //有参构造
    {
        stack = new int[len];
        stack[len] = {0};
    }
    Stack(Stack &other):len(other.len)  //拷贝构造函数
    {
        stack = new int[len];
        for(int i=0;i<len;i++)
        {
            stack[i] = other.stack[i];
        }
    }
    Stack &operator=(const Stack &other)    //拷贝赋值函数
    {
        if(this != &other)
        {
            this->count = other.count;
            this->len = other.len;
            int *newstack = new int[this->len];
            for(int i=0;i<len;i++)
            {
                newstack[i] = other.stack[i];
            }
            delete [] stack;
            stack = newstack;

        }
        return *this;
    }
    ~Stack()        //析构函数
    {
        delete [] stack;
    }
    int top();
    bool empty();
    int size();
    int push(int n);
    void expand();
    void show();
    int pop();
};

int Stack::top()
{
    if(empty())
    {
        cout<<"error" <<endl;
        return -1;
    }
    return stack[count-1];
}

bool Stack::empty()
{
    return count == 0;
}

int Stack::size()
{
    return count;
}

int Stack::push(int n)
{
    if(count == len)
    {
        expand();
    }
    stack[count++] = n;
    return 0;
}

void Stack::expand()
{
    len = len * 2;
    int *newstack = new int[len];
    for(int i=0;i<count;i++)
    {
        newstack[i] = stack[i];
    }
    delete [] stack;
    stack = newstack;
}

void Stack::show()
{
    for(int i=count-1;i>=0;i--)
    {
        cout<<stack[i]<<" ";
    }
    cout<<endl;
}

int Stack::pop()
{
    if(empty())
    {
        cout<<"error"<<endl;
        return -1;
    }
    stack[--count] = 0;
    return 0;
}
class Queue
{
private:
    int len;
    int count = 0;
    int *queue;
public:
    Queue():len(10)
    {
        queue = new int[len];
        queue[len] = {0};
    };
    Queue(int n):len(n)
    {
        queue = new int[len];
        queue[len] = {0};
    };
    Queue(Queue &other):len(other.len)
    {
        queue = new int[len];
        for(int i=0;i<len;i++)
        {
            queue[i] = other.queue[i];
        }
    };
    Queue &operator=(const Queue &other)    //拷贝赋值函数
    {
        if(this != &other)
        {
            this->count = other.count;
            this->len = other.len;
            int *newqueue = new int[this->len];
            for(int i=0;i<len;i++)
            {
                newqueue[i] = other.queue[i];
            }
            delete [] queue;
            queue = newqueue;

        }
        return *this;
    }
    ~Queue()
    {
        delete []queue;
    };
    int front();
    int back();
    bool empty();
    int size();
    int push(int n);
    void pop();
    void expand();
    void show();
};
int Queue::front()
{
    if(empty())
    {
        cout<<"error"<<endl;
        return  -1;
    }
    return queue[count-1];
}
int Queue::back()
{
    if(empty())
    {
        cout<<"error"<<endl;
        return  -1;
    }
    return queue[0];
}
bool Queue::empty()
{
    return count == 0;
}
int Queue::size()
{
    return count;
}
int Queue::push(int n)
{
    if(count == len)
    {
        expand();
    }
    queue[count++] = n;
    return 0;
}
void Queue::pop()
{
    for(int i=0;i<count;i++)
    {
        queue[i] = queue[i+1];
    }
    count--;
}
void Queue::expand()
{
    len = len * 2;
    int *newqueue = new int[len];
    for(int i=0;i<count;i++)
    {
        newqueue[i] = queue[i];
    }
    delete [] queue;
    queue = newqueue;
}
void Queue::show()
{
    for(int i=0;i<count;i++)
    {
        cout<<queue[i]<<"  ";
    }
    cout<<endl;
}
int main()
{
    Stack s1(3);
    s1.push(1);
    s1.push(2);
    s1.push(3);
    s1.push(3);
    s1.pop();
    Stack s2;
    s2 = s1;
    s1.show();
    s2.show();
    cout<<s2.top()<<"   "<<s2.size()<<endl;
    cout<<"************"<<endl;
    Queue q1(3);
    q1.push(1);
    q1.push(2);
    q1.push(3);
    q1.show();
    q1.pop();
    q1.show();
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
IntelliJ IDEA 是一款强大的集成开发环境 (IDE),常用于Java和其他多种语言开发。版本 2024.1.3是一个假设的版本,但你可以按照以下步骤创建一个Java项目: 1. **打开IntelliJ IDEA**: 打开安装的 IntelliJ IDEA,确保你已经登录了账户(如果你有)。 2. **新建项目**: - **菜单选择**: 在欢迎界面或顶部菜单栏中,点击 "File"(文件) > "New"(新建)> "Project"(项目)。 3. **选择项目类型**: 在打开的 "Create New Project" 窗口中,选择 "Java" 作为项目类型,并选择 "Java" 作为模板。 4. **设置项目结构**: 为项目命名,可以选择创建空项目或者从现有源码导入。你可以选择 "From Sources",然后指定Java源代码所在的目录,或者选择 "Gradle" 或 "Maven" 如果你的项目依赖于构建工具。 5. **配置SDK**: 确认或选择你的Java版本,如果你已经安装了多个JDK,可以从下拉列表中选择。 6. **添加依赖和库**: 如果你的项目需要第三方库,可以在 "Libraries" 部分添加JAR文件或通过 Gradle或Maven管理依赖。 7. **配置模块**: 对于大型项目,可能需要创建模块。选择 "New Module" 并配置每个模块的属性。 8. **完成设置**: 点击 "Finish" 来创建项目。IDEA会为你创建项目的目录结构,设置编译器选项,并初始化必要的文件。 9. **启动项目**: 创建完成后,可以通过 "Run" 或 "Debug" 按钮启动新创建的Java应用。 **相关问题**: 1. IntelliJ IDEA支持哪些版本的Java? 2. 如何在IntelliJ IDEA中查看和编辑项目结构? 3. 创建Java项目时,如何管理依赖和插件? 4. IntelliJ IDEA有没有提供自动代码补全和错误检查的功能?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值