S
Sumit
created a LIFO in C, need your suggestion what else more can i do on it,
and what should i change to make it best. need your suggestion specially
in function - stack_is_empty() , stack_remove() and stack_free() i have
doubt on those function.
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?
Thanks
#include <stdio.h>
#include <stdlib.h>
enum
{
SIZE_ARR = 20,
FAIL = -1,
NO = 0,
YES = 1
};
struct sstack_node
{
char name[SIZE_ARR];
struct sstack_node * next;
};
struct sstack_list
{
struct sstack_node * top;
};
/*--- FUNCTION DICLEARTION -----*/
struct sstack_list * stack_init( void );
void push(struct sstack_list *s , char * );
struct sstack_node * pop(struct sstack_list * s);
int stack_is_empty(struct sstack_list* s);
struct sstack_list * stack_refresh(struct sstack_list* s);
struct sstack_node * stack_frame(char *);
void stack_remove(struct sstack_list *);
void stack_free(struct sstack_list *);
int copy(char * to, char * from);
/*-------------------------------------*/
int main( void )
{
struct sstack_list * stack = NULL;
struct sstack_node * stnode = NULL;
stack = stack_init(); /* initializing new stack */
/* pushing some string into stack */
push(stack, "comp");
push(stack, "dot");
push(stack, "lang");
push(stack, "dot");
push(stack, "c");
push(stack, "programming");
push(stack, "unix");
push(stack, "linux");
/* poping */
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n\n", stnode -> name);
if( YES == stack_is_empty(stack) )
stack_refresh(stack);
else
printf("stack is not empty yet \n");
stack_remove(stack);
return 0;
}
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");
}
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;
}
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;
}
}
struct sstack_node * stack_frame( char * name)
{
struct sstack_node * newframe = NULL;
newframe = ( struct sstack_node * ) malloc (1 * sizeof *newframe);
if( NULL == newframe )
{
fprintf(stderr,"error: malloc fail \n");
return NULL;
}
copy( newframe -> name , name);
newframe -> next = NULL;
return newframe;
}
int copy(char to[], char from[])
{
int i=0;
while(( to = from ) != '\0')
++i;
to='\0';
return i;
}
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;
}
int stack_is_empty(struct sstack_list* s)
{
if( NULL == s )
{
printf("stack was not initlialized \n");
return FAIL;
}
else if( NULL == s -> top )
{
printf("stack is empty \n");
return YES;
}
else
{
printf("stack is not empty \n");
return NO;
}
printf("%s:%d impossible condition \n", __FILE__, __LINE__);
return NO;
}
struct sstack_list * stack_init( void )
{
struct sstack_list * s = NULL;
s = (struct sstack_list *) malloc (1 * sizeof *s);
if( NULL == s )
{
fprintf(stderr,"error: malloc fail \n");
return s;
}
s -> top = NULL;
return s;
}
and what should i change to make it best. need your suggestion specially
in function - stack_is_empty() , stack_remove() and stack_free() i have
doubt on those function.
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?
Thanks
#include <stdio.h>
#include <stdlib.h>
enum
{
SIZE_ARR = 20,
FAIL = -1,
NO = 0,
YES = 1
};
struct sstack_node
{
char name[SIZE_ARR];
struct sstack_node * next;
};
struct sstack_list
{
struct sstack_node * top;
};
/*--- FUNCTION DICLEARTION -----*/
struct sstack_list * stack_init( void );
void push(struct sstack_list *s , char * );
struct sstack_node * pop(struct sstack_list * s);
int stack_is_empty(struct sstack_list* s);
struct sstack_list * stack_refresh(struct sstack_list* s);
struct sstack_node * stack_frame(char *);
void stack_remove(struct sstack_list *);
void stack_free(struct sstack_list *);
int copy(char * to, char * from);
/*-------------------------------------*/
int main( void )
{
struct sstack_list * stack = NULL;
struct sstack_node * stnode = NULL;
stack = stack_init(); /* initializing new stack */
/* pushing some string into stack */
push(stack, "comp");
push(stack, "dot");
push(stack, "lang");
push(stack, "dot");
push(stack, "c");
push(stack, "programming");
push(stack, "unix");
push(stack, "linux");
/* poping */
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n", stnode -> name);
stnode = pop(stack);
printf("POPED: %s \n\n", stnode -> name);
if( YES == stack_is_empty(stack) )
stack_refresh(stack);
else
printf("stack is not empty yet \n");
stack_remove(stack);
return 0;
}
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");
}
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;
}
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;
}
}
struct sstack_node * stack_frame( char * name)
{
struct sstack_node * newframe = NULL;
newframe = ( struct sstack_node * ) malloc (1 * sizeof *newframe);
if( NULL == newframe )
{
fprintf(stderr,"error: malloc fail \n");
return NULL;
}
copy( newframe -> name , name);
newframe -> next = NULL;
return newframe;
}
int copy(char to[], char from[])
{
int i=0;
while(( to = from ) != '\0')
++i;
to='\0';
return i;
}
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;
}
int stack_is_empty(struct sstack_list* s)
{
if( NULL == s )
{
printf("stack was not initlialized \n");
return FAIL;
}
else if( NULL == s -> top )
{
printf("stack is empty \n");
return YES;
}
else
{
printf("stack is not empty \n");
return NO;
}
printf("%s:%d impossible condition \n", __FILE__, __LINE__);
return NO;
}
struct sstack_list * stack_init( void )
{
struct sstack_list * s = NULL;
s = (struct sstack_list *) malloc (1 * sizeof *s);
if( NULL == s )
{
fprintf(stderr,"error: malloc fail \n");
return s;
}
s -> top = NULL;
return s;
}