Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array representation of linked lists

I am reading Algorithms in C++ book by Robert Sedgwick. It was mentioned that linked lists can be represented by arrays. Can any one show the simple implementation of linked lists using arrays ?

Is it possible to implement Josephous problem using array implementation of linked lists ? If possible a sample implementation would be helpful.

Thanks!

like image 365
venkysmarty Avatar asked Nov 26 '10 13:11

venkysmarty


1 Answers

Instead of a pointer or reference to the next element of the linked list, record the index in the array of the next element. Use an index that cannot possibly be an array index (e.g. -1) to indicate the end of the list.

Given you're asking then for a solution to a well-known and much solved problem, I'll assume it's an assignment and leave the solution to the reader :)

like image 149
The Archetypal Paul Avatar answered Sep 20 '22 19:09

The Archetypal Paul