LIFO

Ingo Wilken Ingo.Wilken at arbi.informatik.uni-oldenburg.de
Wed Nov 7 09:32:06 AEST 1990


james at 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 at arbi.informatik.uni-oldenburg.de
My opinions may have  | wilken at uniol.UUCP (..!uunet!unido!uniol!wilken)
changed, but not the  | wilken%arbi.informatik.uni-oldenburg.de at DOLUNI1.BITNET
fact that I am right! | wilken at uniol.ZER * Voice: +049 04461 80800 (Weekends)



More information about the Comp.lang.c mailing list