-
Notifications
You must be signed in to change notification settings - Fork 0
/
27.Stack_operations_using_array.c
116 lines (106 loc) · 2.88 KB
/
27.Stack_operations_using_array.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
#include<stdio.h>
#include<stdlib.h>
typedef struct{
int stackSize;
int top;
int *arr;
}stack;
void displayStack(stack * ptr){
printf("\nStack Elements : [ ");
for(int i=0; i<=ptr->top; i++){
printf("%d ", ptr->arr[i]);
}
printf("]\n\n");
}
int isEmpty(stack *ptr){
if(ptr->top == -1)
return 1;
return 0;
}
int isFull(stack *ptr){
if(ptr->top == ptr->stackSize-1)
return 1;
return 0;
}
void push(stack * ptr, int data){
if(isFull(ptr))
printf("Stack Overflow ! Cann't push %d in the stack.\n", data);
else{
ptr->top++;
ptr->arr[ptr->top] = data;
}
}
int pop(stack * ptr){
if(isEmpty(ptr)){
return -1;
}
else{
int item = ptr->arr[ptr->top];
ptr->top--;
return item;
}
}
int peek(stack * ptr, int pos){
if((ptr->top - pos + 1) < 0){
return -1;
}
return (ptr->arr[ptr->top - pos + 1]);
}
int main(){
stack * s = (stack *)malloc(sizeof(stack));
s->stackSize = 8;
s->top = -1;
s->arr = (int *)malloc(s->stackSize * sizeof(int));
int n = -1, a = -1, pos, element, popElement, peekElement;
printf("*** STACK OPERATIONS MENU***\n______________________________\nPush : 1\tPull : 2\tPeek Element : 3\nDisplay Stack : 4\tExit : 0\n");
while(n != 0){
printf("Enter your choice : ");
scanf("%d", &n);
switch(n){
case 1:
printf("Enter Element to push : ");
scanf("%d", &element);
push(s, element);
break;
case 2:
popElement = pop(s);
if(popElement == -1)
printf("Stack is Empty !\n");
else
printf("POP element : %d\n", popElement);
break;
case 3:
printf("=== MENU OF PICK OPERATION ===\n________________________________\nPeek Element from a position : 1\nPeek all Element : 2\n");
printf("Enter your choice : ");
scanf("%d", &a);
if(a == 1){
printf("Enter a position : ");
scanf("%d", &pos);
peekElement = peek(s, pos);
if(peekElement == -1)
printf("%d is not a valid position to peek element\n", pos);
else
printf("Element in %d position is : %d\n", pos, peekElement);
}
else if(a == 2){
for(int i = 1; i <= s->top+1; i++){
printf("Element in %d : %d\n", i, peek(s,i));
}
printf("\n");
}
else
printf("\nWrong Selection !\n");
break;
case 4:
displayStack(s);
break;
case 0:
exit(1);
break;
default:
printf("Wrong Selection! Try again\n");
}
}
getchar();
return 0;
}