-4

I have implemented singly liked list and doubly linked list in Java, now my teacher asked me to implement array using singly and doubly linked list (In Java). I came across a few solutions on how to implement singlylinkedlist and doubly linked list using array but I need it other way around. Can anyone help me on this.

3
  • 3
    Show your few solutions and probably people would give you a hand. Commented Mar 20, 2013 at 6:23
  • 1
    First list down all the properties of array. then tell which property is probelm for you?
    – Azodious
    Commented Mar 20, 2013 at 6:24
  • 1
    People will be happy to help if you share what you've tried. In the meantime, I recommend checking out this article.
    – jahroy
    Commented Mar 20, 2013 at 6:25

1 Answer 1

2

I don't know your exact assignment. However, assuming that by "implementing an array" you mean providing a way of performing the base functionality of an array, then think about what an array lets you do.

An array has a fixed size decided upon at creation time and is not resizeable, so implement a linked list with a constructor specifying its size, and do not let the list grow or shrink after that. It might be usefull to create all of the nodes at that time, but it might not be strictly required.

An array provides random access (you can access or assign to any slot in an array, instead of a linked list's front / end only approach) so implement methods to get and set at indices. If you know your way around a linked list, this should be a pretty simple thing to do (just iterate from the head or tail as many cells in as it takes to get to the specified index).

It needs to be able to do:

// the constructor with size specified.
PseudoArray pa = new PseudoArray(10);

// assigning to arbitrary indices
pa.set(0, "Hello, I am the first element.");
pa.set(5, "I am in the middle of the list.");

// reading from arbitrary indices
pa.get(0);
pa.get(5);

You might have to add things like making sure that only objects of certain types get in to it, but basically, random access and fixed, predetermined size are the biggest issues.

Hopefully I have understood your question.

1
  • This is a tremendous answer. Welcome to StackOverflow!
    – jahroy
    Commented Mar 20, 2013 at 6:55

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.