File tree Expand file tree Collapse file tree 1 file changed +141
-0
lines changed Expand file tree Collapse file tree 1 file changed +141
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ #include < iostream>
3
+ using namespace std ;
4
+
5
+ class Stack
6
+ {
7
+ public:
8
+ int top;
9
+ int size;
10
+ int * Q;
11
+ bool isEmpty ();
12
+ bool isFull ();
13
+ void push (int x);
14
+ int pop ();
15
+ void display ();
16
+ };
17
+
18
+ bool Stack::isEmpty ()
19
+ {
20
+ if (top == -1 )
21
+ return true ;
22
+ return false ;
23
+ }
24
+
25
+ bool Stack::isFull ()
26
+ {
27
+ if (top == size - 1 )
28
+ return true ;
29
+ return false ;
30
+ }
31
+
32
+ void Stack::push (int x)
33
+ {
34
+ if (isFull ())
35
+ cout << " Stack Overflow" << endl;
36
+ else
37
+ {
38
+ top++;
39
+ Q[top] = x;
40
+ }
41
+ }
42
+
43
+ int Stack::pop ()
44
+ {
45
+ int x = -1 ;
46
+ if (isEmpty ())
47
+ cout << " Stack Underflow" << endl;
48
+ else
49
+ {
50
+ x = Q[top];
51
+ top--;
52
+ }
53
+ return x;
54
+ }
55
+
56
+ void Stack::display ()
57
+ {
58
+ int i;
59
+ for (i = 0 ; i <= top; i++)
60
+ cout << Q[i] << " " ;
61
+ cout << " " ;
62
+ }
63
+
64
+ class Queue
65
+ {
66
+ private:
67
+ Stack s1;
68
+ Stack s2;
69
+ public:
70
+ Queue (int size);
71
+ ~Queue ();
72
+ void enqueue (int x);
73
+ int dequeue ();
74
+ void Display ();
75
+ };
76
+
77
+ Queue::Queue (int size)
78
+ {
79
+ s1.top = -1 ;
80
+ s2.top = -1 ;
81
+
82
+ s1.size = size;
83
+ s2.size = size;
84
+
85
+ s1.Q = new int [size];
86
+ s2.Q = new int [size];
87
+ }
88
+
89
+ Queue::~Queue ()
90
+ {
91
+ delete[] s1.Q ;
92
+ delete[] s2.Q ;
93
+ }
94
+
95
+ void Queue::enqueue (int x)
96
+ {
97
+ s1.push (x);
98
+ }
99
+
100
+ int Queue::dequeue ()
101
+ {
102
+ int x = -1 ;
103
+ if (s2.isEmpty ())
104
+ {
105
+ if (s1.isEmpty ())
106
+ {
107
+ cout << " Both Queue underflow" << endl;
108
+ return x;
109
+ }
110
+ else
111
+ {
112
+ while (!s1.isEmpty ())
113
+ {
114
+ s2.push (s1.pop ());
115
+ }
116
+ }
117
+ }
118
+ x = s2.pop ();
119
+ return x;
120
+ }
121
+
122
+ void Queue::Display ()
123
+ {
124
+ s1.display ();
125
+ s2.display ();
126
+ }
127
+
128
+ int main ()
129
+ {
130
+ Queue q (5 );
131
+
132
+ q.enqueue (10 );
133
+ q.enqueue (20 );
134
+ q.enqueue (30 );
135
+ q.enqueue (40 );
136
+ q.enqueue (50 );
137
+
138
+ q.dequeue ();
139
+
140
+ q.Display ();
141
+ }
You can’t perform that action at this time.
0 commit comments