[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Stack using doubly linked list

arnuld

6/1/2011 10:57:00 AM

It works fine. I have explained everything in comments. As usual, all
comments for improving the program are welcomed :)

/* A stack (LIFO) implemented using doubly linked list for learning data
structures :) .
* Definiton of stack and operations on it are taken from Data Structures
and Algorithms by Aho, Hopcraft and Ullman
* page 67, section 2.3
*
* VERSION 0.0
*/


#include <stdio.h>
#include <stdlib.h>

enum {
VALUE_SUCCESS = 0, VALUE_ERROR = 1,
VALUE_TRUE = 1, VALUE_FALSE = 0
};


struct stack_node
{
int num;
struct stack_node* next;
struct stack_node* prev;
};


struct stack_list
{
struct stack_node* head;
};



struct stack_node* top(struct stack_list* s);
void pop(struct stack_list* s);
int push(struct stack_list* s, int i);
void makenull(struct stack_list* s);
int empty(struct stack_list* s);

void print_list(struct stack_list* s);
void print_node(struct stack_node* p);


int main(void)
{
struct stack_list* s = malloc(1 * sizeof *s);
if(NULL == s)
{
fprintf(stderr, "IN: %s @%d: Invalid Args!\n", __FILE__, __LINE__);
return EXIT_FAILURE;
}

print_list(s);
push(s,1);
print_list(s);

printf("empty = %d\n", empty(s));
push(s,2);
push(s,3);
print_list(s);
printf("empty = %d\n", empty(s));

makenull(s);
printf("empty = %d\n", empty(s));
print_list(s);

return EXIT_SUCCESS;
}



int push(struct stack_list* s, int i)
{
int ret = VALUE_ERROR;

if(NULL == s)
{
fprintf(stderr, "IN: %s @%d: Invalid Args!\n", __FILE__, __LINE__);
ret = VALUE_ERROR;
}
else
{
struct stack_node* p = malloc(1 * sizeof *p);
if(NULL == p)
{
fprintf(stderr, "IN: %s @%d: Out of Memory!\n", __FILE__,
__LINE__);
ret = VALUE_ERROR;
}
else
{
p->num = i;
p->next = NULL;
p->prev = NULL;
if(NULL == s->head)
{
printf("Adding first element\n");
s->head = p;
}
else
{
/* struct stack_node* h = s->head; */
p->next = s->head;
s->head->prev = p;
s->head = p;
}
ret = VALUE_SUCCESS;
}
}

return ret;
}


void pop(struct stack_list* s)
{
if(NULL == s || NULL == s->head)
{
printf("Nothing to pop\n");
}
else
{
struct stack_node* p = s->head;
s->head = s->head->next;
if(s->head) s->head->prev = NULL;
free(p);
}
}



void makenull(struct stack_list* s)
{
if(NULL == s)
{
fprintf(stderr, "IN: %s @%d: Invalid Args!\n", __FILE__, __LINE__);
}
else
{
while(s->head)
{
printf("Makenull removing: %d\n",s->head->num);
pop(s);
}
}
}


int empty(struct stack_list* s)
{
int ret = VALUE_FALSE;
if(NULL == s)
{
fprintf(stderr, "IN: %s @%d: Invalid Args!\n", __FILE__, __LINE__);
ret = VALUE_FALSE;
}
else if(NULL == s->head)
{
ret = VALUE_TRUE;
}
else
{
ret = VALUE_FALSE;
}

return ret;
}





/* ----------------------- my won fucntions for printing
-------------------------- */


void print_list(struct stack_list* s)
{
struct stack_node* p = s->head;

for(; p; p = p->next)
{
print_node(p);
}
printf("----------------------------------------------------\n\n");
}


void print_node(struct stack_node* p)
{
printf("p->num = %d\n", p->num);
}

====================== OUTPUT ===========================
[arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra stack_dl.c
[arnuld@dune C]$ ./a.out
----------------------------------------------------

Adding first element
p->num = 1
----------------------------------------------------

empty = 0
p->num = 3
p->num = 2
p->num = 1
----------------------------------------------------

empty = 0
Makenull removing: 3
Makenull removing: 2
Makenull removing: 1
empty = 1
----------------------------------------------------

[arnuld@dune C]$




--
www.lispmachine.wordpress.com
find my email-ID @above blog
1 Answer

luserXtrog

6/1/2011 4:31:00 PM

0

On Jun 1, 5:56 am, arnuld <sunr...@invalid.address> wrote:
> It works fine. I have explained everything in comments. As usual, all
> comments for improving the program are welcomed :)
>
> /* A stack (LIFO) implemented using doubly linked list for learning data
> structures :) .
>  * Definiton of stack and operations on it are taken from Data Structures
> and Algorithms by Aho, Hopcraft and Ullman
>  * page 67, section 2.3
>  *
>  * VERSION 0.0
>  */
>
> #include <stdio.h>
> #include <stdlib.h>
>
> enum {
>   VALUE_SUCCESS = 0, VALUE_ERROR = 1,
>   VALUE_TRUE = 1, VALUE_FALSE = 0
>
> };
>
> struct stack_node
> {
>   int num;
>   struct stack_node* next;
>   struct stack_node* prev;
>
> };
>
> struct stack_list
> {
>   struct stack_node* head;
>
> };
>
> struct stack_node* top(struct stack_list* s);
> void pop(struct stack_list* s);
> int push(struct stack_list* s, int i);
> void makenull(struct stack_list* s);
> int empty(struct stack_list* s);
>
> void print_list(struct stack_list* s);
> void print_node(struct stack_node* p);
>
> int main(void)
<snip>

Since you aked for all comments, FWIW, your code could be 8 lines
shorter
if you put the functions themselves here. That makes the functions
easier
to read since they're closer to the structures they work with. Or you
could
put the prototypes at the very top, ready to snip into a header file.

When you turn this into a module, *don't delete main()!*
Instead, wrap it in something like

#ifdef TESTMODULE
int main(void)
....
#endif

So you can easily perform tests on the module after it's been altered
to play with other modules.