数据结构

链表的建立

#include <bits/stdc++.h>
//#pragma  GCC optimize("Ofast")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
//#define double long double
#define int long long
//#define endl '\n';
using namespace std;
const int N=16005,M=1e1;
const int INF = 0x3f3f3f3f;
const int mod=100003;
typedef pair<int,int> PII;

struct G
{
    int x;
    G *en;
};
G *u=NULL,*h=NULL;
void solve()
{
    int a,b;
    cin>>a>>b;
    for(int i=1;i<=a;i++)//开始建表
    {
        G *p=(G*)malloc(sizeof (G));
        p->x=i;
        p->en=NULL;
        if(h==NULL)
        {
            h=p;  //头节点赋予
        }
        else
        {
            u->en=p;
        }
        u=p;
    }
    u->en=h;
    int now=a;
    G *q=h;
    while (now>2)
    {
        int f=1;
        while (f<b-1)
        {
            q=q->en;
            f++;
        }
        q->en=q->en->en;  //删除节点
        q=q->en;   //指向下一个准备开始遍历
        now--;
    }
    cout<<q->x<<" "<<q->en->x;
}


signed main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int T=1;
//    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}

 

posted @ 2023-09-24 16:05  whatdo+  阅读(3)  评论(0编辑  收藏  举报