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 intcapacity;

}

~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 = datatop--;

return true;

}

bool getTop(int& val) const {

if (isEmpty()) return false;

val = datatop;

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 intcapacity;

}

~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;

datarear = val;

rear = (rear + 1) % capacity;

return true;

}

bool deQueue(int& val) {

if (isEmpty()) return false;

val = datafront;

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;

}

相关推荐
Tisfy12 小时前
LeetCode 3090.每个字符最多出现两次的最长子字符串:二重循环 / 滑动窗口
算法·leetcode·字符串·题解·模拟·双指针·滑动窗口
-dzk-12 小时前
【技巧】LC 136.只出现一次的数字
算法·异或
有点。12 小时前
C++二叉树二(练习题)
数据结构·c++·算法·图论
专注仿真13 小时前
问答大模型技术方案算法实现-RAPTOR树构建算法与BEG集成使用
python·算法
zlinear数据采集卡13 小时前
数据采集卡从入门到精通(10):采样率与分辨率的核心关系——反比律、架构分布与过采样
arm开发·嵌入式硬件·算法·fpga开发·架构·开源
GeekZHR15 小时前
C语言指针进阶补充6:动态内存管理、mem系列内存函数、复杂指针声明,一次补齐指针的“三大盲区“
java·c语言·算法·指针
Herbert_hwt15 小时前
第七章 Java深入理解枚举类型
java·开发语言·算法
.道阻且长.15 小时前
8.LeetCode算法习题讲解--滑动窗口--长度最小的子数组
算法·leetcode·职场和发展
(❁´◡`❁)Jimmy(❁´◡`❁)15 小时前
P1156 [USACO01OPEN] 垃圾陷阱
算法·动态规划
疯狂打码的少年15 小时前
【数据结构】二叉排序树(BST)的定义与操作
数据结构·笔记·算法