Joseph's problem dual-chain table implementation

Source: Internet
Author: User
<span style="color:#cc33cc;">
/******************************** Author: grant yuan algorithm; double-stranded table, time; ***********************************/# 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 ("\ n");} 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 \ n ", ans); Return 0 ;}</span>

Joseph's problem dual-chain table implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.