It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Well, this is an option:
(there's no checking whether index is bigger than the number of elements of the linked list, you can make one easily though)
Code:typedef struct { int data; struct node *next; } node; node *getElementByIndex(node *myList, int index) { node *pos = myList; // pointing to the first element while(index >= 0) { pos = pos->next; --index; } return pos; }
Last edited by eXeCuTeR; 07-19-2008 at 04:21 PM.
King Mir: It is ok if we using circular buffer. But how to solve when the array is full? *confuse*
You have to create a new bigger array. Then you copy each element over. This is most easily done by dequeuing one element at a time, and queuing it in the bigger buffer queue. Or you can just copy the everything from the first element to the end of the buffer first, then every thing from the start of the buffer to the last index, not including the last index itself.
Pictorially you want to do this:
Old buffer
---|-- < start index
---|-- < one past the end index.
DEFABC
New buffer:
|----------- < start index
------|----- < one past the end index.
ABCDEFXXXXXX
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
A little bit more complicated I think, but it's OK...
Thank you very much King Mir