Jump to content

Getting the size of a Stack in C

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
fantanoice

fantanoice

    Newbie

  • Members
  • PipPip
  • 26 posts
Edit: Nevermind this folks. Turns out the function was actually provided in a source file. My bad...?

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.

Posted Image
Posted Image
Join Dashing today! http://www.dashing.co.nr


#2
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
I think you could just use sizeof()
Something like this might work:
size=sizeof(stkPtr->dataList);

Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#3
fantanoice

fantanoice

    Newbie

  • Members
  • PipPip
  • 26 posts
I don't need to know the size in 'memory', I need to know 'how many', if that makes sense. ;)

Posted Image
Posted Image
Join Dashing today! http://www.dashing.co.nr


#4
SolidState

SolidState

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
You could increase and decrease the count variable everytime you call your push and pop functions.

Edited by SolidState, 05 November 2009 - 02:23 AM.
Thought better


#5
fantanoice

fantanoice

    Newbie

  • Members
  • PipPip
  • 26 posts

SolidState said:

You could increase and decrease the count variable everytime you call your push and pop functions.
That could work, though I have specific instructions to make a function that will count the elements in the stack. Also, the stack needs to be able to clear at any moment in time, meaning that I would need to assign 0 to a variable every time that happened. It probably isn't the most efficient way to go about things.

But thanks for the input guys.

Posted Image
Posted Image
Join Dashing today! http://www.dashing.co.nr