File tree Expand file tree Collapse file tree 1 file changed +72
-0
lines changed Expand file tree Collapse file tree 1 file changed +72
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @file stack_array.c
3+ * @author Xuhua Huang
4+ * @brief
5+ * @version 0.1
6+ * @date 2025-06-07
7+ *
8+ * @copyright Copyright (c) 2025
9+ *
10+ */
11+
12+ #include <limits.h>
13+ #include <stdbool.h>
14+ #include <stdio.h>
15+
16+ #define MAX_STACK_SIZE 10
17+ #define EMPTY (-1)
18+ #define STACK_EMPTY INT_MIN
19+
20+ int stack [MAX_STACK_SIZE ];
21+ int top = EMPTY ;
22+
23+ bool is_empty () {
24+ return top == EMPTY ;
25+ }
26+
27+ bool is_full () {
28+ return top == MAX_STACK_SIZE - 1 ;
29+ }
30+
31+ bool push (int value ) {
32+ if (is_full ()) {
33+ return false;
34+ }
35+ stack [++ top ] = value ;
36+ return true;
37+ }
38+
39+ int pop () {
40+ if (is_empty ()) {
41+ return STACK_EMPTY ;
42+ }
43+ int result = stack [top ];
44+ top -- ;
45+ return result ;
46+ }
47+
48+ int main (int argc , char const * argv [])
49+ {
50+ int value ;
51+
52+ // Test push
53+ for (int i = 0 ; i < 12 ; i ++ ) {
54+ if (push (i )) {
55+ printf ("Pushed %d onto stack\n" , i );
56+ } else {
57+ printf ("Stack is full, could not push %d\n" , i );
58+ }
59+ }
60+
61+ // Test pop
62+ for (int i = 0 ; i < 12 ; i ++ ) {
63+ value = pop ();
64+ if (value != STACK_EMPTY ) {
65+ printf ("Popped %d from stack\n" , value );
66+ } else {
67+ printf ("Stack is empty, could not pop\n" );
68+ }
69+ }
70+
71+ return 0 ;
72+ }
You can’t perform that action at this time.
0 commit comments