forked from dharmanshu1921/Daa-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsaquestion.cpp
197 lines (171 loc) · 3.28 KB
/
dsaquestion.cpp
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
187
188
189
190
191
192
193
194
195
196
197
/*#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
class Stack{
private:
int top;
char arr[4];
public:
Stack ()
{
top = -1;
for(int i=0;i<4;i++)
{
arr[i]=0;
}
}
void push(char value)
{
if(top==4)
{
cout<<"stack overflow"<<endl;
}
else
{
top++;
arr[top]=value;
}
}
int pop()
{
if(top==-1)
{
cout<<"stack underflow"<<endl;
return 0;
}
else
{
int popvalue = arr[top];
arr[top]=0;
top--;
return popvalue;
}
}
void display()
{
cout<<"all values of stack are"<<endl;
for(int i=3;i>=0;i--)
{
cout<<arr[i]<<endl;
}
}
};
int main(){
char value;
Stack s1;
cout<<"Enter first char"<<endl;
cin >> value;
s1.push(value);
cout<<"Enter second char"<<endl;
cin >> value;
s1.push(value);
cout<<"Enter third char"<<endl;
cin >> value;
s1.push(value);
cout<<"Enter fourth char"<<endl;
cin >> value;
s1.push(value);
s1.display();
s1.pop();
s1.display();
s1.pop();
s1.display();
s1.pop();
s1.display();
s1.pop();
s1.display();
return 0;
}*/
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
class Queue{
private:
int front;
int rear;
char arr[4];
public:
Queue() {
front = -1;
rear = -1;
for (int i = 0; i < 5; i++) {
arr[i] = 0;
}
}
bool isEmpty() {
if (front == -1 && rear == -1)
return true;
else
return false;
}
bool isFull(){
if(rear==4)
return true;
else
return false;
}
void enqueue(int val){
if(isFull()){
cout<<"Queue Full"<<endl;
}
else if(isEmpty()){
rear=0;
front=0;
arr[rear]=val;
}
else{
rear++;
arr[rear]=val;
}
}
int dequeue() {
int x = 0;
if (isEmpty()) {
cout <<endl<< "Queue is Empty" << endl;
return x;
} else if (rear == front) {
x = arr[rear];
rear = -1;
front = -1;
return x;
} else {
cout <<endl<< "front value: " << front << endl;
x = arr[front];
arr[front] = 0;
front++;
return x;
}
}
void display() {
cout << endl<<"All values in the Queue are - " << endl;
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
}
};
int main(){
char value;
Queue s1;
cout<<"Enter first char"<<endl;
cin >> value;
s1.enqueue(value);
cout<<"Enter second char"<<endl;
cin >> value;
s1.enqueue(value);
cout<<"Enter third char"<<endl;
cin >> value;
s1.enqueue(value);
cout<<"Enter fourth char"<<endl;
cin >> value;
s1.enqueue(value);
s1.display();
s1.dequeue();
s1.display();
s1.dequeue();
s1.display();
s1.dequeue();
s1.display();
s1.dequeue();
s1.dequeue();
return 0;
}