"Data structure and algorithm analysis--c language description" after reading note 7

Source: Internet
Author: User

Given two sorted table L1,L2, the process of calculating L1∩L2 is written using only basic table operations.

Note: The table has a table header.

struct Node;

typedef struct NODE *ptrtonode;

typedef ptrtonode List;

typedef ptrtonode Position;


struct Node

{

ElementType Element;

Position Next;

}

Program:

List linkunion (LIST&NBSP;L1,LIST&NBSP;L2) {    list l=malloc (sizeof (struct  Node);     if (l==null)     {printf ("Linklist l is null  !\n "); return l;    }    position p=l1->next,q=l2- >next,r=l;    while (P!=null && q!=null)     {if (P- >element < q->element)   {    p=p->next;}  else if (p->element > q->element) {    q=q->next;} Else{    position tmp=malloc (sizeof (Struct node)); &NBSP;&NBSP;&NBSP;&NBSP;IF (tmp ==null)     {printf ("the node tmp is null !\n");return 0;     }    tmp->element=p->element;    tmp- >next=null;    r->next=Tmp;    r=r->next;    p=p->next;    q=q-> Next;}     }    return l;}


The Josephus problem is the following game: n individuals numbered from 1 to N, sitting around in a circle, starting from number 1th to pass a hot potato. After M-Pass, the person holding the hot potato is cleared from the seat, the circle around is indented, and the person who sits behind the removed person picks up the hot potato and continues the game. Finally the rest of the people win. Therefore, if m=0 and n=5 are cleared in turn, number 5th wins. If M=1 and n=5, then the order of the people purged is 2,4,1,5.

Program:

int Josephus (int M, int N) {int i=n,j=0,tmp,n[n]={0};  while (i!=1) {//Select the person cleared, numbered j+1, array subscript jfor (tmp=0;tmp<m;)     {if (n[j]==0) tmp++; j= (j+1)%N;}    Clear number of people j+1, total number I minus 1n[j]=1;i--;//the next person to start delivering hot potatoes while (n[(j+1)%n]!=0) j++;j= (j+1)%N; } return j+1; J+1 is the number of the last person}


"Data structure and algorithm analysis--c language description" after reading note 7

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.