forked from Gaurang2908/HacktoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.cpp
101 lines (100 loc) · 1.97 KB
/
1.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
#include<stdio.h>
#define N 5
int stack1[5], stack2[5]; // declaration of two stacks
// declaration of top variables.
int top1=-1, top2=-1;
int count=0;
// inserting the elements in stack1.
void push1(int data)
{
// Condition to check whether the stack1 is full or not.
if(top1==N-1)
{
printf("\n Stack is overflow...");
}
else
{
top1++; // Incrementing the value of top1
stack1[top1]=data; // pushing the data into stack1
}
}
// Removing the elements from the stack1.
int pop1()
{
// Condition to check whether the stack1 is empty or not.
if(top1==-1)
{
printf("\nStack is empty..");
}
else
{
int a=stack1[top1]; // Assigning the topmost value of stack1 to 'a' variable.
top1--; // decrementing the value of top1.
return a;
}
}
// pushing the data into the stack2.
void push2(int x)
{
// Condition to check whether the stack2 is full or not
if(top2==N-1)
{
printf("\nStack is full..");
}
else
{
top2++; // incrementing the value of top2.
stack2[top2]=x; // assigning the 'x' value to the Stack2
}
}
// Removing the elements from the Stack2
int pop2()
{
int element = stack2[top2]; // assigning the topmost value to element
top2--; // decrement the value of top2
return element;
}
void enqueue(int x)
{
push1(x);
count++;
}
void dequeue()
{
if((top1==-1) && (top2==-1))
{
printf("\nQueue is empty");
}
else
{
for(int i=0;i<count;i++)
{
int element = pop1();
push2(element);
}
int b= pop2();
printf("\nThe dequeued element is %d", b);
printf("\n");
count--;
for(int i=0;i<count;i++)
{
int a = pop2();
push1(a);
}
}}
void display()
{
for(int i=0;i<=top1;i++)
{
printf("%d , ", stack1[i]);
}
}
int main()
{
enqueue(10);
enqueue(20);
enqueue(30);
dequeue();
enqueue(40);
display();
}