文章

61

粉丝

98

获赞

17

访问

15.3k

头像
击鼓传花 题解:c++自定义无头节点的循环链表实现
P1018 贵州大学机试题
发布于2024年3月16日 09:48
阅读数 184

#include<bits/stdc++.h>
using namespace std;

typedef struct node{
    int number;
    struct node* next;
}LiNode, *LiList;

int main(){
    int n;
    cin >> n;
    LiList L = (LiList) malloc(sizeof(LiNode));
    L->number = 1;
    L->next = L;        //不带头节点的循环单链表
    LiNode *rear = L;       //尾指针
    LiNode* p;
    //插入循环单链表
    for(int i = 2; i <= n; i++){
        p = (LiNode*) malloc(sizeof(LiNode));
        p->number = i;
        rear->next = p;
        p->next = L;
        rear = p;
    }

    /*
    p = L;
    cout << p->number << endl;
    while(p->next != L){
        p = p->next;

        cout << p->number << endl;
    }
    */


    //开始击鼓传花
    p = rear;
    LiNode *q;
    for(int i = 0; i < n-1; i++){
        p = p->next->next;
        q = p->next;
        p->next = q->next;
        free(q);
    }

    cout << p->number;

    ret...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发