-
Notifications
You must be signed in to change notification settings - Fork 46
/
stack1.c
54 lines (44 loc) · 1.08 KB
/
stack1.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
/*********************************************************
* From C PROGRAMMING: A MODERN APPROACH, Second Edition *
* By K. N. King *
* Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
* All rights reserved. *
* This program may be freely distributed for class use, *
* provided that this copyright notice is retained. *
*********************************************************/
/* stack1.c (Chapter 19, page 488) */
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#define STACK_SIZE 100
static int contents[STACK_SIZE];
static int top = 0;
static void terminate(const char *message)
{
printf("%s\n", message);
exit(EXIT_FAILURE);
}
void make_empty(void)
{
top = 0;
}
bool is_empty(void)
{
return top == 0;
}
bool is_full(void)
{
return top == STACK_SIZE;
}
void push(int i)
{
if (is_full())
terminate("Error in push: stack is full.");
contents[top++] = i;
}
int pop(void)
{
if (is_empty())
terminate("Error in pop: stack is empty.");
return contents[--top];
}