数据结构实验 约瑟夫环的循环链表实现

附代码:

/****************************************************
*
*   This is a Circular List class
*   Created by ZhiYu.Wang
*   Current time: 2014.5.17
*
*****************************************************/

#include <iostream>
#include <cstdio>
using namespace std;
class list{
private:

    // the data structure of the node in list.
    struct node{
        node* next;
        int val;
        node(){
            next = NULL;
            val = -1;
        }
        ~node(){
            next = NULL;
            val = -1;
        }
    };

    // root node.
    node* root;
public:
    // construct the list.
    list(){
        root = NULL;
    }

    // dealloc the list.
    ~list(){
        node* tp = root;
        root = root -> next;
        while(root != tp){
            node* rt = root;
            root = root -> next;
            delete rt;
        }
        delete root;
    }

    // Get the number of the last people.
    int getRootVal(){
        return root -> val;
    }

    // Insert a people to the list.
    void Insert(int val){
        if(root == NULL){
            root = new node;
            root -> val = val;
            root -> next = root;
            return;
        }
        node* rt = root -> next;
        root -> next = new node;
        root = root -> next;
        root -> val = val;
        root -> next = rt;
    }

    // Get the amount of the list.
    int getNum() const{
        node* rt = root -> next;
        int cnt = 1;
        while(rt != root){
            cnt ++;
            rt = rt -> next;
        }
        return cnt;
    }

    // Delete the k-th people from the list.
    bool DeleteNum(int k, int now){
        if(getNum() == 1)
            return false;
        k -= 1;
        while(k --){
            root = root -> next;
        }
        node* rt = root -> next;
        cout << "The " << now << "-th people who will out of the circle is: ";
        cout << rt -> val << endl;
        root -> next = root -> next -> next;
        delete rt;
        return true;
    }
};

// to judge the error in scan process.
bool err(){
    if(cin.fail()){
        cin.clear();
        cin.sync();
        return true;
    }
    return false;
}

int main(){

    // create a list.
    list* l = new list;
    int n, k;

    // scan the number n.
    cout << "Please input the number of the people." << endl;
    while(true){
        cin >> n;
        if(!err()) break;
        cout << "Please try again to input the number of the people." << endl;
    }

    // scan the number k
    cout << "Please input the number of the k." << endl;
    while(true){
        cin >> k;
        if(!err()) break;
        cout << "Please try again to input the number of the k." << endl;
    }

    // insert element to the list l.
    for(int i = 1; i <= n; ++i)
        l -> Insert(i);

    // circulate to delete the element int the list l.
    int now = 1;
    while(l -> getNum() > 1)
        l -> DeleteNum(k, now++);

    // print the last people's number.
    cout << "The number of last people is: ";
    cout << l -> getRootVal() << endl;

    // dealloc the list l.
    delete l;

    // return success;
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值