Right, so I'm a little confused at how to go about this. O.o (It's probably really simple...)
I've got three struct typedefs, 'Node', 'List' and 'Stack'. These are their contents:
typedef struct Node
{
void *data; /* Pointer to data stored in node */
struct Node *next; /* pointer to next node in list */
} Node;
typedef struct
{
Node *head; /* First element in list */
Node *tail; /* last element in list */
Node *iterator; /* Used to iterate through list */
int size; /* number of elements currently in list */
} List;
typedef struct
{
List *dataList; /* Stack data is contained in a linked list */
} Stack;
I already have a push and pop function for the stack which seem to be compiling properly, so let's not worry about that.
I need a function that can count all the elements in the stack and return them in an int. I'm unsure about how to go about this in a logical manner and I can't seem to find any kind of example solution. I'm guessing it would involve something like Stack stackVariable->dataList->size, but I could be compeltely wrong.
Anybody want to give a stab at the logic? D:
----
Edit: I may have worked it out. Does this look right?
int stk_size(Stack *stkPtr)
{
while(stkPtr->dataList != NULL)
{
stkPtr->dataList->size = stkPtr->dataList->size + 1;
}
return stkPtr->dataList->size;
}
Edited by fantanoice, 05 November 2009 - 05:07 AM.


Sign In
Create Account




Back to top









