You are on page 1of 4

Main:

#include<iostream>
using namespace std;
#include"LinkedQueue.h"
void HotPotato(int N) {
LinkedQueue q;

for (int i = 0;i < N;i++) {//Creting Players


string n;
cout << "Enter Name of Player " << i + 1 << ": ";
cin >> n;
q.Enqueue(n);
}

for( ;N-1>0; N--){


int time = rand() % 10 + 1;
for(int t=1;t!=time;t++){
string name = q.GetFront();
q.Dequeue();
q.Enqueue(name);
}
cout << "--------------------------" << endl;
cout << q.GetFront() << " is Out of game" << endl;
q.Dequeue();
}
cout << "************************" << endl;
cout << "Winner of the Game is: ";
q.display();
cout << endl;
cout << "************************" << endl;

int main() {
char c;
do {
int n;
cout << "Enter Number Of players: ";
cin >> n;
HotPotato(n);
cout << "Do you wanna Play it Again \npress y for yes or n for no: ";
cin >> c;
} while (c=='y'||c=='Y');

system("pause");
return 0;
}
LinkedQueue.h
#pragma once
#include"Node.h"
class LinkedQueue
{
public:
Node* front, * rear; //Pointer for the front and rear of the queue
LinkedQueue(); //Constructor to initialize front and rear as NULL
bool isEmpty(); //To check if queue is empty i.e. front and rear are
NULL
void Enqueue(string); //Takes a string (name of child) as argument and
creates a node and inserts it after rear(be careful to update thenext of the new node to
point towards front)
void Dequeue(); //Deletes the first node of the queue pointed by front
(be careful to update the next of the rear node to point towards the new front)
string GetFront(); //Returns the value of the front node
void display(); //Displays the contents of the queue in the FIFO
order

};
LinkedQueue.CPP

#include "LinkedQueue.h"
LinkedQueue::LinkedQueue()
{
front = rear = NULL;
}
bool LinkedQueue::isEmpty()
{
if (front == NULL && rear == NULL)
return true;
else
return false;
}
void LinkedQueue::Enqueue(string x) {
Node* temp = new Node;
temp->data = x;
temp->next = NULL;
if (isEmpty())
{
front = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}
void LinkedQueue::Dequeue()
{
Node* temp = front;
if (isEmpty()) {
cout << "Queue Underflow"<<endl;
}
if (front == rear) {
front = rear = NULL;
}
else {
front = front->next;
}
delete temp;
}
string LinkedQueue::GetFront() {
if (isEmpty()) {
cout << "Queue is empty\n";
}
return front->data;
}

void LinkedQueue::display() {
Node* p;
p = front;
while (p!=NULL) {
cout<<p->data <<" ";
p = p->next;
}
}

You might also like