本篇文章中所有數據結構都是后期整理的,如有問題歡迎指正,轉載請注明出處http://www.cnblogs.com/a1982467767/p/8893553.html
約瑟夫環問題實現
實驗程序:
1 //cir_linklist.h 2 //線性表鏈式結構——循環鏈表 3 #ifndef _LINKLIST_H_ //如果沒有定義這個宏 4 #define _LINKLIST_H_ //定義這個宏 5 //#define _CRT_SECURE_NO_WARNINGS//消除scanf在Vs中不兼容的問題 6 #include <stdlib.h> 7 #include <string.h> 8 9 //主要申明區間 10 LinkList Create_Linklist(void);//創建鏈表函數申明 11 LinkList Output_Linklist(LinkList head);//鏈表的遍歷,數據輸出 12 int CountNumber_Linklist(LinkList head);//計算鏈表數據的長度 13 void Distory(LinkList *h);//線性表的銷毀 14 15 /*創建一個以data為元素鏈表,按data升序排列,以-1輸入為建立結束*/ 16 LinkList Create_Linklist(void) 17 { 18 int x; 19 LinkList head, s, r; 20 head = (ListNode*)malloc(sizeof(ListNode)); /*為頭結點head申請空間*/ 21 if (!head)//內存分配判斷 22 { 23 printf("內存申請失敗,程序退出......\n"); 24 exit(-1); 25 } 26 r = head; 27 r->next = head; 28 printf("請按data升序輸入,以-1輸入為結束\n"); 29 printf("請輸入一個data:"); 30 scanf("%d", &x); 31 while (x != -1) 32 { 33 if (r == head) 34 r->data = x; 35 else 36 { 37 s = (ListNode*)malloc(sizeof(ListNode));/*為添入結點申請空間*/ 38 if (!s)//內存分配判斷 39 { 40 printf("內存申請失敗,程序退出......\n"); 41 exit(-1); 42 } 43 s->data = x; 44 s->next = r->next; 45 r->next = s;/*這兩句表示將新創建的s結點連接到r結點的后面,r初次對應的head並沒有數據,所以head是含有空頭的鏈表,畫圖可以更方便理解*/ 46 r = s;/*用r將新定義的結點s取代,這樣可以使用s進行反復連接*/ 47 48 } 49 printf("請輸入下一個data:"); 50 scanf("%d", &x); /*輸入下一個data*/ 51 } 52 return head; 53 } 54 55 /*輸出鏈表head中的所有數據元素*/ 56 LinkList Output_Linklist(LinkList head) 57 { 58 ListNode *p; 59 int i = 0; 60 if (!head) 61 { 62 printf("表元素為空\n"); 63 return 0; 64 } 65 p = head; 66 if (head->next == head)//鏈表為空判斷 67 { 68 printf("鏈表中沒有數據......\n"); 69 return head; 70 } 71 while (p->next != p) 72 { 73 printf("%d ", p->data); 74 p = p->next;/*p指向p的下一個結點*/ 75 76 } 77 printf("\n"); 78 return head; 79 } 80 81 int CountNumber_Linklist(LinkList head)//顯示鏈表的長度 82 { 83 int count = 0; 84 LinkList s; 85 s = head; 86 while (s->next != s) 87 { 88 count++; 89 s = s->next; 90 } 91 return count; 92 } 93 94 //銷毀線性表 95 void Distory(LinkList *h) 96 { 97 LinkList p, q; 98 p = *h; 99 while (p->next != p) 100 { 101 q = p; 102 p = p->next; 103 free(q); 104 } 105 *h = NULL; 106 printf("銷毀鏈表成功......\n"); 107 } 108 #endif
1 //josephus.h 2 nt josephus_LinkList(LinkList josephus_Link, int s, int m) 3 { 4 //s表示報數的數,m表示長度? 5 LinkList p, pre; 6 int count; 7 if (!josephus_Link) 8 { 9 printf("表元素為空\n"); 10 return 0; 11 } 12 /*找第S個元素*/ 13 p = josephus_Link; 14 for (count = 1; count < s; count++) 15 { 16 p = p->next; 17 } 18 printf("輸出約瑟夫序列:"); 19 while (p != p->next) 20 { 21 pre = p->next; 22 while (pre->next != p) pre = pre->next; 23 for (count = 1; count < m; count++) 24 { 25 pre = p; 26 p = p->next; 27 } 28 printf("%d\t", p->data); 29 pre->next = p->next; 30 free(p); 31 p = pre->next; 32 } 33 printf("%d\t", p->data); 34 free(p); 35 return 1; 36 }
//main.c #include<stdio.h> #include <windows.h>//插入此頭文件的目的是調用Sleep(1000)函數,中間1000正常情況下表示一秒鍾 typedef struct node { int data; struct node *next; }ListNode, *LinkList; #include "linklist.h" #include "josephus.h" int main(void) { LinkList head; int m; system("color 02");//顏色背景函數System("color 100")可以查看該函數后面的兩個字符的說明 head = Create_Linklist(); m = CountNumber_Linklist(head); josephus_LinkList(head, m, 4); system("PAUSE"); return 0; }