Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct N
- {
- char pName[50];
- struct N * pNext;
- // struct N * pPrev;
- }Node;
- typedef struct DLList
- {
- int cnt;
- Node * pFirst;
- Node * pLast;
- }DLL;
- void initList ( DLL ** l)
- {
- (*l)->cnt=0;
- (*l)->pFirst = NULL;
- // (*l)->pPrev = NULL;
- //(*l)->pFirst->pNext = NULL;
- // (*l)->pFirst->pPrev = NULL;
- //(*l)->pLast->pNext = NULL;
- // (*l)->pLast->pPrev = NULL;
- }
- DLL *listPtr;
- //
- //Node * giveName( DLL ** ppl, char Name[50] )
- //{
- // if( (*ppl) )
- // {
- // listPtr->pNext= NULL;// listPtr->pPrev= NULL;
- // strcpy(listPtr->pName,Name);
- // listPtr->pNext = listPtr;
- // }
- // return listPtr;
- //}
- void insertAtRear ( DLL ** listPtr, char Name[50])
- {
- Node* p = (Node*)malloc(sizeof(Node));
- strcpy(p->pName,Name);
- p->pNext = NULL;
- if((*listPtr)->pLast == NULL) /// when the list is EMPTY
- {
- (*listPtr)->pFirst = (*listPtr)->pLast = p;
- p->pNext=(*listPtr)->pFirst;
- }
- else ///when the list is not EMPTY
- {
- (*listPtr)->pLast->pNext = p; // chain after the former last node
- p->pNext = (*listPtr)->pFirst; // p is after the former last node
- (*listPtr)->pLast = p; // p is the last node
- }
- (*listPtr)->cnt++;
- }
- void deleteNode ( DLL * l, char Name[50])
- {
- Node * p;
- Node * prevPtr;
- p = l->pFirst;
- prevPtr = l->pLast;
- int j;
- for( j=0; j < l->cnt; j++)
- {
- if( stricmp ( p->pName, Name) == 0 )
- {
- prevPtr->pNext = p->pNext;
- if( p == l->pFirst)
- l->pFirst = p->pNext;
- if( p == l-> pLast)
- l->pLast = prevPtr;
- l->cnt--;
- break;
- }
- prevPtr=p;
- p = p->pNext;
- }
- }
- char * Circle ( DLL * l , int nrKids)
- {
- while ( l->cnt != 1)
- {
- int counter=1;
- Node *p = l->pFirst;
- while(counter < nrKids && p)
- {
- p = p->pNext;
- counter++;
- }
- deleteNode(l, p->pName);
- }
- return l->pFirst->pName;
- }
- void Display(DLL *l, int n, FILE *O)
- {
- Node *p = l->pFirst;
- if(p == NULL) fprintf(O,"Failed to create the list");
- while(p != NULL)
- {
- int i;
- for(i=0; i<n; i++)
- {
- fprintf(O,"%s \n", p->pName);
- p = p->pNext;
- }
- }
- }
- int main ()//int argc, int * argv[])
- {
- DLL * Game;
- Game = (DLL*) malloc(sizeof(DLL));
- initList(&Game);
- FILE *P = fopen("P.in","r");
- FILE *O = fopen("O.out","w");
- if( P == NULL )
- printf("The input file could not be opened.");
- if ( O == NULL )
- printf("The output file could not be opened.");
- int nrKids, i=0;
- char kidName[1050], junk[50];
- fscanf(P, "%d", &nrKids);
- fgets(junk,50,P);
- fprintf(O, "%d \n", nrKids);
- for(i=0; i<nrKids; i++)
- {
- fscanf(P,"%s", kidName);
- insertAtRear(&Game, kidName);
- }
- Display(Game, nrKids, O);
- fprintf("\n%s\n", Circle(Game, nrKids));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement