Jump to content

  • Log In with Google      Sign In   
  • Create Account


Linked lists question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 misterwubwub   Members   -  Reputation: 129

Like
0Likes
Like

Posted 30 March 2013 - 06:45 PM

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!



Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5203

Like
0Likes
Like

Posted 30 March 2013 - 06:49 PM

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

#3 misterwubwub   Members   -  Reputation: 129

Like
0Likes
Like

Posted 30 March 2013 - 07:00 PM

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



#4 Paradigm Shifter   Crossbones+   -  Reputation: 5203

Like
0Likes
Like

Posted 30 March 2013 - 07:05 PM

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, 30 March 2013 - 07:06 PM.

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

#5 misterwubwub   Members   -  Reputation: 129

Like
0Likes
Like

Posted 30 March 2013 - 07:09 PM

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?



#6 Paradigm Shifter   Crossbones+   -  Reputation: 5203

Like
0Likes
Like

Posted 30 March 2013 - 07:14 PM

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

#7 Dragonsoulj   Crossbones+   -  Reputation: 2015

Like
0Likes
Like

Posted 30 March 2013 - 07:54 PM

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.



#8 Bacterius   Crossbones+   -  Reputation: 8272

Like
2Likes
Like

Posted 30 March 2013 - 08:34 PM

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, 30 March 2013 - 09:49 PM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#9 Alpha_ProgDes   Crossbones+   -  Reputation: 4688

Like
0Likes
Like

Posted 30 March 2013 - 08:50 PM

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.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#10 misterwubwub   Members   -  Reputation: 129

Like
0Likes
Like

Posted 30 March 2013 - 11:47 PM

thank you bacterius!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS