CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Printing our stack data structure

To print out our whole data structure, we can't just use a standard C11 function as C11 doesn't know/understand our data structure.

Thus we'll write our own function, print_stack, to traverse the stack and successively print each item, using printf.

Again, we must check for the case of the empty stack:


void print_stack(void)
{
    STACKITEM  *thisitem = stack;

    while(thisitem != NULL) {
        printf("%i", thisitem->value);

        thisitem = thisitem->next;

        if(thisitem != NULL)
            printf(" -> ");
    }
    if(stack != NULL)
	printf("\n");
}

Again, our stack is simple because each node only contains a single integer. If more complex, we may call a different function from within print_stack to perform the actual printing:


    ....
    print_stack_item( thisitem );     

 


CITS2002 Systems Programming, Lecture 19, p5, 3rd October 2023.