-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSA_Ques6.c
132 lines (131 loc) · 2.65 KB
/
DSA_Ques6.c
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
#include <stdio.h>
int IsEmpty (int stack[], int count){
if (stack[0]==-1 && count==0){
return 1;
}
else{
return 0;
}
}
int IsFull (int stack[], int count, int SIZE){
if (count<=SIZE-1){
return 0;
}
else{
return 1;
}
}
int push(int stack[], int count){
int element;
printf("Enter the element to be inserted in the push: ");
scanf("%d",&element);
stack[count]=element;
count+=1;
return count;
}
int pop(int stack[], int count, int pos){
int i;
if(pos<=count){
for(i=count-pos;i<count-1;i++){
stack[i]=stack[i+1];
}
count-=1;
}
else{
printf("Index out of range. Pop Operation cannot be performed\n");
}
return count;
}
int s_top(int stack[], int count){
printf("The Top element of the Stack is: %d\n",stack[count-1]);
}
int display(int stack[], int count, int SIZE){
int i;
printf("The Stack is: \n");
for(i=0;i<count;i++){
printf("%d\t", stack[i]);
}
printf("\n");
}
int main(){
int SIZE, pos;
printf("Enter the number of elements: ");
scanf("%d",&SIZE);
int stack[100]={-1}, top=-1, option, conti='Y', count=0;
while(conti=='Y' || conti=='y'){
printf("------------INDEX---------------\n");
printf("1. Push Operation\n2. Pop Operation\n3. IsEmpty\n4. IsFull\n5. Top\n6. Display");
printf("\nEnter the operation to be performed: ");
scanf("%d",&option);
switch(option){
case 1: {
if(IsFull(stack,count,SIZE)==1){
printf("Push Operation cannot be performed\n");
printf("Stack is Full\n");
}
else{
count= push(stack, count);
}
break;
}
case 2: {
if(IsEmpty(stack, count)==1){
printf("Pop Operation cannot be performed\n");
printf("Stack is Empty\n");
}
else{
printf("Enter the position of the element to be popped: ");
scanf("%d",&pos);
count= pop(stack, count, pos);
}
break;
}
case 3: {
if(IsEmpty(stack, count)==1){
printf("Yes, Stack is EMPTY !\n");
}
else{
printf("No, Stack is not EMPTY !\n");
}
break;
}
case 4: {
if(IsFull(stack, count, SIZE)==1){
printf("The Stack is FULL !\n");
}
else{
printf("The Stack is not FULL !\n");
}
break;
}
case 5:{
if(IsEmpty(stack, count)==1){
printf("The Top element is not yet added.\n");
}
else{
s_top(stack, count);
}
break;
}
case 6:{
if(IsEmpty(stack, count)==1){
printf("The Stack cannot be displayed.\n");
}
else{
display(stack, count, SIZE);
}
break;
}
default: {
printf("Invalid Operation entered by user\n");
break;
}
}
printf("Do you want to continue? (Y/N): ");
scanf("\n%c",&conti);
if(conti=='N' || conti == 'n'){
break;
}
}
return 0;
}