- 1

how can i write a function which return the number of elements of a stack?

13th Oct 2017, 7:30 PM
shirin aflakian
shirin aflakian - avatar
3 Answers
+ 1
Either make it traverse the whole stack (while next doesn't equal null) and increment your counter variable everytime you pass on a node, or just keep track of your stack while adding/deleting with a count variable in the stack's class.
13th Oct 2017, 7:35 PM
Muhammad Kamal
Muhammad Kamal - avatar
+ 1
I didn't say you'll return the elements (it's a pointer btw) you'll only count it.. count = 0 while condition do count = count + 1 end while return count
14th Oct 2017, 1:04 PM
Muhammad Kamal
Muhammad Kamal - avatar
- 1
Without node ,,,? How can i return numbers not element?
13th Oct 2017, 7:53 PM
shirin aflakian
shirin aflakian - avatar