Batch - B Practical Test Question

Batch - B Practical Test Question
Batch - B Practical Test Question

Doubly Circular Linked List Game Code

#include<stdio.h>
#include<memory.h>

struct Node
{
int value;
struct Node *next;
struct Node *prev;
};

struct Node *FIRST=NULL, *head=NULL, *temp=NULL;

void insert(int );
void printSeq();
void GameCode(int);

int main()
{
int no,i;
printf("Enter How Mnay Nodes you want to Add : ");
scanf("%d",&no);
for(i=0;i<no;i++)
{
insert(i+1);
}
printSeq();
GameCode(no);
printf("\n WINNER IS  : %d\n",FIRST->value);
return 0;
}

void GameCode(int no)
{
int i=0,j=1;
head=FIRST;
while(j<no)
{
i=1;
if(j%2==1)
{
while(i<no-j)
{
head=head->next;
i++;
}
temp=head;
if(temp==FIRST)
{
FIRST=temp->next;
}
head=head->next;
temp->prev->next=head;
head->prev=temp->prev;
printf("\n ITERATION : %d - DELETED NODE IS : %d",j,temp->value);
free(temp);
}
else
{
while(i<no-j)
{
head=head->prev;
i++;
}
temp=head;
if(temp==FIRST)
{
FIRST=temp->prev;
}
head=head->prev;
head->next=temp->next;
temp->next->prev=head;
printf("\n ITERATION : %d - DELETED NODE IS : %d",j,temp->value);
free(temp);
}
j++;
}
}

void insert(int no)
{
temp=(struct Node *)malloc(sizeof(struct Node)*1);
temp->value=no;
temp->next=NULL;
temp->prev=NULL;

if(FIRST==NULL)
{
FIRST=temp;
FIRST->next=FIRST;
FIRST->prev=FIRST;
}
else
{
head=FIRST;

while(head->next!=FIRST)
{
head=head->next;
}

head->next=temp;
temp->next=FIRST;
FIRST->prev=temp;
temp->prev=head;
}
}

void printSeq()
{
head=FIRST;
do
{
printf("%d -> ",head->value);
head=head->next;
}while(head!=FIRST);
}

Comments