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

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.xdnf.cn/news/12421.html

相关文章:

  • 腾讯云服务器端口怎么全部打开?CVM和轻量端口开通教程
  • 【杂谈】-吉卜力化(Ghiblified ) AI 图像:艺术与隐私的交织
  • 06.最长连续序列
  • 我的创作纪念日——聊聊我想成为一个创作者的动机
  • 总结这几个月来我和AI一起开发并上线第一个应用的使用经验
  • 数据融合是什么?进行数据融合的4大关键环节!
  • 前端面试准备-7
  • 从 Stdio 到 HTTP SSE,在 APIPark 托管 MCP Server
  • PasteForm(ABP)框架之实现更加灵活的类似多租户的归属过滤功能,比如只能查看自己的相关数据
  • Abaqus分析步与输出:
  • 【leetcode】347. 前k个高频元素
  • 利率的计量
  • VIN码车辆识别码解析接口如何用C#进行调用?
  • 如何利用Elastic Stack(ELK)进行安全日志分析
  • 常见串口种类介绍
  • 一、ES6-let声明变量【解刨分析最详细】
  • 右值引用和移动语义
  • 酷黑NBA足球赛事直播源码体育直播M39模板赛事源码
  • redis数据过期策略、淘汰策略
  • RADIUS-管理员获取共享密钥
  • 【CPU】英特尔酷睿Ultra 5 225H与Ultra7 258V(Lunar Lake架构)PK
  • [蓝桥杯]航班时间
  • 【.net core】天地图坐标转换为高德地图坐标(WGS84 坐标转 GCJ02 坐标)
  • 六、数据库的安全性
  • C++11 中 final 和 override 从入门到精通
  • 在 Spring Boot 中使用 JSP
  • 【实施指南】Android客户端HTTPS双向认证实施指南
  • 如何排查和解决PHP连接数据库MYSQL失败写锁的问题
  • Hive中ORC存储格式的优化方法
  • GC1809:高性能24bit/192kHz音频接收芯片解析