Josepheus Problem using Queue Datastructure

Thursday, November 29th, 2012

Here is the program for Josepheus Problem using Queue Datastructure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "iostream"
using namespace std;
 
template <class Q>
 
class Queue
{
    int a[100];
    short int N,f,r;
 
public:
    Queue(short int s)
    {
        N=s;
        f=r=0;
        cout<<"The size of the queue is assigned as:::"<<N;
        cout<<endl;
    }
 
    Q size()
    {
        return (N-f+r)%N;
    }
 
    bool isEmpty()
    {
        if(r==f)
            return true;
        else
            return false;
    }
 
    Q front()
    {
        try
        {
            if(isEmpty()==true)
            {
                short int t=0;
                throw t;
            }
            else
                return a[f];
        }
        catch(...)
        {
            cout<<"Queue EmptyException"<<endl;
        }
    }
 
    void enQueue(Q m)
    { 
        try
        {
            if(size()==N-1)
            { 
                short int r=0;
                throw r;
            }
 
            else
            {
                a[r]=m;
                r=(r+1)%N;
            }
        }
        catch(...)
        {
            cout<<"Queue full exception"<<endl;
        }
    }
    Q deQueue()
    {
 
        try
        {
            if(isEmpty()==true)
            {
                short int k=0;
                throw k;
            }
            else
            {
                short int temp;
                temp=a[f];
                f=(f+1)%N;
                return temp;
            }
        }
        catch(...)
        {
            cout<<"Queue Empty Exception"<<endl;
        }
    }
 
};
 
int main()
{ 
    char s; 
    int a; 
    cout<<"---------------------------"<<endl;
    cout<<"Welcome to Josephus Problem"<<endl;
    cout<<"---------------------------"<<endl;
    cout<<"press any key to continue"<<endl;
    getchar();
    do
    {
        cout<<"1.New Game"<<endl; 
        cout<<"2.Rules"<<endl; 
        cout<<"3.Exit"<<endl; 
        cout<<"Enter the choice"<<endl; 
        cin>>a; 
 
        switch(a) 
        {
 
        case 1:
            {
                short int j;
                cout<<"Enter the size of the Queue"<<endl;
                cin>>j;
                Queue<int>q1(j);
                short int n,k;
                int count=1;
                cout<<"Enter the number of people::"<<endl;
                cin>>n;
                cout<<"Enter the number of Successive cut"<<endl;
                cin>>k;
 
                for(int i=1;i<=n;i++)
                {
                    q1.enQueue(i);
                }
 
                while(n>0)
                {
 
                    if(q1.size()==1)
                    {
                        int a;
                        a=q1.deQueue();
                        cout<<"The person escaped is"<<a<<endl;
                        break;
                    }
                    else
                    {
                        if(count%k==0)
                        {
                            q1.deQueue();
                            n--;
                            count++;
                            continue;
                        }
                        else
                        {
                            short int b;
                            b=q1.deQueue();
                            q1.enQueue(b);
                            count++;
                            continue;
                        }
                    }
                }
 
                break;
            }
 
        case 2:
            {
                cout<<"Enter the number of people and number of successive cuts";
                cout<<"the person who survives the cut during every repition of 
                    the loop is considered to be escaped"<<endl;
                break;
            }
 
        case 3:
            {
                cout<<"Do you want to exit press E"<<endl;
                break;
            }
        }
        cout<<"press S to continue"<<endl;
        cin>>s;
    }while(s=='S');
}
Avatar of Arun

Author Name :
Arun

Total : 0 Comment


Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: