daily pastebin goal
23%
SHARE
TWEET

KidsGame

a guest Mar 13th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct N
  6. {
  7.     char pName[50];
  8.     struct N * pNext;
  9.    // struct N * pPrev;
  10. }Node;
  11.  
  12. typedef struct DLList
  13. {
  14.     int cnt;
  15.     Node * pFirst;
  16.     Node * pLast;
  17. }DLL;
  18.  
  19. void initList ( DLL ** l)
  20. {
  21.     (*l)->cnt=0;
  22.     (*l)->pFirst = NULL;
  23.    // (*l)->pPrev = NULL;
  24.     //(*l)->pFirst->pNext = NULL;
  25.    // (*l)->pFirst->pPrev = NULL;
  26.     //(*l)->pLast->pNext = NULL;
  27.    // (*l)->pLast->pPrev = NULL;
  28.  
  29. }
  30.  
  31. DLL *listPtr;
  32. //
  33. //Node * giveName( DLL ** ppl, char Name[50] )
  34. //{
  35. //    if( (*ppl) )
  36. //    {
  37. //        listPtr->pNext= NULL;// listPtr->pPrev= NULL;
  38. //        strcpy(listPtr->pName,Name);
  39. //        listPtr->pNext = listPtr;
  40. //    }
  41. //    return listPtr;
  42. //}
  43.  
  44. void insertAtRear ( DLL ** listPtr, char Name[50])
  45. {
  46.         Node* p = (Node*)malloc(sizeof(Node));
  47.         strcpy(p->pName,Name);
  48.         p->pNext = NULL;
  49.  
  50.  
  51.         if((*listPtr)->pLast == NULL) /// when the list is EMPTY
  52.         {
  53.             (*listPtr)->pFirst = (*listPtr)->pLast = p;
  54.             p->pNext=(*listPtr)->pFirst;
  55.  
  56.         }
  57.         else ///when the list is not EMPTY
  58.         {
  59.             (*listPtr)->pLast->pNext = p; // chain after the former last node
  60.             p->pNext = (*listPtr)->pFirst; // p is after the former last node
  61.             (*listPtr)->pLast = p; // p is the last node
  62.         }
  63.         (*listPtr)->cnt++;
  64. }
  65.  
  66. void deleteNode ( DLL * l, char Name[50])
  67. {
  68.     Node * p;
  69.     Node * prevPtr;
  70.     p = l->pFirst;
  71.     prevPtr = l->pLast;
  72.     int j;
  73.     for( j=0; j < l->cnt; j++)
  74.     {
  75.         if( stricmp ( p->pName, Name) == 0 )
  76.         {
  77.             prevPtr->pNext = p->pNext;
  78.             if( p == l->pFirst)
  79.                 l->pFirst = p->pNext;
  80.             if( p == l-> pLast)
  81.                 l->pLast = prevPtr;
  82.             l->cnt--;
  83.             break;
  84.         }
  85.         prevPtr=p;
  86.         p = p->pNext;
  87.     }
  88. }
  89.  
  90. char * Circle ( DLL * l , int nrKids)
  91. {
  92.     while ( l->cnt != 1)
  93.     {
  94.         int counter=1;
  95.         Node *p = l->pFirst;
  96.         while(counter < nrKids && p)
  97.         {
  98.             p = p->pNext;
  99.             counter++;
  100.         }
  101.         deleteNode(l, p->pName);
  102.     }
  103.     return l->pFirst->pName;
  104. }
  105.  
  106. void Display(DLL *l, int n, FILE *O)
  107. {
  108.     Node *p = l->pFirst;
  109.     if(p == NULL) fprintf(O,"Failed to create the list");
  110.     while(p != NULL)
  111.     {
  112.         int i;
  113.         for(i=0; i<n; i++)
  114.         {
  115.             fprintf(O,"%s \n", p->pName);
  116.             p = p->pNext;
  117.         }
  118.     }
  119.  
  120. }
  121.  
  122. int main ()//int argc, int * argv[])
  123. {
  124.     DLL * Game;
  125.     Game = (DLL*) malloc(sizeof(DLL));
  126.     initList(&Game);
  127.  
  128.     FILE *P = fopen("P.in","r");
  129.     FILE *O = fopen("O.out","w");
  130.  
  131.     if( P == NULL )
  132.         printf("The input file could not be opened.");
  133.     if ( O == NULL )
  134.         printf("The output file could not be opened.");
  135.  
  136.  
  137.     int nrKids, i=0;
  138.     char kidName[1050], junk[50];
  139.  
  140.     fscanf(P, "%d", &nrKids);
  141.     fgets(junk,50,P);
  142.     fprintf(O, "%d \n", nrKids);
  143.  
  144.     for(i=0; i<nrKids; i++)
  145.     {
  146.         fscanf(P,"%s", kidName);
  147.         insertAtRear(&Game, kidName);
  148.     }
  149.  
  150.     Display(Game, nrKids, O);
  151.     fprintf("\n%s\n", Circle(Game, nrKids));
  152.  
  153.     return 0;
  154. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top