Re: LIFO in C, need your suggestions



On Wed, 05 Aug 2009 11:38:17 +0000, Sumit wrote:

I wrote one more extra function copy(), because for using only one
function "strcpy()" from string library unnecessary i have to include
whole library <string.h> , that is what i wrote copy() function. is it
good idea?

I am sure other will comment on this.



/*--- FUNCTION DICLEARTION -----*/

int stack_is_empty(struct sstack_list* s);

This function is a boolean, so I guess right name must be is_stack_empty()



void stack_remove(struct sstack_list * s ) {
if( s == NULL)
{
printf(" there is no stack list \n"); return;
}

if( s -> top != NULL )
{
printf(" stack is not empty yet, still making it empty \n");
pop(s);
}
stack_free( s );
printf("stack has been removed successfully \n");
}


I wonder what exactly is the difference between stack_remove() and
stack_free(). You don't explain that. A very general and random
interpretation will be stack_remove() will remove the stack and stack_free
() will free the stack, which means stack_remove() only removes the stack
but does not free the memory held by its different elements ? And when
all the elements are removed how you will free() them when pointers are
gone ? I am sure someone will make some different meaning out of it and
it will not be his mistake. I am not even saying your style is wrong, Its
just that you did not tell *us* what they do. Put a line or 2 for
comments. PURPOSE: ...... WHAT IT DOES: ..... WHAT I WANT: ....
WHAT HAPPEND: ....




void stack_free(struct sstack_list * sfree) {
if( sfree != NULL)
{
free( sfree );
sfree = NULL;
return;
}
printf("address is already free \n"); return ;

}



struct sstack_node* pop(struct sstack_list * s) {
struct sstack_node * temp = NULL;

if( s == NULL)
{
printf("stack is not initialized \n"); return NULL;
}

if( s -> top == NULL )
{
printf("Stack is empty \n");
return NULL;
}
temp = s -> top;
if(s -> top == NULL)
{
printf("s t a c k - e m p t y \n");
return NULL;
}
s -> top = s -> top -> next ;
return temp;
}

I see people using pop() 2 ways. One that always free()s the element
after popping it (me) and the other when it pops but does not free()
(you).
Just telling you, keep this on mind.



void push(struct sstack_list *s , char *name ) {
struct sstack_node * newframe = NULL; newframe = stack_frame(name);
/* got a new node for insert into stack */ if( NULL == newframe )
return;
if( NULL == s -> top ) /* chekcing first frame into stack */
s -> top = newframe;
else
{
newframe -> next = s -> top;
s -> top = newframe;
}
}

void push(struct sstack_list *s , char *name )
{
struct sstack_node * newframe = malloc( sizeof * newframe * 1) ;
if( NULL == newframe )
{
fprintf(stderr, "FILE: %s, LINE: %d : malloc() failed\n",
__FILE__, __LINE);
return;
}

newframe->next = NULL
strcpy(newframe, name);

if( NULL == s -> top ) /* chekcing first frame into stack */
s -> top = newframe;
else
{
newframe -> next = s -> top;
s -> top = newframe;
}
}

Its just 3 lines longer than yours. It removes the need of stack_frame().
I only make a new function when either there is a special need or when
the lines of code increase beyond 40. Its a matter of choice I guess but
I will like to know the viewpoint of others.



int copy(char to[], char from[])
{
int i=0;
while(( to[i] = from[i] ) != '\0')
++i;

to[i]='\0';
return i;
}


I really wonder why you need that ? strcpy() does that same. Why are you
reinventing the wheel ? You have included *whole* of stdlib.h but then
used only one thing from it (malloc), then why you scared of including
string.h ?



struct sstack_list* stack_refresh(struct sstack_list* s) {
if( s == NULL )
{
printf("stack is not initlialized \n "); return NULL;
}
if( s -> top != NULL)
{
printf("stack is not empty cannot refresh stack until it is not
be empty \n"); return s;
}
s -> top = NULL;
printf("stack has been refreshed successfully \n"); return s;
}

Whats the meaning of stack_refresh ? .. you have stack with 100 elements
and you call stack_refresh() which sets the top to null without fee()ing
the memory held by 100 elements.



struct sstack_list * stack_init( void ) {
struct sstack_list * s = NULL;
s = (struct sstack_list *) malloc (1 * sizeof *s); if( NULL == s )


C-FAQs 7.7b







--
www.lispmachine.wordpress.com
my email is @ the above blog.

.



Relevant Pages

  • Re: DOLIST with empty list?
    ... To decide to return the empty list, ... But this fundamentally redefines DOLIST/DOSUBS in a manner ... any new results on the stack, you get a list of those ... and I replace each by recalling the variable's contents, ...
    (comp.sys.hp48)
  • Re: Beat Toudai
    ... of empty pdf pages. ... KPDF got empty little, and empty big, pages. ... But still it and evince ... Execution stack: ...
    (sci.lang.japan)
  • Re: LIFO in C, need your suggestions
    ... struct sstack_node * next; ... stnode = pop; ... printf(" stack is not empty yet, ...
    (comp.lang.c)
  • Re: LIFO in C, need your suggestions
    ... The empty test can return FAIL but that is not interesting to anyone. ... struct sstack_node * next; ... void stack_free(struct sstack_list * sfree) ...
    (comp.lang.c)