File tree Expand file tree Collapse file tree 2 files changed +75
-0
lines changed Expand file tree Collapse file tree 2 files changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ struct Queue {
5
+ stack<int > s1, s2;
6
+ // Add an item to the queue
7
+ void enQueue (int x)
8
+ {
9
+ // Push item into the first stack (s1)
10
+ s1.push (x);
11
+ }
12
+ // Remove an item from the queue
13
+ int deQueue ()
14
+ {
15
+ // If both stacks are empty, then exit
16
+ if (s1.empty () && s2.empty ()){
17
+ cout << " Q is empty" ;
18
+ exit (0 );
19
+ }
20
+
21
+ // If second stack (s2) is empty, move elements from first stack (s1) to second stack (s2)
22
+ if (s2.empty ()) {
23
+ while (!s1.empty ()){
24
+ s2.push (s1.top ());
25
+ s1.pop ();
26
+ }
27
+ }
28
+ // Return the top item from secodn stack (s2)
29
+ int x = s2.top ();
30
+ s2.pop ();
31
+ return x;
32
+ }
33
+ void sizeQueue ()
34
+ {
35
+ cout<<" Size of the Queue: " <<s1.size ()+s2.size ()<<endl;
36
+ }
37
+ };
38
+
39
+ int main ()
40
+ {
41
+ Queue q;
42
+ int x, data;
43
+ char ch;
44
+ do
45
+ {
46
+ cout<<" \t Main Menu \n " ;
47
+ cout<<" \t 1. Insertion to Queue \n " ;
48
+ cout<<" \t 2. Deletion from Queue \n " ;
49
+ cout<<" \t 3. Size of Queue\n " ;
50
+ cout<<" \t 4. Quit \n " ;
51
+ cout<<" Enter your choice: " ;
52
+ cin>>x;
53
+ switch (x)
54
+ {
55
+ case 1 : cout<<" Enter the value to be inserted in Queue: " ;
56
+ cin>>data;
57
+ q.enQueue (data);
58
+ break ;
59
+ case 2 : cout<<" The deleted element is: " <<q.deQueue ()<<endl;
60
+ break ;
61
+ case 3 : q.sizeQueue ();
62
+ break ;
63
+ case 4 : exit (0 );
64
+ default : cout<<" Wrong Choice Entered\n " ;
65
+ }
66
+ cout<<" Do you want to go to the main menu (y/n): " ;
67
+ cin>>ch;
68
+ }
69
+ while (ch==' Y' ||ch==' y' );
70
+ return 0 ;
71
+ }
Original file line number Diff line number Diff line change @@ -4,6 +4,10 @@ This is a repository containing various C++ Programs to understand the basic con
4
4
5
5
## Programs
6
6
7
+ * [ Queue Using Stack] ( https://github.com/altruistcoder/Data-Structures/blob/master/Queue/queue_using_stack.cpp ) :
8
+
9
+ C++ Code for implementing a queue using stacks.
10
+
7
11
* [ Merge Sort] ( https://github.com/altruistcoder/Data-Structures/blob/master/Sorting%20Programs/merge_sort.cpp ) :
8
12
9
13
C++ Code for implementing Merge Sort algorithm.
You can’t perform that action at this time.
0 commit comments