It would help if you specified the part you didn't understand.
You can also try google. to find more explanations and even java applets demonstrating circular queues.
Printable View
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;
}
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
A little bit more complicated I think, but it's OK...
Thank you very much King Mir :)