dalton85 Δημοσ. 6 Ιουνίου 2007 Δημοσ. 6 Ιουνίου 2007 Ειναι μια υλοποιηση σε C# του αλγοριθμου του LeLann. Οποιος μπορει να βοηθησει θα μου λυσει αρκετα τα χερια. Γιατι οι ταυτοτητες παιρνουν συνεχεια 1 και 2 δεν μπορω να καταλαβω αφου κανω ελεγχο μεσα απο την συναρτιση. ο αλγοριθμος ειναι : var Listp : set of P init{p}; statep; BEGIN If p is initiator then /* εάν ο επεξεργαστής είναι αρχικοποιητής */ Begin statep:=candidate; /* δηλώνει υποψήφιος και */ send <tok,p> to Nextp; /* στέλνει την ταυτότητά του */ receive <tok,q>; while q≠p do begin /* τα μηνύματα των άλλων τα τοποθετεί */ Listp:=Listp ∪ {q}; /* στη λίστα του */ send <tok,q> to Nextp; /* τα προωθεί */ receive <tok,q> end; if p=max(Listp) then statep:=leader /* συνθήκη εκλογής */ else statep:=lost end; else repeat receive <tok,q>; /* αν δεν είναι αρχικοποιητής */ send <tok,q> to Nextp; /* απλώς προωθεί τα μηνύματα */ if statep=sleep then statep:=lost until false end Το προγραμμα που δουλευω: #include<stdio.h>#include<math.h> struct komvoi { int komvo; struct komvoi *next2; }*head2,*tail2,*list_runner3,*new_node2; struct daktilios /*Dimiourgia tou struct gia tin lista me tous kombous*/ { int resived; int arxikop; int taytotita; int komvos; struct daktilios *next; struct daktilios *point; }*head,*new_node,*tail,*list_runner,*list_runner2,*temp,*resived; int check(int ident,int plithos) { list_runner2=head; while(list_runner2!=NULL) { if(list_runner2->taytotita==ident) { ident=rand()%plithos; check(ident,plithos); } list_runner2=list_runner2->next; } return(ident); } void fill(int taytot) { if(head2==NULL) /*Dhmiourgoume thn lista tou ka8e arxikopoihth me tous upoloipous arxikopoihtes pou exoun kanei dilwsei gia arxigos*/ { new_node2=(struct komvoi *)malloc(sizeof(struct komvoi)); new_node2->komvo=taytot; new_node2->next2=NULL; head2=new_node2; tail2=head2; list_runner->point=head2; } else { new_node2=(struct komvoi *)malloc(sizeof(struct komvoi)); tail2->next2=new_node2; new_node2->komvo=taytot; new_node2->next2=NULL; tail2=new_node2; } } main() { int plithos,i,flag,min,id; printf("dwse to plithos twn komvwn:\n"); scanf("%d",&plithos); for(i=0;i<plithos;i++) { if(i==0) /*Dhmiourgoume thn lista me tous kombous*/ { new_node=(struct daktilios *)malloc(sizeof(struct daktilios)); new_node->taytotita=rand()%plithos; new_node->arxikop=rand()%2; new_node->komvos=(i+1); new_node->next=NULL; head=new_node; tail=head; printf("o komvos %d pire taytotita %d\n",(i+1),new_node->taytotita); } else { new_node=(struct daktilios *)malloc(sizeof(struct daktilios)); tail->next=new_node; id=check(1,plithos); new_node->taytotita=id; new_node->arxikop=rand()%2; new_node->komvos=(i+1); new_node->next=NULL; tail=new_node; printf("o komvos %d pire taytotita %d\n",(i+1),new_node->taytotita); } }getch(); printf("\na"); list_runner=head; flag=1; while(flag!=0) { if(list_runner->arxikop==1) { list_runner->arxikop=2; list_runner->next->resived=list_runner->taytotita; if(list_runner->resived!=list_runner->taytotita) { fill(list_runner->resived); } else flag=0; } else { list_runner->next->resived=list_runner->resived; list_runner->arxikop=3; } } list_runner=head; flag=1; while((list_runner!=NULL)&&(flag!=0)) { if(list_runner->arxikop==2) list_runner3=list_runner->point; min=list_runner3->komvo; while(list_runner3!=NULL) { if(list_runner3->komvo<min) min=list_runner3->komvo; list_runner3=list_runner3->next2; } if(min==list_runner->taytotita) { list_runner->arxikop=5; flag=0; printf("O Leader einai o komvos %d",list_runner->komvos); } list_runner=list_runner->next; } getch(); }
Προτεινόμενες αναρτήσεις
Αρχειοθετημένο
Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.