Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc9231eb

Browse files
committed
Create stack_array.c
1 parent822db1a commitc9231eb

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

‎Stack/stack_array.c‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
#defineMAX_STACK_SIZE 10
17+
#defineEMPTY (-1)
18+
#defineSTACK_EMPTY INT_MIN
19+
20+
intstack[MAX_STACK_SIZE];
21+
inttop=EMPTY;
22+
23+
boolis_empty() {
24+
returntop==EMPTY;
25+
}
26+
27+
boolis_full() {
28+
returntop==MAX_STACK_SIZE-1;
29+
}
30+
31+
boolpush(intvalue) {
32+
if (is_full()) {
33+
return false;
34+
}
35+
stack[++top]=value;
36+
return true;
37+
}
38+
39+
intpop() {
40+
if (is_empty()) {
41+
returnSTACK_EMPTY;
42+
}
43+
intresult=stack[top];
44+
top--;
45+
returnresult;
46+
}
47+
48+
intmain(intargc,charconst*argv[])
49+
{
50+
intvalue;
51+
52+
// Test push
53+
for (inti=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 (inti=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+
return0;
72+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp