-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
55 lines (51 loc) · 970 Bytes
/
stack.c
File metadata and controls
55 lines (51 loc) · 970 Bytes
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
#include <stdio.h>
// Push Operation
void push(void *arr,int max,int *top, void *value, int data_type) {
if(*top + 1 >= max)
printf("Stack Overflow !\n");
else{
switch(data_type)
{
case 1:
((int *)arr)[*top + 1] = *((int *)value);
*top += 1;
break;
case 2:
((char *)arr)[*top + 1] = *((char *)value);
*top += 1;
break;
}
}
}
void* pop(void *arr, int *top, int data_type){
if(*top >= 0){
void *r;
switch(data_type){
case 1:
r = ((int*)arr+ *top);
*top -= 1;
return r;
case 2:
r = ((char*)arr+*top);
*top -= 1;
return r;
}
}
else{
printf("Stack Underflow !\n");
return NULL;
}
}
void* peek(void *arr, int *top, int data_type){
if(*top >= 0){
switch(data_type){
case 1:
return ((int*)arr + *top);
case 2:
return ((char*)arr + *top);
}
}else{
printf("Stack is empty\n");
return NULL;
}
}