Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include<string.h>
- #include<time.h>
- struct node
- {
- char name[100];
- struct node *previous;
- struct node *next;
- } *start, *end, *current;
- void swap(char s1[],char s2[])
- {
- char aux[100];
- strcpy(aux,s1);
- strcpy(s1,s2);
- strcpy(s2,aux);
- }
- void createlist(int nr,char v[nr][100])
- {
- int counter=0;
- struct node *mynode;
- start=malloc(nr*sizeof(struct node));
- if(nr<1)
- {
- free(start);
- printf("You chose to have 0 elements.They have to be more than 0");
- exit(0);
- }
- else
- {
- if(start==NULL)
- {
- printf("The start of the list is empty");
- exit(0);
- }
- else
- {
- strcpy(start->name,v[counter]);
- start->next=NULL;
- start->previous=NULL;
- end=start;
- counter++;
- for(int i=1;i<nr;i++)
- {
- mynode=(struct node*)malloc(sizeof(struct node));
- if(!mynode)
- {
- printf("Memory has not been allocated");
- exit(0);
- }
- else
- {
- strcpy(mynode->name,v[counter]);
- end->next=mynode;
- mynode->previous=end;
- end=mynode;
- counter++;
- }
- }
- end->next=start;
- start->previous=end;
- }
- }
- }
- void displaylist(struct node *start)
- {
- struct node *p;
- p=start;
- printf(" %s ",p->name);
- p=p->next;
- while(p!=start)
- {
- printf(" %s ",p->name);
- p=p->next;
- }
- }
- void deletenode(char x[])
- {
- struct node *p,*q;
- p=q=start;
- while(p!=NULL&&strcmp(p->name,x)!=0)
- {
- q=p;
- p=p->next;
- }
- if(p!=NULL)
- {
- if(p!=start)
- {
- q->next=p->next;
- p->next->previous=q;
- }
- else
- {
- start=p->next;
- start->previous=end;
- end->next=start;
- }
- }
- }
- int main()
- {
- srand(time(NULL)) ;
- int nr,number,direction;
- struct node *newnode;
- char str[100];
- start=NULL;
- printf("How many elements do you want the list to have?\n");
- scanf("%d",&nr);
- printf("What is the number of the players who will be eliminated ?\n");
- scanf("%d",&number);
- char names[nr][100];
- for(int i=0;i<nr;i++)
- {
- printf("\nName of the %d player is:",i+1);
- scanf("%s",str);
- strcpy(names[i],str);
- }
- for(int i=0;i<nr-1;i++)
- for(int j=i+1;j<nr;j++)
- if(strcmp(names[i],names[j])>0)
- swap(names[i],names[j]);
- printf("What should the direction be?\nPress 1 for counter-clockwise\nPress 2 for clockwise");
- scanf("%d",&direction);
- createlist(nr,names);
- printf("The players are:");
- displaylist(start);
- if(direction%2==0)
- {
- printf("\nWe will go clockwise\n");
- newnode=start;
- }
- else
- {
- printf("\nWe will go counter-clockwise\n");
- newnode=start->previous;
- }
- int k=1;
- while(nr!=0)
- {
- if(k==number)
- {
- k=1;
- deletenode(newnode->name);
- printf("Eliminating %s\n",newnode->name);
- nr--;
- }
- else
- {
- k++;
- printf("Skipping %s\n",newnode->name);
- }
- if(direction%2==0)
- newnode=newnode->next;
- else
- newnode=newnode->previous;
- }
- printf("\nGame over!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement