-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy_one_stack_into_another_stack.c
More file actions
106 lines (91 loc) · 1.7 KB
/
Copy_one_stack_into_another_stack.c
File metadata and controls
106 lines (91 loc) · 1.7 KB
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
// Copy one stack elements into another stack
#include <stdio.h>
#define MAX 50
int stack1[MAX], top1 = -1, temp_stack[MAX], temp_top = -1, stack2[MAX], top2 = -1;
void push(int item)
{
if (top1 == MAX - 1)
{
printf("Stack Overflow.\n");
}
else
{
top1++;
stack1[top1] = item;
}
}
int pop()
{
int var;
if (top1 == -1)
{
printf("Stack underflow.\n");
}
else
{
var = stack1[top1];
top1--;
}
return var;
}
void display_original_stack()
{
int i;
printf("After push elements the stack is\n");
for (i = 0; i <= top1; i++)
{
printf("%d ", stack1[i]);
}
}
void display_copied_stack()
{
int i;
printf("\n\nAfter copy elements from original stack is\n");
for (i = 0; i <= top2; i++)
{
printf("%d ", stack2[i]);
}
}
int temp_stack_is_full()
{
return temp_top == MAX - 1;
}
int copied_stack_is_full()
{
return top2 == MAX - 1;
}
int main()
{
int n, i, j;
printf("Enter size of stack\n");
scanf("%d", &n);
if (n > MAX)
{
printf("Overflow.\n");
}
else
{
printf("Enter Stack Elements\n");
for (i = 0; i < n; i++)
{
scanf("%d", &stack1[i]);
push(stack1[i]);
}
display_original_stack();
if (!temp_stack_is_full())
{
for (i = 0; i < n; i++)
{
temp_stack[++temp_top] = pop();
}
}
if (!copied_stack_is_full())
{
for (j = 0; j < n; j++)
{
stack2[++top2] = temp_stack[temp_top--];
}
}
display_copied_stack();
}
}