-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoal22.c
91 lines (84 loc) · 1.86 KB
/
goal22.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
// operations(push, pop, isempty, isfull) on stack using array
#include <stdio.h>
#include <stdlib.h>
struct stack
{
int size;
int top;
int *arr;
};
int isempty(struct stack *ptr)
{
if (ptr->top == -1)
{
return 1;
}
return 0;
};
int isfull(struct stack *ptr)
{
if (ptr->top == ptr->size - 1)
{
return 1;
}
return 0;
};
void push(struct stack * ptr, int val)
{
if(isfull(ptr))
{
printf("the stack is overflowing, cannot push %d.\n",val);
}
else
{
ptr->top++;
ptr->arr[ptr->top] = val;
}
};
int pop(struct stack * ptr)
{
int val;
if(isempty(ptr))
{
printf("the stack is underflowing, cannot pop.\n");
return -1;
}
else
{
int val = ptr->arr[ptr->top];
ptr->arr[ptr->top] = val;
ptr->top--;
return val;
}
};
int main()
{
struct stack *sp = (struct stack *)malloc(sizeof(struct stack));
sp->size = 10;
sp->top = -1;
sp->arr = (int *)malloc(sp->size * sizeof(int));
printf("the stack has been created successfully.\n");
printf("%d, here 1 means full and 0 means not full\n", isfull(sp));
printf("%d, here 1 means empty and 0 means not empty\n", isempty(sp));
push(sp, 23);
push(sp, 24);
push(sp, 25);
push(sp, 26);
push(sp, 27);
push(sp, 28);
push(sp, 29);
push(sp, 30);
push(sp, 31);
push(sp, 32);
push(sp, 33);
push(sp, 34);
push(sp, 35);
printf("%d, here 1 means full and 0 means not full\n", isfull(sp));
printf("%d, here 1 means empty and 0 means not empty\n",isempty(sp));
printf("%d\n",pop(sp));
printf("%d\n",pop(sp));
printf("%d\n",pop(sp));
printf("%d, here 1 means full and 0 means not full\n", isfull(sp));
printf("%d, here 1 means empty and 0 means not empty\n",isempty(sp));
return 0;
}