A
arnuld
Have written some LIFO in C. Have already discussed it here but some
improvements were left:
http://groups.google.com/group/comp.lang.c/browse_thread/thread/
f0ea73e0d528ae25/f309938a64506e42?q=stack+arnuld&lnk=ol&
any advice on new code will be appreciated. Please ignore push()
returning int while other functions returning pointer. Just tried to make
it more useful with returning int, will make change to others soon.
Implementation of LIFO conforms to definition presented in section 2.3 of
"Data Structures and Algorithms" by Aho, Hopcraft and Ullman.
/* A LIFO written using a singly linked list with 4 operations: Pop,
Push, Top and Print elements in the list.
*
* VERISON 0.5
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { VAL_ERR = -1, VAL_SUCC = 0, LIFO_ARR_SIZE = 10 };
struct my_LIFO
{
char arrc[LIFO_ARR_SIZE+1];
struct my_LIFO* next;
};
struct LIFO_list
{
struct my_LIFO* head;
};
int push( struct LIFO_list*, const char* );
struct LIFO_list* pop( struct LIFO_list* );
struct my_LIFO* top( struct LIFO_list* );
struct my_LIFO* make_null( struct LIFO_list* );
struct my_LIFO* is_empty( struct LIFO_list* );
struct LIFO_list* LIFO_new( void );
void LIFO_print( const struct LIFO_list* );
void LIFO_print_element( const struct my_LIFO* );
struct LIFO_list* LIFO_free( struct LIFO_list* );
int main( void )
{
struct my_LIFO* p = NULL;
struct LIFO_list* ms = NULL;
ms = LIFO_new();
LIFO_print(ms);
push(ms, "compppppppppppppp");
push(ms, "(dot)");
push(ms, "lang");
push(ms, "(dot)");
push(ms, "c");
LIFO_print(ms);
pop(ms);
p = top(ms);
LIFO_print(ms);
LIFO_free(ms);
free(ms);
ms = NULL;
return 0;
}
int push(struct LIFO_list* s, const char* c )
{
struct my_LIFO* p = malloc( 1 * sizeof *p );
if( NULL == p )
{
fprintf(stderr, "malloc() failed\n");
return VAL_ERR;
}
p->next = NULL;
/* If use gave us more characters than what we want, then its his
problem */
if( strlen(c) < LIFO_ARR_SIZE )
{
strcpy(p->arrc, c);
}
else
{
printf("Input is more than what is recommended. Adding '%s' failed
\n", c);
free(p);
return VAL_ERR;
}
if( NULL == s )
{
fprintf(stderr, "LIFO not initialized ?\n");
free(p);
}
else if( NULL == s->head )
{
/* printf("LIFO is Empty, adding first element\n"); */
s->head = p;
}
else
{
/* printf("LIFO not Empty, adding in front of first element
\n"); */
p->next = s->head;
s->head = p; /* push new element onto the head */
}
return VAL_SUCC;
}
struct LIFO_list* pop( struct LIFO_list* s )
{
struct my_LIFO* p = NULL;
if( NULL == s )
{
printf("There is no LIFO list ?\n");
}
else if( NULL == s->head )
{
printf("There is no element on the LIFO\n");
}
else
{
p = s->head;
s->head = s->head->next;
free(p);
}
return s;
}
struct my_LIFO* top( struct LIFO_list* s)
{
if( NULL == s )
{
printf("There is no LIFO list ?\n");
return NULL;
}
else if( NULL == s->head )
{
printf("There is no element on the LIFO\n");
}
return s->head;
}
/* Make a LIFO empty */
struct my_LIFO* make_null( struct LIFO_list* s )
{
if( NULL == s )
{
printf("Can not make NULL when there is no LIFO List\n");
return NULL;
}
else if( NULL == s->head )
{
printf("LIFO is already Empty\n");
}
else
{
LIFO_free(s);
}
return s->head;
}
struct my_LIFO* is_empty( struct LIFO_list* s )
{
if( NULL == s )
{
printf("There is no LIFO\n");
return NULL;
}
else if( NULL == s->head )
{
printf("LIFO is Empty\n");
}
else
{
printf("LIFO is not Empty\n");
}
return s->head;
}
/* ---------- small helper functions -------------------- */
struct LIFO_list* LIFO_free( struct LIFO_list* s )
{
if( NULL == s )
{
printf("Can't free a NULL LIFO list\n");
}
while( s->head ) pop(s);
return s;
}
struct LIFO_list* LIFO_new( void )
{
struct LIFO_list* p = malloc( 1 * sizeof *p );
if( NULL == p )
{
fprintf(stderr, "malloc() in LIFO Initialization failed\n");
exit( EXIT_FAILURE ); /* There is no point in going beyond this
point */
}
p->head = NULL;
return p;
}
void LIFO_print( const struct LIFO_list* s )
{
struct my_LIFO* p = NULL;
if( NULL == s )
{
printf("Can not print an Empty LIFO\n");
}
else
{
for( p = s->head; p; p = p->next ) LIFO_print_element(p);
}
printf("-------------------------- \n");
}
void LIFO_print_element(const struct my_LIFO* s)
{
if( s ) printf("arrc = %s\n", s->arrc);
}
========================= OUTPUT ==========================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra LIFO.c
[arnuld@dune programs]$ ./a.out
--------------------------
Input is more than what is recommended. Adding 'compppppppppppppp' failed
arrc = c
arrc = (dot)
arrc = lang
arrc = (dot)
improvements were left:
http://groups.google.com/group/comp.lang.c/browse_thread/thread/
f0ea73e0d528ae25/f309938a64506e42?q=stack+arnuld&lnk=ol&
any advice on new code will be appreciated. Please ignore push()
returning int while other functions returning pointer. Just tried to make
it more useful with returning int, will make change to others soon.
Implementation of LIFO conforms to definition presented in section 2.3 of
"Data Structures and Algorithms" by Aho, Hopcraft and Ullman.
/* A LIFO written using a singly linked list with 4 operations: Pop,
Push, Top and Print elements in the list.
*
* VERISON 0.5
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { VAL_ERR = -1, VAL_SUCC = 0, LIFO_ARR_SIZE = 10 };
struct my_LIFO
{
char arrc[LIFO_ARR_SIZE+1];
struct my_LIFO* next;
};
struct LIFO_list
{
struct my_LIFO* head;
};
int push( struct LIFO_list*, const char* );
struct LIFO_list* pop( struct LIFO_list* );
struct my_LIFO* top( struct LIFO_list* );
struct my_LIFO* make_null( struct LIFO_list* );
struct my_LIFO* is_empty( struct LIFO_list* );
struct LIFO_list* LIFO_new( void );
void LIFO_print( const struct LIFO_list* );
void LIFO_print_element( const struct my_LIFO* );
struct LIFO_list* LIFO_free( struct LIFO_list* );
int main( void )
{
struct my_LIFO* p = NULL;
struct LIFO_list* ms = NULL;
ms = LIFO_new();
LIFO_print(ms);
push(ms, "compppppppppppppp");
push(ms, "(dot)");
push(ms, "lang");
push(ms, "(dot)");
push(ms, "c");
LIFO_print(ms);
pop(ms);
p = top(ms);
LIFO_print(ms);
LIFO_free(ms);
free(ms);
ms = NULL;
return 0;
}
int push(struct LIFO_list* s, const char* c )
{
struct my_LIFO* p = malloc( 1 * sizeof *p );
if( NULL == p )
{
fprintf(stderr, "malloc() failed\n");
return VAL_ERR;
}
p->next = NULL;
/* If use gave us more characters than what we want, then its his
problem */
if( strlen(c) < LIFO_ARR_SIZE )
{
strcpy(p->arrc, c);
}
else
{
printf("Input is more than what is recommended. Adding '%s' failed
\n", c);
free(p);
return VAL_ERR;
}
if( NULL == s )
{
fprintf(stderr, "LIFO not initialized ?\n");
free(p);
}
else if( NULL == s->head )
{
/* printf("LIFO is Empty, adding first element\n"); */
s->head = p;
}
else
{
/* printf("LIFO not Empty, adding in front of first element
\n"); */
p->next = s->head;
s->head = p; /* push new element onto the head */
}
return VAL_SUCC;
}
struct LIFO_list* pop( struct LIFO_list* s )
{
struct my_LIFO* p = NULL;
if( NULL == s )
{
printf("There is no LIFO list ?\n");
}
else if( NULL == s->head )
{
printf("There is no element on the LIFO\n");
}
else
{
p = s->head;
s->head = s->head->next;
free(p);
}
return s;
}
struct my_LIFO* top( struct LIFO_list* s)
{
if( NULL == s )
{
printf("There is no LIFO list ?\n");
return NULL;
}
else if( NULL == s->head )
{
printf("There is no element on the LIFO\n");
}
return s->head;
}
/* Make a LIFO empty */
struct my_LIFO* make_null( struct LIFO_list* s )
{
if( NULL == s )
{
printf("Can not make NULL when there is no LIFO List\n");
return NULL;
}
else if( NULL == s->head )
{
printf("LIFO is already Empty\n");
}
else
{
LIFO_free(s);
}
return s->head;
}
struct my_LIFO* is_empty( struct LIFO_list* s )
{
if( NULL == s )
{
printf("There is no LIFO\n");
return NULL;
}
else if( NULL == s->head )
{
printf("LIFO is Empty\n");
}
else
{
printf("LIFO is not Empty\n");
}
return s->head;
}
/* ---------- small helper functions -------------------- */
struct LIFO_list* LIFO_free( struct LIFO_list* s )
{
if( NULL == s )
{
printf("Can't free a NULL LIFO list\n");
}
while( s->head ) pop(s);
return s;
}
struct LIFO_list* LIFO_new( void )
{
struct LIFO_list* p = malloc( 1 * sizeof *p );
if( NULL == p )
{
fprintf(stderr, "malloc() in LIFO Initialization failed\n");
exit( EXIT_FAILURE ); /* There is no point in going beyond this
point */
}
p->head = NULL;
return p;
}
void LIFO_print( const struct LIFO_list* s )
{
struct my_LIFO* p = NULL;
if( NULL == s )
{
printf("Can not print an Empty LIFO\n");
}
else
{
for( p = s->head; p; p = p->next ) LIFO_print_element(p);
}
printf("-------------------------- \n");
}
void LIFO_print_element(const struct my_LIFO* s)
{
if( s ) printf("arrc = %s\n", s->arrc);
}
========================= OUTPUT ==========================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra LIFO.c
[arnuld@dune programs]$ ./a.out
--------------------------
Input is more than what is recommended. Adding 'compppppppppppppp' failed
arrc = c
arrc = (dot)
arrc = lang
arrc = (dot)