zoukankan      html  css  js  c++  java
  • 约瑟夫问题 双链表实现

    <span style="color:#cc33cc;">
    /*********************************
         author   : Grant Yuan
         algorithm; 双链表、
    	 time    ;2014/10/3 20:38
     *********************************/   
    #include<iostream>
    #include <cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    
    typedef struct node
    {
    	int id;
    	struct node *front,*next;
    }Node,*PNode;
    int m,n;
    
    PNode Create_list()
    {
    	PNode head,p,q;
    	head=(Node *)malloc(sizeof(Node));
    	head->id=1;
    	p=(Node *)malloc(sizeof(Node));
    	p=head;
    	for(int i=2;i<=n;i++)
    	{
    		q=(Node *)malloc(sizeof(Node));
    		q->id=i;
    		q->front=p;
    		p->next=q;
    		p=q;
    	}
    	p->next=head;head->front=p;
    	return head;
    }
    
    void Delete_List(PNode p)
    {
    	while(p->next){
    		PNode q;
    		q=p->next;
    		free(p);
    		p=q;
    	}
    }
    PNode Get_Next_Node(PNode head,int k)
    {
    	PNode p=head;
    	for(int i=1;i<k;i++)
    	{
    	  p=p->next;
    	}
    	return p;
    }
    PNode Get_front_Node(PNode head,int k)
    {
    	PNode p=head;
    	for(int i=1;i<k;i++)
    	{
    		p=p->front;
    	}
    	return p;
    }
    
    void Delete_Node(PNode p)
    {
    	PNode q;
    	(p->front)->next=p->next;
    	(p->next)->front=p->front;
    }
    
    void Print_List(PNode head)
    {
    	PNode p=head;
    	printf("%d ",p->id);
    	p=p->next;
    	while(1){
    	   if(p==head) break;
    	   printf("%d ",p->id);
    	   p=p->next;
    	}
    	printf("
    ");
    }
    int main()
    {
        cin>>n>>m;
        PNode head,p,q;
        head=Create_list();
    	int ans;p=head;
         while(1){
    		p=Get_Next_Node(p,m);
    		q=p->next;ans=p->id;
    	    if(p->next==p) break;
    	    Delete_Node(p);
    	    p=q;
         }
         printf("%d
    ",ans);
        return 0;
    }
    </span>


  • 相关阅读:
    dos
    admin package
    ant 调用系统环境变量
    idea6+tomcat5.5开发web程序
    VFloppy
    ant中classpath
    Velocity用户手册
    ant中 Tomcat的任务调用(包括deploy,undeploy,load,start,stop等)
    [转]aidl高级应用篇
    Android NDK开发环境安装(OK版)
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254423.html
Copyright © 2011-2022 走看看