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