home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************
- * STACK.C - simple demonstration of a stack *
- * *
- * To compile: "tcc stack1" *
- ****************************************************************/
-
- #include <stdio.h>
-
- /*------- Declare the stack variables --------*/
- #define MAX_STACK 10
- int stkArray[MAX_STACK];
- int stkIndex = -1;
-
- /*----- Push the data item on the stack ------*/
- void push(int data)
- {
- /* Does the stack have any room in it? */
- if (stkIndex == MAX_STACK-1)
- {
- fputs("ERROR: stack overflow\n",stderr);
- return;
- }
-
- /* Put the data in the stack */
- stkArray[++stkIndex] = data;
- }
-
- /*----- Pop the data item from the stack -----*/
- int pop(void)
- {
- /* Does the stack have anything in it? */
- if (stkIndex < 0)
- {
- fputs("ERROR: stack underflow\n",stderr);
- return -1;
- }
-
- /* Get the data off the stack */
- return stkArray[stkIndex--];
- }
-
- /*--------- Test the stack functions ---------*/
- void main(void)
- {
- int i;
-
- for (i=0; i<11; i++)
- push(i*3);
-
- for (i=9; i>-2; i--)
- if (i*3 != pop())
- fputs("ERROR: bad stack\n",stderr);
- }