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>

  • 相关阅读:
    11. Container With Most Water
    9. Palindrome Number
    375. 猜数字大小 II leetcode java
    leetcode 72 编辑距离 JAVA
    73. 矩阵置零 leetcode JAVA
    快速排序 JAVA实现
    63. 不同路径 II leetcode JAVA
    重写(override)与重载(overload)
    62 不同路径 leetcode JAVA
    leetcode 56 合并区间 JAVA
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5418512.html
Copyright © 2011-2022 走看看