代码如下:(好霸气的方法) ----- 数组实现Joseph_Circle
int Joseph_Circle(int n, unsigned int m) { if(n <= 0 || m < 0) return -1; int Last = 0; for(int i=2; i<=n; i++) { Last = (Last + m) % i; } return Last; }
返回最后剩下的节点,返回该节点的地址!!!
代码及解析如下:
//本代码涉及的知识点 //从无头链表中删除一个节点 //申请释放内存 //防止野指针的产生 //环状链表循环操作 //代码及解析如下 #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <climits> #include <set> #include <cmath> #include <ctime> #include <vector> #include <cassert> #include <algorithm> #define MAXN 10010 #define RST(N)memset(N, 0, sizeof(N)) using namespace std; typedef int data_t; typedef struct ListNode { data_t data; struct ListNode *next; }LNode, *pNode; pNode Create(int n) { pNode head = NULL; pNode p1 = NULL, p2 = NULL; p1 = p2 = (pNode)malloc(sizeof(LNode)); int value = 1; //此例链表值是从1开始的 while(value <= n) { p1->data = value++; if(head == NULL) head = p1; else p2->next = p1; p2 = p1; p1 = (pNode)malloc(sizeof(LNode)); } p2->next = head; return head; } pNode Joseph_Circle(pNode head, int m) { assert(head != NULL); pNode pCur = head; pNode pTemp = NULL; int cnt = 1; while(pCur->next != pCur) //条件 { if(cnt == m) { cnt = 1; pTemp = pCur->next; //删除一个节点 pCur->data = pTemp->data; pCur->next = pTemp->next; //此时直接跳向了下一个节点了,所以不用再pCur = pCur->next了 free(pTemp); //释放 continue; } cnt++; pCur = pCur->next; //注意,不要漏了这一步 } pTemp = NULL; //防止野指针 return pCur; } int main() { int n, m; while(cin >> n >> m) { pNode head = Create(n); pNode pRes = Joseph_Circle(head, m); cout << "The result is : " << pRes->data << endl; } return 0; }
约瑟夫环问题,给定一个输入链表,每跳出一个节点,把它加进另一个链表中,形成另一个环状单链表,如:
输入:
1 -> 2 -> 3 -> 4 -> 5
^ |
|____________________|
输出:
3 -> 1 -> 5 -> 2 -> 4
^ |
|___________________|
//代码及解析如下
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <climits> #include <set> #include <cmath> #include <ctime> #include <vector> #include <cassert> #include <algorithm> #define MAXN 10010 #define RST(N)memset(N, 0, sizeof(N)) using namespace std; typedef int data_t; typedef struct ListNode { data_t data; struct ListNode *next; }LNode, *pNode; pNode Create(int n) { pNode head = NULL; pNode p1 = NULL, p2 = NULL; p1 = p2 = (pNode)malloc(sizeof(LNode)); int cnt = 1; while(cnt <= n) { p1->data = cnt++; //别忘了++ if(head == NULL) head = p1; else p2->next = p1; p2 = p1; p1 = (pNode)malloc(sizeof(LNode)); } p2->next = head; return head; } pNode Joseph_Circle(pNode head1, int m) { assert(head1 != NULL); pNode pCur = head1; //当前节点 pNode pNext = NULL; //当前下一个节点 pNode head2 = NULL; //第二个链表 pNode p1 = NULL; //第二个链表添加新元素要用到 int cnt = 1; while(pCur->next != pCur) { if(cnt == m) { pNext = pCur->next; swap(pCur->data, pNext->data); //交换当前前后节点值 pCur->next = pNext->next; //第一个链表删除当前下一个节点 if(head2 == NULL) //第二个链表添加删除的节点 head2 = pNext; else p1->next = pNext; p1 = pNext; cnt = 1; //计数重置1 continue; } cnt++; pCur = pCur->next; } p1->next = pCur; //第一个链表最后一个元素添加进第二个链表 pCur->next = head2; //指向头 return head2; } int main() { int n, m; while(cin >> n >> m) { pNode head1 = Create(n); //test head1 begin //测试head1 pNode p = head1; cout << p->data << " "; for(p=p->next; p!=head1; p=p->next) { cout << p->data; if(p->next == head1) cout << endl; else cout << " "; } //test head1 end pNode head2 = Joseph_Circle(head1, m); //test head2 begin //测试head2 pNode q = head2; cout << q->data << " "; for(q=q->next; q!=head2; q=q->next) { cout << q->data; if(q->next == head2) cout << endl; else cout << " "; } //test head2 end } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/keshacookie/article/details/47303127