Linked lists question

Started by
8 comments, last by misterwubwub 11 years ago

im learning about arrays and lists in java and i come across linked lists and i have yet to think of a useful way of using them... mabe im not fully understanding their capabilites? if anyone could clear this up greatly appreciated!

Advertisement

If an array exceeds the size you need to copy all the elements into a new larger array which can be expensive. You can also insert or remove items in the middle of the list easily whereas in an array you have to move elements up or down to do the same thing (assuming you want the ordering to remain the same).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

that sounds the same as a list? why have linked lists?

I thought an ArrayList in Java was a resizeable array and not a linked list... could be wrong though, it could actually be a linked list.

Basically:

array type data structure = fast random access to any element in the data, resizing can be expensive if the size exceeds current capacity, insertions/removals not at the end mean elements have to be moved.

linked list type data structure = slow random access (linear traversion is quick though), no penalty for resizing or insertion/deletion of elements anywhere in the list.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

so it works exactly the same as a list... i dont see any reason to use it over just a normal list...

are there any uses for it over lists?

Is a List different to an ArrayList? I'm not a Java programmer... If a "normal" List is a linked list then there's no point rolling your own.

Even if the interface is the same doesn't mean that the implementation is... you typically use data structures based on the most common operations you will be doing with the data (so random access => array type structure, lots of insertions/deletions, linear access only => linked list).

You may want to read up on "Big O" notation (and not in Cosmopolitan, it means something different there), which gives an idea of how fast you can expect an algorithm to perform with different data structures.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

According to the documentation for list:

The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.

Meaning LinkedList is made using List.

And from the documentation for linked list:

In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue.

im learning about arrays and lists in java and i come across linked lists and i have yet to think of a useful way of using them... mabe im not fully understanding their capabilites? if anyone could clear this up greatly appreciated!

Linked lists are good when you are doing a lot of random insertions, in a linked list this operation is very cheap whereas in an array you need to shuffle things around. For instance, say you have an array with one billion items in it, and you want to insert some item right in the middle. You will need to take the 500 million items after the insertion point, and shift them up 1 place up the array to make space for the element you're about to insert. In a linked list? No problem, find the element before and the element after where you want to insert your element, and simply modify their "next element" (and "previous element", if applicable) pointers, which naturally inserts the new element into the list without touching any element other than the two surrounding the insertion point.

On the other hand, you cannot have random access in a traditional linked list - if you want to read the Nth element, you need to walk through the linked list until you reach it, costing you N operations, instead of 1 operation for an array, since you can just directly get the element you want in an array. As you can see, they both have pros and cons.

The way you use the list is exactly the same, because they ultimately perform the same function (they are both lists) but depending on what you do with the list - what kind of operations, what sort of data you're manipulating - you should select the most appropriate one for your particular situation to optimize performance.

It is worth noting that unless you are in a very particular situation, linked lists do not win over arrays, simply because arrays are faster in modern hardware due to cache coherency and sequential memory access, whereas linked list traversal tends to jump all over memory. So the places where you would realistically use a linked list, other than for learning, are quite limited.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Linked Lists and Doubly linked lists are the pathways to understanding tree and graph data structures, which are very very useful. So think of Linked Lists (single or double) as a gateway drug :)

Beginner in Game Development?  Read here. And read here.

 

thank you bacterius!

This topic is closed to new replies.

Advertisement