12.4 Delete cyclic linked list and linked list nodes-Joseph's ring

Source: Internet
Author: User

Repeat this code because it is worth learning.

1. For the linked list, you can easily insert data into the header, but if you keep a tail node, you can easily insert nodes at the end.

2. You do not need to define a pointer to retain the parent node of the node you want to delete. You only need to set a few pointer values. For example, in the Code: If (COUNT = stride-1)

Sample Code:

# Include <stdio. h> struct monkey {int ID; monkey * Next;}; int main (INT argc, char * argv []) {monkey * link, * monkey, * lastmonkey; int totalmonkeys, stride, count; printf ("Please input the numbers of monkeys: \ n"); scanf ("% d", & totalmonkeys); printf ("Please input a number for striding: \ n "); scanf (" % d ", & STRIDE); link = NULL; For (INT I = 0; I <totalmonkeys; I ++) {monkey = new monkey; monkey-> id = I + 1; if (link = Null) {link = lastmonkey = monkey;} else {lastmonkey-> next = monkey; lastmonkey = monkey ;}} lastmonkey-> next = link; Count = 1; printf ("monkey team sequence:"); While (Link! = NULL) {If (Link-> next = link) {printf ("% 4D \ n", link-> ID); Delete link; break ;} if (COUNT = stride-1) {monkey = link-> next; Link-> next = monkey-> next; printf ("% 4D", monkey-> ID ); delete monkey; monkey = NULL; Count = 0;} link = link-> next; count ++;} 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.