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;

}

相关推荐
TheLegendMe7 小时前
贪心+线程安全单例
算法·哈希算法
豐儀麟阁贵7 小时前
8.5在方法中抛出异常
java·开发语言·前端·算法
胖咕噜的稞达鸭7 小时前
算法入门:滑动窗口--->找到字符串中所有的字母异位词,串联所有的子串,最小覆盖子串
数据库·redis·算法
小青龙emmm8 小时前
2025级C语言第二次周测(国教专用)题解
c语言·开发语言·算法
WolfGang0073218 小时前
代码随想录算法训练营Day28 | 509.斐波那契数列、70.爬楼梯、746.使用最小花费爬楼梯
算法
Boop_wu8 小时前
[Java EE] 多线程进阶(JUC)(2)
java·jvm·算法
闻缺陷则喜何志丹9 小时前
【SOSDP模板 容斥原理 逆向思考】3757. 有效子序列的数量|分数未知
c++·算法·力扣·容斥原理·sosdp·逆向思考
CoovallyAIHub9 小时前
如何在手机上轻松识别多种鸟类?我们发现了更简单的秘密……
深度学习·算法·计算机视觉
第二只羽毛9 小时前
遵守robots协议的友好爬虫
大数据·爬虫·python·算法·网络爬虫
艾斯比的日常10 小时前
Java 三色标记算法:并发垃圾回收的核心技术解析
java·开发语言·算法