1. 程式人生 > >單向迴圈連結串列的實現以及約瑟夫環的實現

單向迴圈連結串列的實現以及約瑟夫環的實現

/*
 * single.h
 *
 *  Created on: 2012-7-21
 *      Author: root
 */

#ifndef SINGLE_H_
#define SINGLE_H_
#include<stdio.h>
#include<stdlib.h>
#define TURE 1
#define FALSE 0
typedef struct link{
	int data;
	struct link *next;
}Link,*pLink;
pLink  CreateLink();                                        //宣告建立單向迴圈連結串列的函式
void  PrintLink(pLink H,int no);                            //宣告列印單向迴圈連結串列的函式
pLink CatLink(pLink H,pLink S);                             //宣告連線兩個單向迴圈連結串列的函式
pLink GetLink(pLink H,int addr);                            //宣告給定位置返回該位置節點的函式
void  Josephu(pLink H,int n,int local,int no);              //約瑟夫函式的宣告

#endif /* SINGLE_H_ */
/*
 * main.c
 *
 *  Created on: 2012-7-21
 *      Author: root
 */
#include"single.h"
int main()
{
#if 0                                                          //建立單向迴圈連結串列
	pLink H;
 H = CreateLink(); //create a link;
	#endif

#if 0
		int  numb;                                                 //列印該迴圈連結串列  numb為列印次數
		printf("\nplease input the times of print:");
		scanf("%d",&numb);
		PrintLink(H,numb);

#endif

#if 0                                                          //給定位置返回該位置在連結串列中的節點
		pLink s ;
		int addr;
		printf("\nplease input the addr you want to get:");
			scanf("%d",&addr);

		s = GetLink(H,addr);
		printf("%d\n",s->data);
#endif

#if 0
		pLink temp,S;                                               //兩個單向迴圈連結串列的連線
		S = CreateLink();
	 temp = CatLink(H,S);

		printf("\nplease input the times of print:");
		scanf("%d",&numb);

		PrintLink(	temp,numb);
#endif

	#if 0
	int  i = 0,numb;
	printf("\nplease input the times of print:");
	scanf("%d",&numb);


#endif

#if 0
	int addr,numb;                                               //addr表示從第幾個位置開始   numb表示約定數
	printf("\nplease input the frist addr:");                    
	scanf("%d",&addr);

	printf("\nplease input the times:");
	scanf("%d",&numb);

	Josephu(H,9,addr,numb);                                      // 9 表示參加約瑟夫環的人數

#endif
return 0;
}

/*
 * single.c
 *
 *  Created on: 2012-7-21
 *      Author: root
 */
#include"single.h"
#if 0
pLink CreateLink()                          //create the list  建立新連結串列
{
	pLink  r ,H;
	H = NULL;
	int a;
	printf("please input numb:");
		scanf("%d",&a);
	while(a != -1)
	{
		pLink new=(pLink)malloc(sizeof(Link));
		new->data = a;
		new->next = NULL;
   if(H == NULL)
   	   {
	   H = new;
	   r = H;
   	   }
   else{
		r->next= new;
		r = r->next;
   	   }
		printf("please input numb:");
			scanf("%d",&a);
	}

	r->next = H;   //建立單向迴圈連結串列的關鍵
return H;
}
#endif

#if 0
void PrintLink(pLink H,int no)                 //print the list       列印連結串列
{
	pLink r = H;
	int i;
	for(i = 0;i < no;i++)
		{
		printf("%d\t",r->data);
		r=r->next;
		}
	printf("\n");
}
#endif

#if 0
pLink GetLink(pLink H,int addr){
	pLink p=H;
	int i;
	for(i = 1;i< addr;i++)
	{
		p = p->next;
	}
	return p;
}

#endif

#if 0
#endif

#if 0
pLink CatLink(pLink H,pLink S){
	pLink p1,p2,r1,r2;
	p1 = H;
	p2 = H->next;
	r1 = S;
	r2 = S->next;
	H->next = NULL;
	S->next = NULL;
	p1->next = r2;
	r1->next = p2;
return p1;
}


#endif

#if 0
void Josephu(pLink H,int n,int local,int no){
	int i ;
	pLink s = GetLink(H,local-1);
	pLink p, r ;
	while(s!=s->next){
		r = s;
		for(i= 1;i<= no-1;i++)
		{
			r= r->next;
		}
	s = r->next;
	p = s->next;
	r->next = p;
	printf("%d\t",s->data);
	free(s);
	s = r;
	}
	printf("%d",s->data);
	printf("\n");
}


#endif

單向迴圈連結串列的應用代表之一就是約瑟夫環的實現
Josephu問題:設編號分別為:1,2,…,n的n個人圍坐一圈。約定序號為k(1≤k≤n)的人從1開始計數,數到m的那個人出列,他的下一位又從1開始計數,數到m的那個人又出列,依次類推,直到所有人出列為止