+ 2
Can linked list be traverse without pointer?
It is only way to traverse the linked list using pointer- for(temp=start;temp->start!=NULL;temp=temp->next); Is there any other way to traverse linked list without using pointer-?
1 Answer
+ 1
I would say no, since a linked list typically looks something like this:
class Item {
//some members...
Item* next;
};
You are asking if there is a way to avoid having the 'next' member be a pointer. I don't think so, since by definition a linked list item keeps a "link" to the next item (else it is not a linked list).
What else could 'next' be but a pointer? A reference like this..
class Item {
//some members...
Item& next;
};
...wont work since you need to initialize a reference when it is declared (and it cannot be NULL) - and for a linked list this will in principle be impossible for the last link in the chain (since there are no 'next' for the last one).
The only (potential) way I can see is something like this:
class Item {
//some members...
static Item members[];
static int index;
static Item next(){return members[index];}
void add(){/*add *this to members*/}
};
But I would argue this is closer to a Vector than to a LinkedList!