home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************************
- * File Name: stacks.cpp
- * Description: Using Stacks
- * Author: Arlyn K. Chesley
- * Date: 2/15/96 started
- *
- * Program description Purpose:
- * To understand how Stacks work. A Stack is a FILO(first-in-last-out)
- * Use a debugger to step through this program. Watch the stacks as
- * items are added to and removed from the stack.
- * Need to implement 4 functions stack_new, stack_push, stack_pop,
- * and stack_free
- *************************************************************************/
-
- #include <stdio.h>
- #include <stdlib.h>
-
-
- /************************************************************************
- * function prototyping
- *************************************************************************/
- int stack_new(
- int size, /* I-size of a single element */
- int count, /* I-Maximum number of data elements */
- long *key); /* O-Key to the newly created stack area */
-
- int stack_push(
- long key, /* I-Key to the stack to operate upon */
- char *data); /* I-Pointer to data element to be saved */
-
- int stack_pop(
- long key, /* I-Key to the stack to operate upon */
- char *data); /* O-Place retrieved data is to be copied to */
-
- int stack_free(
- long key); /* I-Key to the stack to operate upon */
-
- #define RC_OK 0 /* Operation successful */
- #define RC_OVERFLOW 1 /* Stack overflow condition encountered */
- #define RC_UNDERFLOW 2 /* Stack underflow condition encountered */
- #define RC_MALLOC_ERROR 3 /* Memory allocation error occcurred */
-
- #include "d:\advanc_c\source_c\p1.cpp"
-
- main()
- {
- char str[80]; /* Buffer to hold the string */
- int ndx; /* Index into the string buffer */
- long number_key; /* Identifies stack which holds numbers */
- long letter_key; /* Identifies stack which holds letters */
- long work_key; /* Stack to use for current character */
- char value; /* Value popped from the stack */
- int rc; /* Status return code */
-
- /***************************************************************************
- * Allocate a stack to hold the numbers
- ****************************************************************************/
-
- rc = stack_new(sizeof(char), 40, &number_key);
- if(rc != RC_OK)
- { /* Number failure */
- printf("Error %d allocating number stack\n", rc);
- exit(0);
- } /* Number failure */
-
- /***************************************************************************
- * Allocate a stack to hold the letters
- ****************************************************************************/
-
- rc = stack_new(sizeof(char), 40, &letter_key);
- if(rc != RC_OK)
- { /* Letter failure */
- printf("Error %d allocating character stack\n", rc);
- exit(0);
- } /* Letter failure */
-
- /***************************************************************************
- * Process string read from the keyboard until an EOF is
- * encountered or until a empty string is retrieved
- ****************************************************************************/
- while(gets(str) !=NULL && str[0] != '\0')
- { /* String found */
- /************************************************************************
- * Look at each character in the string. Seperate them into
- * numeric characters and letter characters.
- *************************************************************************/
- for(ndx = 0; str[ndx] != '\0'; ndx++)
- { /* Process string */
- if(str[ndx] >= '0' && str[ndx] <= '9')
- work_key = number_key; /* Save to the number stack */
- else
- work_key = letter_key; /* Save to the letter stack */
- /********************************************************************
- * Push the character onto it's stack.
- *********************************************************************/
-
- rc = stack_push(work_key, &str[ndx]);
- if(rc != RC_OK)
- { /* Error */
- printf("Error %d pushing %c onto stack\n", rc, str[ndx]);
- exit(0);
- } /* Error */
- } /* Process string */
- /*************************************************************************
- * Print the numbers from the string in reverse order. Note: the
- * print operation will empty the stack which should reset it for
- * the next string
- *************************************************************************/
- printf("Numbers in reverse order are: ");
- rc = stack_pop(number_key, &value);
- while(rc == RC_OK)
- { /* Print it */
- printf("%c", value);
- rc = stack_pop(number_key, &value);
- } /* Print it */
- if(rc == RC_UNDERFLOW)
- { /* Number print errror */
- printf("Error %d while printing numbers\n", rc);
- exit(0);
- } /* Number print error */
- /*************************************************************************
- * Print the letters from the string in reverse order. Note: the
- * print operation will empty the stack which should reset it for the
- * next string.
- *************************************************************************/
-
- printf("\n\nLetters in reverse order are: ");
- while(stack_pop(letter_key, &value) == RC_OK)
- printf("%c", value);
- if(rc == RC_UNDERFLOW)
- { /* Number print error */
- printf("Error %d while printing letters\n", rc);
- exit(0);
- } /* Number print error */
- printf("\n\n"); /* Give us some space between runs */
- } /* String found */
- /*************************************************************************
- * We're done. Don't forget to free the stacks we allocated.
- *************************************************************************/
-
- stack_free(number_key);
- stack_free(letter_key);
- return(0);
- } /* Main */