I've been working on a stack program for class for the past week or so, and i've completely hit a wall here. I'll post what I have so far. The problem I'm having is the last two functions, the isempty() and the ptintStack() functions.
Code:
#include <stdio.h>
#include <stdlib.h>
/* self-referential structure */
struct stackNode
{
int data; /* define data as an int */
struct stackNode *nextPtr; /* stackNode pointer */
}; /* end structure stackNode */
typedef struct stackNode StackNode; /* synonym for struct stackNode */
typedef StackNode *StackNodePtr; /* synonym for StackNode* */
/* prototypes */
void push( StackNodePtr *topPtr, int info );
int pop( StackNodePtr *topPtr );
void instructions( void );
void printStack( StackNodePtr currentPtr );
int isEmpty( StackNodePtr topPtr );
/* function main begins program execution */
int main()
{
StackNodePtr stackPtr = NULL; /* points to stack top */
int choice; /* user's menu choice */
int value; /* int input by user */
instructions(); /* display the menu */
printf( "? " );
scanf( "%d", &choice );
/* while user does not enter 3 */
while ( choice != 3 ) {
switch ( choice ) {
/* push value onto stack */
case 1:
printf( "Enter an integer: " );
scanf( "%d", &value );
push( &stackPtr, value );
printStack( stackPtr );
break;
/* pop value off stack */
case 2:
/* if stack is not empty */
if ( !isEmpty( stackPtr ) ) {
printf( "The popped value is %d.\n", pop( &stackPtr ) );
} /* end if */
printStack( stackPtr );
break;
default:
printf( "Invalid choice.\n\n" );
instructions();
break;
} /* end switch */
printf( "? " );
scanf( "%d", &choice );
} /* end while */
printf( "End of run.\n" );
return 0; /* indicates successful termination */
} /* end main */
/* display program instructions to user */
void instructions( void )
{
printf( "Enter choice:\n"
"1 to push a value on the stack\n"
"2 to pop a value off the stack\n"
"3 to end program\n" );
} /* end function instructions */
/* Insert a node at the stack top */
void push( StackNodePtr *topPtr, int info )
{
StackNodePtr newPtr; /* pointer to new node */
newPtr = malloc( sizeof( StackNode ) );
/* insert the node at stack top */
if ( newPtr != NULL ) {
newPtr->data = info;
newPtr->nextPtr = *topPtr;
*topPtr = newPtr;
} /* end if */
else { /* no space available */
printf( "%d not inserted. No memory available.\n", info );
} /* end else */
} /* end function push */
/* Remove a node from the stack top */
int pop( StackNodePtr *topPtr )
{
StackNodePtr tempPtr; /* temporary node pointer */
int popValue; /* node value */
tempPtr = *topPtr;
popValue = ( *topPtr )->data;
*topPtr = ( *topPtr )->nextPtr;
free( tempPtr );
return popValue;
} /* end function pop */
/* Print the stack */
void printStack( StackNodePtr currentPtr )
{
I'm stuck here!
} /* end function printList */
/* Return 1 if the stack is empty, 0 otherwise */
int isEmpty( StackNodePtr topPtr )
{
And here.
} /* end function isEmpty */
Can someone point me to a good tutorial on Stacks, i can't seem to google anything that simplifies it for me. And any help you all could give me would be much appreciated. This is the hardest time I've had so far with programming