need some help on link lists using stacks...

This is a discussion on need some help on link lists using stacks... within the C Programming forums, part of the General Programming Boards category; no errors and warnings...but dowanna accaept input... ...and i'm totally confused..can someone help me repair this codes... Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    1

    need some help on link lists using stacks...

    no errors and warnings...but dowanna accaept input... ...and i'm totally confused..can someone help me repair this codes...
    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    
    
    struct stackNode {
    
       char data;
    
       struct stackNode *nextPtr;
    
    };
    
    
    
    typedef struct stackNode StackNode;
    
    typedef StackNode *StackNodePtr;
    
    
    
    void push( StackNodePtr *, int );
    
    int pop( StackNodePtr * );
    
    int isEmpty( StackNodePtr );
    
    void printStack( StackNodePtr );
    
    void instructions( void );
    
    
    
    int main()
    
    { 
    
       StackNodePtr stackPtr = NULL;
    
       int choice;
    
       char value[20];
    
     
    
     instructions();
    
     printf( "? " );
    
     scanf( "%d", &choice );
    
    
    
       while ( choice != 3 ) { 
    
    
    
          switch ( choice ) { 
    
             case 1:
    
                printf( "Enter a student name: " );
    
    			fflush(stdin);
    
                gets(value );
    
                printStack( stackPtr );
    
                break;
    
             case 2:
    
                if ( !isEmpty( stackPtr ) )
    
                   printf( "The deleted name is %s.\n", 
    
                   pop( &stackPtr ) );
    
    			   printStack( stackPtr );
    
                break;
    
             default:
    
                printf( "Invalid choice.\n\n" );
    
                instructions();
    
                break;
    
          }
    
    
    
          printf( "? " );
    
          scanf( "%d", &choice );
    
       }
    
    
    
       printf( "End of run.\n" );
    
       return 0;
    
    }
    
    
    
    void instructions( void )
    
    { 
    
       printf( "Enter choice:\n"
    
              "1 to enter a student name stack\n"
    
              "2 to delete a student name stack\n"
    
              "3 to end program\n" );
    
    }
    
    
    
    void push( StackNodePtr *head, int info )
    
    { 
    
       StackNodePtr newPtr;
    
    
    
       newPtr = malloc( sizeof( StackNode ) );
    
       if ( newPtr != NULL ) { 
    
          newPtr->data = info;
    
          newPtr->nextPtr = *head;
    
          *head = newPtr;
    
       }
    
       else
    
          printf( "%d not inserted. No memory available.\n",
    
                  info );
    
    }
    
    
    
    int pop( StackNodePtr *head )
    
    { 
    
       StackNodePtr tempPtr;
    
       int popValue;
    
    
    
       tempPtr = *head;
    
       popValue = ( *head )->data;
    
       *head = ( *head )->nextPtr;
    
       free( tempPtr );
    
       return popValue;
    
    }
    
    
    
    void printStack( StackNodePtr currentPtr )
    
    { 
    
       if ( currentPtr == NULL )
    
          printf( "The stack is empty.\n\n" );
    
       else { 
    
          printf( "The stack is:\n" );
    
    
    
          while ( currentPtr != NULL ) { 
    
             printf( "%s --> ", currentPtr->data );
    
             currentPtr = currentPtr->nextPtr;
    
          }
    
    
    
          printf( "NULL\n\n" );
    
       }
    
    }
    
    
    
    int isEmpty( StackNodePtr topPtr )
    
    { 
    
       return topPtr == NULL;
    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >fflush(stdin)
    http://www.eskimo.com/~scs/C-faq/q12.26.html
    FAQ > Explanations of... > Why fflush(stdin) is wrong

    >gets()
    http://www.eskimo.com/~scs/C-faq/q12.23.html
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)

    Code:
    printf( "The deleted name is %s.\n", pop( &stackPtr ) );
    /* [560 Warning] argument no. 2 should be a pointer */
    Code:
    printf( "%s --> ", currentPtr->data );
    /* [560 Warning] argument no. 2 should be a pointer */
    Code:
    newPtr->data = info;
    /* [734 Info] Loss of precision (assignment) (31 bits to 7 bits) */
    Code:
    /* [714 Info] Symbol 'push(struct stackNode **, int)' not referenced */
    Last edited by Dave_Sinkula; 05-09-2005 at 01:56 PM. Reason: Better quote of code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm confused about link lists (again)
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 09:13 PM
  2. functions using link lists
    By chood72 in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2005, 03:51 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Link lists as datastructure
    By stseitli in forum C Programming
    Replies: 13
    Last Post: 07-09-2002, 08:34 AM
  5. double link lists
    By rumi_red in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2001, 10:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21