0

Do I have to search for the node as a stack or queue, and when I find it set the data that the node returns equal to local variables?

Or is there a way of calling a known position in the list, like how you can with an array.

Visually what I'm asking here is:

So how Arrays work like this:

ArrayofStrings[Postion] returns String located at Position

Is there a way to do the same with a Linked List like this?

SinglyLinkedList(Node) returns Multiple(string) Data(double) 

I'm guessing I have to search the list for the node, but I wanted to make sure there wasn't an easier way to do this.

UberPwnd
  • 5
  • 5
  • 1
    What? Are you talking about .get? – Marco Acierno May 29 '14 at 11:20
  • Yes, that's how linkedlist works even java LinkedList traverses through element to get to the specific element. check out http://stackoverflow.com/a/322742/1629362 answer – bitkot May 29 '14 at 11:28
  • @AmitChotaliya So what you're saying is, in order to access a specific node, that I have to basically do a linear search of the nodes until I find a matching node? Weather I want to read, or modify the data in that node. – UberPwnd May 29 '14 at 11:40
  • Exactly, LinkedLists are good for traversing, adding or removing anywhere because you just have to change two pointers(reference in Java) but If you want to go to a specific location you have go one by one. – bitkot May 29 '14 at 12:06
  • Whereas Arrays are good for traversing, adding data at the end and getting element from a specific location. If you add or remove element from the middle it will shift all the elements after that location. – bitkot May 29 '14 at 12:07
  • @AmitChotaliya I wish you would have wrote that in the answer so I could give you my +1 and accept it as my answer! You were super helpful! Thank you! – UberPwnd May 29 '14 at 14:30

2 Answers2

0

All lists in java provide an implementation of get, which returns the n'th element in list.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
0

LinkedLists pros and cons

pros

  • traversing
  • adding or removing anywhere because you just have to change two pointers(reference in Java)

cons

  • get by index because it will traverse through elements until it finds the specific one (doubly linked list may be a little faster because if you have total count you can start from beginning or end depending on the index ie if there are 100 elements and you want to goto 75 you can start from the other end which will only require to traverse 25 elements)

Arrays pros and cons.

pros

  • traversing (even subset),
  • adding data at the end
  • getting element from a specific location.

cons

  • If you add or remove element from the middle it will shift all the elements after that location.
bitkot
  • 4,466
  • 2
  • 28
  • 39