Thread: building a stack

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    2

    building a stack

    I need help modifying this code that builds a queue to one that builds a stack

    Code:
      /* =========================  MakeList ()  ============================ */
    /*  Builds a linked list of names based on data from a file.  This      */
    /*  functions calls MakeNode () and LoadNode () program functions.      */
    /*  It returns a pointer to the start of the list it built.             */
    /* ==================================================================== */
    struct record * MakeList ( void )
    {
      FILE * fp;                    /* output file pointer         */ 
      struct record * start = NULL, /* @ of first node in list     */
                    * current,      /* @ of where we are at any pt */
                    * new;          /* some pointers to use later  */
      char buffer [ 81 ];           /* space for string from file  */
    
    /* check output file success */
      if ( ( fp = fopen ( FILENAME, "r" ) ) == NULL ) {
        printf ( "MAKELIST: Unable to open input file %s!\n", FILENAME );
        exit ( 1 );
        }
    
      fgets ( buffer, 81, fp );              /* do a 'priming read'      */
      while ( ! feof ( fp ) ) {     /* read & process all file contents  */
        new = MakeNode ();
        if ( start == NULL )
          start = current = new;             /* start a new list         */
        else {
          current->next = new;               /* move pointer to new node */
          current = current->next;
          }
        LoadNode ( current, buffer );
        fgets ( buffer, 81, fp );            /* get rady for next test   */
        }
      fclose ( fp );
      return start;
    }  /* --------------------  end MakeList ()  -------------------------- */
    
    /* =========================  LoadNode ()  ============================ */
    /*  Loads a list node with data from a string passed in.  Calls no      */
    /*  program other functions.                                            */
    /*     Parms:  locn := ptr to list node to be filled                    */
    /*             str  := holds source data to be separated and loaded     */
    /* ==================================================================== */
    void LoadNode ( struct record * locn, char str [] )
    {
      sscanf ( str, "%s %s %d %f", locn->first, locn->last, &locn->age, 
        &locn->average );
      return;
    }  /* --------------------  end LoadNode ()  -------------------------- */
    
    /* =========================  MakeNode ()  ============================ */
    /*  Creates a new node of record type and returns pointer to it.        */
    /*  Calls no other program functions.                                   */
    /* ==================================================================== */
    struct record * MakeNode ( void )
    {
      struct record * newptr;     /* a local pointer */
    
    /* dynamicaly allocate some storage and initialize */
      newptr = ( struct record * ) calloc ( 1, sizeof ( struct record ) );
      newptr->next = NULL;
      return newptr;    /* send address of new node back */
    }  /* --------------------  end MakeNode ()  -------------------------- */

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    But it already is a stack.
    All you need is a function which takes nodes off the tail, rather than the head and you're done.

    Your file reading loop would be much simpler written as
    while ( fgets ( buffer, 81, fp ) != NULL )
    One less fgets() call, and one less feof() call.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    with a stack don't you have to update to start pointer on each loop iteration?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Or change this code so it pushes to the front of the queue rather than appends to the end.

    Doesn't make a bean of difference to me, your code, your choice.
    Either will work if done right.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM