Skip to content

Commit 4d9b0eb

Browse files
committed
added push and pop operations for stack.c
1 parent 6c489ca commit 4d9b0eb

File tree

1 file changed

+14
-2
lines changed
  • Ch4_Functions_Program_Structure/calc

1 file changed

+14
-2
lines changed

Ch4_Functions_Program_Structure/calc/stack.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,22 @@
66
int sp = 0;
77
double val[maxval];
88

9-
void push(double){
10-
9+
void push(double f){
10+
if (sp < MAXVAL){
11+
val[sp++] = f;
12+
}
13+
else{
14+
printf("error: stack full, can't push %g\n", f);
15+
}
1116
}
1217

1318
double pop(void){
19+
if (sp > 0){
20+
return val[--sp];
21+
}
22+
else{
23+
printf("error: stack empty\n");
24+
return 0.0;
25+
}
1426

1527
}

0 commit comments

Comments
 (0)