Hi I was hoping that some one could give me a hand with a bit of code I have a linked list and I am trying to count how many items are in the list could some one please heep me with a bit of code to do this.
Printable View
Hi I was hoping that some one could give me a hand with a bit of code I have a linked list and I am trying to count how many items are in the list could some one please heep me with a bit of code to do this.
Well, in case of non-circular linked list you have to iterate while the pointer to the next element != NULL.
some C pseudo-code:
head is a list and size is a number of elements in it.Code:struct node {
//data elements
struct node *next;
struct node *prev; // if this is a 2-way linked list
};
int size = 0;
for (node *p = head; p; p = head->next, ++size);
if you have a circulary-linked list:
Code:int size = 0;
for (node *pos = (head)->next; pos != (head); pos = pos->next, ++size);
std::list has size() for that.Quote:
Originally Posted by Robbo99
The op has asked about C code, not C++. btw, I hope he didn't mean C++ saying C.Quote:
Originally Posted by gstercken