Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4. #include<time.h>
  5.  struct node
  6. {
  7.     char name[100];
  8.     struct node *previous;
  9.     struct node *next;
  10. } *start, *end, *current;
  11.  
  12. void swap(char s1[],char s2[])
  13. {
  14.     char aux[100];
  15.     strcpy(aux,s1);
  16.     strcpy(s1,s2);
  17.     strcpy(s2,aux);
  18. }
  19.  
  20.     void createlist(int nr,char v[nr][100])
  21. {
  22.     int counter=0;
  23.     struct node *mynode;
  24.     start=malloc(nr*sizeof(struct node));
  25.     if(nr<1)
  26.     {
  27.      free(start);
  28.      printf("You chose to have 0 elements.They have to be more than 0");
  29.      exit(0);
  30.     }
  31.     else
  32.     {
  33.         if(start==NULL)
  34.         {
  35.          printf("The start of the list is empty");
  36.          exit(0);
  37.         }
  38.         else
  39.         {
  40.             strcpy(start->name,v[counter]);
  41.             start->next=NULL;
  42.             start->previous=NULL;
  43.             end=start;
  44.             counter++;
  45.             for(int i=1;i<nr;i++)
  46.             {
  47.               mynode=(struct node*)malloc(sizeof(struct node));
  48.               if(!mynode)
  49.               {
  50.                printf("Memory has not been allocated");
  51.                exit(0);
  52.               }
  53.               else
  54.               {
  55.                strcpy(mynode->name,v[counter]);
  56.                end->next=mynode;
  57.                mynode->previous=end;
  58.                end=mynode;
  59.                counter++;
  60.               }
  61.             }
  62.             end->next=start;
  63.             start->previous=end;
  64.         }
  65.     }
  66. }
  67. void displaylist(struct node *start)
  68. {
  69.     struct node *p;
  70.     p=start;
  71.     printf(" %s  ",p->name);
  72.     p=p->next;
  73.     while(p!=start)
  74.     {
  75.         printf(" %s  ",p->name);
  76.         p=p->next;
  77.     }
  78. }
  79.  
  80. void deletenode(char x[])
  81. {
  82.     struct node *p,*q;
  83.     p=q=start;
  84.     while(p!=NULL&&strcmp(p->name,x)!=0)
  85.     {
  86.       q=p;
  87.       p=p->next;
  88.     }
  89.     if(p!=NULL)
  90.     {
  91.      if(p!=start)
  92.        {
  93.         q->next=p->next;
  94.         p->next->previous=q;
  95.        }
  96.      else
  97.       {
  98.         start=p->next;
  99.         start->previous=end;
  100.         end->next=start;
  101.        }
  102.     }
  103.  
  104. }
  105.  
  106. int main()
  107. {
  108.   srand(time(NULL)) ;
  109.   int nr,number,direction;
  110.   struct node *newnode;
  111.   char str[100];
  112.   start=NULL;
  113.   printf("How many elements do you want the list to have?\n");
  114.   scanf("%d",&nr);
  115.   printf("What is the number of the players who will be eliminated ?\n");
  116.   scanf("%d",&number);
  117.   char names[nr][100];
  118.   for(int i=0;i<nr;i++)
  119.   {
  120.         printf("\nName of the %d player is:",i+1);
  121.         scanf("%s",str);
  122.         strcpy(names[i],str);
  123.   }
  124.   for(int i=0;i<nr-1;i++)
  125.     for(int j=i+1;j<nr;j++)
  126.      if(strcmp(names[i],names[j])>0)
  127.       swap(names[i],names[j]);
  128.   printf("What should the direction be?\nPress 1 for counter-clockwise\nPress 2 for clockwise");
  129.   scanf("%d",&direction);
  130.   createlist(nr,names);
  131.   printf("The players are:");
  132.   displaylist(start);
  133.   if(direction%2==0)
  134.   {
  135.     printf("\nWe will go clockwise\n");
  136.     newnode=start;
  137.   }
  138.   else
  139.   {
  140.     printf("\nWe will go counter-clockwise\n");
  141.     newnode=start->previous;
  142.   }
  143.   int k=1;
  144.   while(nr!=0)
  145.   {
  146.      if(k==number)
  147.      {
  148.       k=1;
  149.       deletenode(newnode->name);
  150.       printf("Eliminating %s\n",newnode->name);
  151.       nr--;
  152.      }
  153.      else
  154.      {
  155.       k++;
  156.       printf("Skipping %s\n",newnode->name);
  157.      }
  158.      if(direction%2==0)
  159.         newnode=newnode->next;
  160.      else
  161.         newnode=newnode->previous;
  162.   }
  163.   printf("\nGame over!");
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement