[comp.lang.c] LIFO

james@dlss2.UUCP (James Cummings) (11/02/90)

	I have been struggling with what ought to be an easy problem.
I am trying to build a Last In First Out stack that I can store the 
several structures in.  I want to be able to "pop" them back out again
in reverse order.
	So far, with a structure like:
		struct myst {
			char word1[10];
			char word2[10];
			struct myst *next;
		};
	and some pointers of the same form, I have only managed to retreive
the last structure on the stack...I seem to be able to go no further.

	Could some kind sole, please nudge me in the right direction or
give me a *pointer to a book?

Env: Sys V 3.2.1
     K&R C

cdm@gem-hy.Berkeley.EDU (Dale Cook) (11/03/90)

In article <111@dlss2.UUCP>, james@dlss2.UUCP (James Cummings) writes:
|> 
|> 	I have been struggling with what ought to be an easy problem.
|> I am trying to build a Last In First Out stack that I can store the 
|> several structures in.  I want to be able to "pop" them back out again
|> in reverse order.
|> 	So far, with a structure like:
|> 		struct myst {
|> 			char word1[10];
|> 			char word2[10];
|> 			struct myst *next;
|> 		};
|> 	and some pointers of the same form, I have only managed to retreive
|> the last structure on the stack...I seem to be able to go no further.
|> 
|> 	Could some kind sole, please nudge me in the right direction or
                   ^^^^^^^^^
   You want a gentle kick? Or maybe a friendly fish? [couldn't resist...]

|> give me a *pointer to a book?

Given the structure you have defined, and no information on how you are
using it, I can only guess that your problem is that you have defined a
stack structure with only one entry.  Are you allocating storage for 
each stack entry as you need it? If not, you need to, or else pick an
arbitrary stack maximum and preallocate your structure as an array.  
----------------------------------------------------------------------
--- Dale Cook
"In order to comprehend the infinite, one need only consider
man's capacity for stupidity"     --- Edward Abbey
----------------------------------------------------------------------
Lengthy disclaimer follows; press 'n' to skip...

========== long legal disclaimer follows, press n to skip ===========
^L
Neither the United States Government or the Idaho National Engineering
Laboratory or any of their employees, makes any warranty, whatsoever,
implied, or assumes any legal liability or responsibility regarding any
information, disclosed, or represents that its use would not infringe
privately owned rights.  No specific reference constitutes or implies
endorsement, recommendation, or favoring by the United States
Government or the Idaho National Engineering Laboratory.  The views and
opinions expressed herein do not necessarily reflect those of the
United States Government or the Idaho National Engineering Laboratory,
and shall not be used for advertising or product endorsement purposes.

Ingo.Wilken@arbi.informatik.uni-oldenburg.de (Ingo Wilken) (11/07/90)

james@dlss2.UUCP (James Cummings) writes:
>		struct myst {
>			char word1[10];
>			char word2[10];
>			struct myst *next;
>		};
>	and some pointers of the same form, I have only managed to retreive
	    ^^^^^^^^^^^^
>the last structure on the stack...I seem to be able to go no further.

You only need one pointer, the stackbase. All you need to do is store a new
item on the stack as the first item in the linked list. So if you push
item1, item2, item3 on the stack, the list looks like this:

empty stack:    stackbase -> NULL
push item1 :    stackbase -> item1 -> NULL
push item2 :    stackbase -> item2 -> item1 -> NULL
push item3 :    stackbase -> item3 -> item2 -> item1 -> NULL
pop  item3 :    stackbase -> item2 -> item1 -> NULL


Well, here a the "standard" stack routines:

-----cut here-----
struct myst *stackbase = (struct myst *) NULL;


push( char *word1, char *word2 )
{
  struct myst *help;

  help = (struct myst *) malloc( sizeof(struct myst) );

  if( help == (struct myst *) NULL )
    /* out of memory */
  else
  {
    strcpy( help->word1, word1 );
    strcpy( help->word2, word2 );
    help->next = stackbase;
    stackbase  = help;
  }
}


char *
tos_word1()           /* top of stack */
{
  if( stackbase == (struct myst *) NULL )
    /* stack is empty */
  else
    return( stackbase->word1 );
}

/* the same thing again for tos_word2() */


pop()
{
  struct myst *help;

  if( stackbase == (struct myst *) NULL )
    /* stack is empty */
  else
  {
    help      = stackbase;
    stackbase = stackbase->next;

    free( (void *) help );
  }
}
-----cut here-----


Hope this helps,
  Ingo
-- 
Ingo Wilken, CS Student, Univ. of Oldenburg, W-Germany * IRC-Nickname: Nobody
----------------------+ Ingo.Wilken@arbi.informatik.uni-oldenburg.de
My opinions may have  | wilken@uniol.UUCP (..!uunet!unido!uniol!wilken)
changed, but not the  | wilken%arbi.informatik.uni-oldenburg.de@DOLUNI1.BITNET
fact that I am right! | wilken@uniol.ZER * Voice: +049 04461 80800 (Weekends)