Sign in to follow this  
misterwubwub

Linked lists question

Recommended Posts

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!

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

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.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by Bacterius

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this