Lab 5 Dsa
Lab 5 Dsa
#define MAX 5
class Stack {
int top;
int *a;
public:
// Maximum size of Stack
int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}
int Stack::Top()
{
if (top < 0) {
cout << "Stack is Empty\n";
return 0;
}
else {
int x = a[top];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
bool Stack::isFull()
{
return (top >= MAX-1);
}
}
return 0;
}
Implement the Josephus problem as follows.
1. Decide the number of participants ‘n’ and generate a doubly linked list.
2. Modify the Doubly linked list to circular linked list by pointing the first
element of the list by
the last element.
3. Choose the number of participants ‘k’ to be skipped.
4. Pick a starting point and remove every (k+1) th participant.
5. Continue until a single participant remains. That’s the leader.
#include <bits/stdc++.h>
node *createDCLL(int n)
{
node *head = NULL;
node *ptr = head, *ptr_prev = head;
do {
cout<<ptr->data <<" ";
ptr = ptr->next;
} while(ptr != head);
cout<<endl;
}
int main()
{
node *leader = NULL;
int n, k;
cout<<"Enter k : ";
cin>> k;
display(leader);
cout<<"leader is : " <<josephus(leader, k);
return 0;
}