zoukankan      html  css  js  c++  java
  • 循环链表之约瑟夫 环

    #include<iostream>
    #include<malloc.h>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    #define maxsize 100
    typedef struct node
    {
    int data;
    struct node *next;
    }lnode,*linklist;

    linklist link(linklist head1,linklist head2)
    {
    lnode *p,*q;
    p=head1->next; q=head2->next;
    while(p->next!=head1)
    {
    p=p->next;
    }
    while(q->next!=head2)
    {
    q=q->next;
    }
    p->next=head2->next;
    q->next=head1;
    return head1;
    }
    linklist creat(int n)
    {
    linklist head=NULL;
    lnode *p,*q,*h;
    for(int i=1;i<=n;i++)
    {
    p=(linklist)malloc(sizeof(lnode));
    p->data=i;
    p->next=NULL;
    if(head==NULL)
    head=p;
    else

    q->next=p;//注意 别写成head 那样的话就变成了一对多关系
    q=p;
    }

    q->next=head;

    return head;
    }


    void yuese(linklist h,int n,int m,int k)
    {
    lnode *p,*q;
    int i;
    p=h;
    for(i=1;i<k;i++)
    {
    q=p;//q=p->prior
    p=p->next;
    }
    while(p->next!=p)
    {
    for(i=1;i<m;i++)
    {
    q=p;
    p=p->next;
    }
    q->next=p->next;
    cout<<p->data<<" ";
    free(p);
    p=q->next;
    }
    cout<<p->data;
    }
    int main()
    {
    linklist h;
    int n,k,m;
    cout<<" 创建h循环单链表:"<<endl;
    cout<<" 请输入元素个数:"<<endl;
    cin>>n;
    cout<<" 请输入开始报数的序号:"<<endl;
    cin>>k;
    cout<<" 报数为m的人出列:"<<endl;
    cin>>m;
    h=creat(n);
    yuese(h,n,m,k);
    }

  • 相关阅读:
    crmfuxi
    段子
    wsfenxiang
    生成器、列表推导式
    闭包、迭代器、递归
    函数的参数及返回值
    嵌套、作用域、命名空间
    定义、函数的调用
    测试样式
    进制转换
  • 原文地址:https://www.cnblogs.com/mykonons/p/6592038.html
Copyright © 2011-2022 走看看