Scenario
Consider a game in which players
are arranged
circularly. The
game has to be played in a way demonstrared as
follows.
1.) The games starts from first player as current player.
2.)A random number(n) from 1 to 10 is generated.
3.) *The count goes from the
current player and deletes
the
Nth player…
*After deleting the player,
now the game starts from the player
next to the deleted player…
*The game is to be stopped if there is only
one player and the
player is declared as a winner…
(Design
an algorithm for the game)
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *prev;
struct Node *next;
}Node;
Node *start=NULL;
void insert(Node *temp)
{
Node *var,*last;
Node *ptr=start;
if(start==NULL)
{
temp->next=temp;
temp->prev=temp;
start=temp;
}
else
{
while(ptr->next!=start)
{
ptr=ptr->next;
}
last=start->prev;
ptr->next=temp;
temp->prev=last;
last->next=temp;
temp->next=start;
start->prev=temp;
}
}
void display(Node *start)
{
Node *ptr=start;
if(start==NULL)
printf("\n No elements present");
else
{
printf("\n Elements in the Linked List are:");
while(ptr->next!=start)
{
printf("\n%d",ptr->data);
ptr=ptr->next;
}
printf("\n%d",ptr->data);
}
return;
}
int delete1(Node *start,int n)
{
Node *t=start,*p,*q;
int count;
while(t->next!=t->next->next)
{
count=1;
while(count<n)
{
t=t->next;
count++;
}
p=t->prev; q=t->next;
p->next=q; q->prev=p;
free(t);
t=q;
}
return t->data;
}
Node *createNode(int data)
{
Node *temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=data;
return temp;
}
int main()
{
int data;
Node *temp;
temp=createNode(10);
insert(temp);
temp=createNode(20);
insert(temp);
temp=createNode(30);
insert(temp);
temp=createNode(40);
insert(temp);
temp=createNode(50);
insert(temp);
display(start);
data=delete1(start,4); /*Winner */
printf("\n Winner is :: %d",data);
return 0;
}
O/P: when N=4
No comments:
Post a Comment