信誉好的做网站,国际贸易网站开发,网站建设吉金手指排名12,企业邮箱的登录入口问题#xff1a;一群人站成一个圆圈#xff0c;从一个人开始报数#xff0c;1#xff0c; 2 #xff0c;。。。m#xff0c;报到m的拉出去砍了#xff0c;求被砍的顺序和最后一个活下来的。
利用单向循环链表实现
C代码如下#xff1a;#xff08;参考书籍#xff…问题一群人站成一个圆圈从一个人开始报数1 2 。。。m报到m的拉出去砍了求被砍的顺序和最后一个活下来的。
利用单向循环链表实现
C代码如下参考书籍数据结构与算法实验指导书 #include iostream
using namespace std;struct NodeType
{int num;char name[20];NodeType* next;
};
class Jose
{
private:NodeType* p_head;
public:Jose(){p_head new NodeType; //带空头的链表p_head-next p_head; //空的循环链表}~Jose(){}void creat();void print();
};
void Jose::creat()
{int i 0, n;NodeType *newp, *tempNode;tempNode p_head;cout \n enter total nums of people: ;cin n;for(i 0; i n; i){newp new NodeType;newp-num i1;cout \n enter name: ;cin newp-name;newp-next p_head; //此处p_head为尾部哨兵tempNode-next newp; //不断地往尾部尾部哨兵之前插入节点tempNode newp;}tempNode-next p_head-next; //断开空头哨兵delete p_head; //释放哨兵节点p_head tempNode-next; //头结点指向第一个节点
}
void Jose::print()
{int m,i;NodeType *del p_head, *tempNode;cout \n enter value m(m2):;cin m;cout \n start: endl;while(del-next ! del) //链表节点个数不为1{for(i 1; i m; i) //del往后移动m位{tempNode del;del del-next;}cout del-num del-name endl;tempNode-next del-next; //断开del节点delete del; //释放del节点del tempNode-next;}cout del-num del-name endl;delete del; //链表只剩一个节点直接删除
}int main()
{Jose gameList;gameList.creat();gameList.print();cout press any key to exit! ;cin.get();return 0;
}
Valgrind检查结果
假设有5个人报数3的出列手动模拟如下