Thread: Push and Pop a string on the stack

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Push and Pop a string on the stack

    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:

    insert
    Code:
    #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;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    To start with, this small snippet of code has some problems:
    Code:
       StackNodePtr pStack = NULL;
       char string1[100];
       
      
       
      
      puts("Enter the string:");
      gets(string1);
      
       push( &pStack, string1 );
    1) You're defining pStack as a StackNodePtr, but you really just want it to be a StackNode. Then drop the NULL initialization and, instead, initialize the members of pStack.

    2) Never use gets().
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    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");
        }
      
    }
    Code in red is unreachable because of the return statement just above it. Would have thought the compiler would complain to you about that one.

    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.
    The way your stack is setup seems to be geared towards storing and printing entire strings, not individual characters. And even then, its flawed since what you are storing on the stack is a pointer to a local value (info) which would likely result in the stack - if you ever called it twice or more - appearing to store multiple copies of whatever the last string was pushed onto the stack.

    You stack needs to store individual characters not entire strings. When you push the string, what you really need to do is push each individual character onto the stack. This will require some redesign.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Thanks all for the quick reply.
    @hk_mp5kpdw: For storing individual characters, do I need to use a for loop?
    I tried using a for loop and entering the string but then after pushing the whole string my stack stores only the last word which I enter.
    eg: if I enter "This is a test", and I enter 1 word in one incrementation of fr loop. My stack shows: test->test->test->NULL

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by ami_31 View Post
    I tried using a for loop and entering the string but then after pushing the whole string my stack stores only the last word which I enter.
    eg: if I enter "This is a test", and I enter 1 word in one incrementation of fr loop. My stack shows: test->test->test->NULL
    That's exactly what I already explained is happening in my previous post:
    And even then, its flawed since what you are storing on the stack is a pointer to a local value (info) which would likely result in the stack - if you ever called it twice or more - appearing to store multiple copies of whatever the last string was pushed onto the stack.
    Quote Originally Posted by ami_31 View Post
    For storing individual characters, do I need to use a for loop?
    Yes, you read the entire string into some buffer, probably fgets would be best. Then you iterate (loop) through that buffer character by character till the end of the string and push individual characters onto your stack.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DIfference between , and ;
    By rocketman03 in forum C++ Programming
    Replies: 6
    Last Post: 10-21-2009, 09:46 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  4. Stack using push and pop
    By silicon in forum C++ Programming
    Replies: 5
    Last Post: 11-03-2003, 04:54 PM

Tags for this Thread