#include #include /* Write a Stack class. Stores ints. */ /* Limit 10 items in the stack */ #define MAX_STACK_SIZE 10 #define OVER_FLOW_OR_UNDERFLOW 100 struct stack { int arr[ MAX_STACK_SIZE ]; int topOfStack; }; typedef struct stack STACK; #define absValue( x ) ( ( (x) > 0 ) ? (x) : -(x) ) int z = 1000; int (absValue)( int x ) { return ( x > 0 ) ? x : -x; } void push_stack( STACK *s, int x ) { if( s->topOfStack == MAX_STACK_SIZE ) exit( OVER_FLOW_OR_UNDERFLOW ); s->arr[ ++s->topOfStack ] = x; } #define isEmpty_stack( s ) ( (s)->topOfStack == -1 ) int (isEmpty_stack)( const STACK *s ) { return s->topOfStack == -1; } int pop_stack( STACK *s ) { if( isEmpty_stack( s ) ) exit( OVER_FLOW_OR_UNDERFLOW ); return s->arr[ s->topOfStack-- ]; } void init_stack( STACK *s ) { s->topOfStack = -1; } STACK * create_stack( ) { STACK *theStack = (STACK *) malloc( sizeof( STACK ) ); init_stack( theStack ); return theStack; } void dispose_stack( STACK *s ) { free( s ); } int compareInts( const void *lhs, const void *rhs ) { int first = *( const int *)lhs; int second = *( const int *)rhs; return first - second; } int main( ) { int arr[ ] = { 6, 43, 3, 8, 12 }; int i = 0; STACK s1; STACK *s2; init_stack( &s1 ); s2 = create_stack( ); i = -4; printf( "%d\n", absValue( -100 ) ); printf( "%x\n", absValue ); printf( "%x\n", &z ); while( i < 5 ) { push_stack( &s1, (absValue)( ++i+3 ) * 2 ); push_stack( s2, (i+3)*(i+3) ); } while( !isEmpty_stack( &s1 ) ) printf( "%d %d\n", pop_stack( &s1 ), pop_stack( s2 ) ); dispose_stack( s2 ); qsort( arr, 5, sizeof( int ), compareInts ); printf( "After sort:\n" ); for( i = 0; i< 5; i++ ) printf( "%d\n", arr[ i ] ); return 0; }