当前位置: 首页 > news >正文

exp1_code

#include <iostream>
using namespace std;

// 链栈节点结构
struct StackNode {
    int data;
    StackNode* next;
    StackNode(int val) : data(val), next(nullptr) {}
};

// 顺序栈实现
class SeqStack {
private:
    int* data;
    int top;
    int capacity;
public:
    SeqStack(int size = 10) : capacity(size), top(-1) {
        data = new int[capacity];
    }
    
    ~SeqStack() {
        delete[] data;
    }
    
    bool isEmpty() const {
        return top == -1;
    }
    
    bool isFull() const {
        return top == capacity - 1;
    }
    
    bool push(int val) {
        if (isFull()) return false;
        data[++top] = val;
        return true;
    }
    
    bool pop(int& val) {
        if (isEmpty()) return false;
        val = data[top--];
        return true;
    }
    
    bool getTop(int& val) const {
        if (isEmpty()) return false;
        val = data[top];
        return true;
    }
};

// 链栈实现
class LinkStack {
private:
    StackNode* topNode;
public:
    LinkStack() : topNode(nullptr) {}
    
    ~LinkStack() {
        while (topNode) {
            StackNode* temp = topNode;
            topNode = topNode->next;
            delete temp;
        }
    }
    
    bool isEmpty() const {
        return topNode == nullptr;
    }
    
    void push(int val) {
        StackNode* newNode = new StackNode(val);
        newNode->next = topNode;
        topNode = newNode;
    }
    
    bool pop(int& val) {
        if (isEmpty()) return false;
        val = topNode->data;
        StackNode* temp = topNode;
        topNode = topNode->next;
        delete temp;
        return true;
    }
    
    bool getTop(int& val) const {
        if (isEmpty()) return false;
        val = topNode->data;
        return true;
    }
};

// 循环队列实现
class CirQueue {
private:
    int* data;
    int front;
    int rear;
    int capacity;
public:
    CirQueue(int size) : capacity(size + 1), front(0), rear(0) {
        data = new int[capacity];
    }
    
    ~CirQueue() {
        delete[] data;
    }
    
    bool isEmpty() const {
        return front == rear;
    }
    
    bool isFull() const {
        return (rear + 1) % capacity == front;
    }
    
    bool enQueue(int val) {
        if (isFull()) return false;
        data[rear] = val;
        rear = (rear + 1) % capacity;
        return true;
    }
    
    bool deQueue(int& val) {
        if (isEmpty()) return false;
        val = data[front];
        front = (front + 1) % capacity;
        return true;
    }
    
    int getLength() const {
        return (rear - front + capacity) % capacity;
    }
};

// 测试栈功能
void testStacks() {
    cout << "测试顺序栈:" << endl;
    SeqStack seqStack(5);
    for (int i = 1; i <= 5; i++) {
        seqStack.push(i);
    }
    int val;
    while (seqStack.pop(val)) {
        cout << val << " ";
    }
    cout << endl;
    
    cout << "测试链栈:" << endl;
    LinkStack linkStack;
    for (int i = 1; i <= 5; i++) {
        linkStack.push(i);
    }
    while (linkStack.pop(val)) {
        cout << val << " ";
    }
    cout << endl;
}

// 解决约瑟夫环问题(仅输出安全位置)
void solveJosephus() {
    const int n = 41;  // 总人数
    const int m = 3;   // 报数间隔
    const int survivors = 2;  // 最后存活人数
    
    CirQueue queue(n);
    for (int i = 1; i <= n; i++) {
        queue.enQueue(i);
    }
    
    int count = 0;
    int out;
    
    // 淘汰过程,直到剩下survivors个人
    while (queue.getLength() > survivors) {
        queue.deQueue(out);
        count++;
        if (count % m == 0) {
            // 移除淘汰顺序输出
        } else {
            queue.enQueue(out);
        }
    }
    
    // 输出最后存活的位置
    cout << "约瑟夫和他的朋友选择的安全位置是:" << endl;
    while (!queue.isEmpty()) {
        queue.deQueue(out);
        cout << out << " ";
    }
    cout << endl;
}

int main() {
    // 测试栈实现
    testStacks();
    
    // 解决约瑟夫环问题
    solveJosephus();
    
    return 0;
}

http://www.lqws.cn/news/143965.html

相关文章:

  • Redis 缓存策略:借助缓存优化数据库性能并保障数据一致性
  • 9.axios底层原理,和promise的对比(2)
  • 外网访问内网服务器常用的三种简单操作步骤方法,本地搭建网址轻松让公网连接
  • 游戏设计模式 - 子类沙箱
  • OCR助力保险业建设
  • 【C++】二叉搜索树
  • ocrapi服务docker镜像使用
  • 从零开始的云计算——番外实战,iptables防火墙项目
  • WordZero:让Markdown与Word文档自由转换的Golang利器
  • 【Go语言基础【2】】数据类型之基础数据类型:数字、字符、布尔、枚举、自定义
  • 1、Go语言基础中的基础
  • 【Go语言基础【四】】局部变量、全局变量、形式参数
  • PPT转图片拼贴工具 v3.0
  • Spark 写文件
  • Dubbo Logback 远程调用携带traceid
  • 41道Django高频题整理(附答案背诵版)
  • PostgreSQL 的扩展pg_prewarm
  • 20250605在微星X99主板中配置WIN10和ubuntu22.04.6双系统启动的引导设置
  • Django CMS 的 Demo
  • NoSQL之Redis配置与优化
  • SQL Server相关的sql语句
  • 嵌入式学习 D33:系统编程--网路编程
  • ubuntu 端口复用
  • Ubuntu20.04设置为开机后直接自动进入纯命令行界面
  • 【Linux】为 Git 设置 Commit 提交模板方法,可统一个人或者项目的提交风格
  • 【Git系列】如何同步原始仓库的更新到你的fork仓库?
  • Excel-vlookup -多条件匹配,返回指定列处的值
  • [测试_10] Selenium IDE | cssSelector | XPath | 操作测试
  • Haproxy的基础配置
  • DeepSeek 助力 Vue3 开发:打造丝滑的日历(Calendar),日历_天气预报日历示例(CalendarView01_18)