Hi All!
I want to push a string on the stack and read it in reverse order which can be done by pop function. But I m not able to do so.
Pls can you help me finding out where I am gonig wrong.
below is my code:
insertCode:#define _CRT_SECURE_NO_WARNINGS // stack.c #include <stdio.h> #include <stdlib.h> #include <string.h> // self-referential structure struct stackNode { char* data; struct stackNode *pNext; }; typedef struct stackNode StackNode; typedef StackNode *StackNodePtr; // function prototypes void push( StackNodePtr *pTop, char info[100] ); char* pop( StackNodePtr *pTop); char isEmpty( StackNodePtr pTop ); void printStack( StackNodePtr pCurrent ); int main( void ) { StackNodePtr pStack = NULL; char string1[100]; puts("Enter the string:"); gets(string1); push( &pStack, string1 ); printStack( pStack ); printf("The popped value is %s", pop(&pStack)); return 0; } void push( StackNodePtr *pTop, char info[100] ) { StackNodePtr pNew; pNew = malloc( sizeof( StackNode ) ); if ( pNew != NULL ) { pNew->data = info; pNew->pNext = *pTop; // insert at top of stack *pTop = pNew; } else { printf( "%s not inserted. No memory available.\n", info ); } } char* pop( StackNodePtr *pTop) { StackNodePtr pTemp; char *sPopValue = {0}; pTemp = *pTop; // attach a pointer to element to be removed sPopValue = ( *pTop )->data; printf("popped item is %s", sPopValue); *pTop = ( *pTop )->pNext; // remove at top of stack free( pTemp ); // release this memory and set it free! return sPopValue; if(*pTop == NULL) { printf("Stack is empty"); } } // output stack contents to the console void printStack( StackNodePtr pCurrent ) { if ( pCurrent == NULL ) { printf( "The stack is empty.\n\n" ); } else { printf( "The stack is:\n" ); while ( pCurrent != NULL ) { printf( "%s -> ", pCurrent->data ); pCurrent = pCurrent->pNext; // move to next element } printf( "NULL\n\n" ); } } char isEmpty( StackNodePtr pTop ) { return pTop == NULL; }