Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!usc!wuarchive!uunet!mcsun!unido!uniol!Ingo.Wilken From: Ingo.Wilken@arbi.informatik.uni-oldenburg.de (Ingo Wilken) Newsgroups: comp.lang.c Subject: Re: LIFO Keywords: with structures Message-ID: <3792@uniol.UUCP> Date: 6 Nov 90 22:32:06 GMT References: <111@dlss2.UUCP> Organization: University of Oldenburg, Germany Lines: 82 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)