Stacks

Discussion in 'C' started by boiishuvo, Aug 28, 2010.

  1. boiishuvo

    boiishuvo New Member

    Joined:
    Aug 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    That is a stack program - I tried to change from integers to characters so when you enter a single char of a-z. I ran the program then it displayed shown below:

    Enter a number: to stop: a
    Enter a number: to stop: Enter a number: to stop: b
    Enter a number: to stop: Enter a number: to stop: c
    Enter a number: to stop: Enter a number: to stop: d
    Enter a number: to stop: Enter a number: to stop: e
    Enter a number: to stop: Enter a number: to stop: ^Z


    The list of numbers reversed:
    Code:
    
      e
    
    
      d
    
    
      c
    
    
      b
    
    
      a
    Press any key to continue . . .
    
    
    Why did it appear the output twice "Enter a number:  to stop: Enter a number:  to stop:"?
    
    The code is below:
    
    
     #include 
    #include 
    #include "stacksADT.h"
    
    int main (void)
    {
            int  done = false;
            char* dataPtr;
    
            STACK* stack;
    
            stack = createStack ();
    
            while (!done)
               {
                dataPtr = (char*) malloc (sizeof(char));
                printf ("Enter a number:  to stop: ");
                if ((scanf ("%c" , dataPtr)) == EOF 
                       || fullStack (stack))
                   done = true;
                else
                   pushStack (stack, dataPtr);
               }
    
            printf ("\n\nThe list of numbers reversed:\n");
            while (!emptyStack (stack))
               {
                dataPtr = (char*)popStack (stack);
                printf ("%3c\n", *dataPtr);
                free (dataPtr);
               }
    
            destroyStack (stack);
            system("pause");
            return 0;
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice