Experiment 3: compile an application to solve the Joseph Ring Problem

Source: Internet
Author: User

Lab Purpose

Compile a simple data reporting application in a C language environment (Turbo C or Vc.

Through this experiment, students can skillfully write basic applications in the C language environment.

Lab Significance

Through this experiment, students can further understand the use of linked lists and pointers in C language and be familiar with basic input and output methods.

Lab background

Joseph's ring problem Joseph PHUs: numbers 1, 2, 3 ,......, N people sit around clockwise. You can select a positive integer as the maximum number of messages. The first person starts to report the number in the clockwise direction from 1. When reporting to m, the number of messages stops. The person who reports m is listed, starting from the next person in the clockwise direction to record the number from 1 until all people are listed.

Lab steps and requirements

1. Create a single-cycle linked list storage structure for N people

2. output the serial number of the person who leaves the team in sequence after the operation is completed.

3. Submit the source program list and program comments

4. Submit the program usage method and program user interface

5. Hands-on demonstration of experiment results

Lab hours

3 hours

# Include <stdio. h> # include <stdlib. h> typedef struct node // create struct {int num; struct node * Next;} lnode, * linklist; int initlink (linklist & L, int N) // create a circular linked list {int I; L = (linklist) malloc (sizeof (lnode); // create a space for the header node l-> next = NULL; // The header Pointer Points to the null linklist rear = L; // rear as the flag for (I = 1; I <= N; I ++) // accept the data of the new node {linklist P = (linklist) malloc (sizeof (lnode); // apply for space for the new node p-> num = I; rear-> next = P; Rear = rear-> next ;} Rear-> next = L-> next; return 0;} int Joseph PHUs (linklist & L, int m) {linklist p, q; // defines P, Q two pointers P = L; // P points to the first node l while (p-> next! = P) {for (INT I = 1; I <m; I ++) P = p-> next; q = p-> next; p-> next = Q-> next; printf ("% d", Q-> num); // output order free (Q ); // release the space of the output data} printf ("% d \ n", p-> num); free (p); // release the space of the last node return 0 ;} void print (linklist L) {linklist P = L-> next; printf ("% d", p-> num); P = p-> next; while (P! = L-> next) {printf ("% d", p-> num); P = p-> next ;}} int main () {linklist L; int N, m; printf ("Enter the number of people sitting around the Round Table N:"); scanf ("% d", & N); initlink (L, N ); printf ("Raw data:"); print (l); printf ("\ n"); printf ("number of packets M = "); scanf ("% d", & M); printf ("quit in sequence:"); Joseph (L, M); printf ("\ n "); return 0 ;}

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.