Linked List

Discussion in 'Programming' started by anpm_dev, Aug 6, 2007.

  1. #1
    What is linked List how the pointers can be effectvely be used :confused:
     
    anpm_dev, Aug 6, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    A linked list is where each element in the list points at the next and previous elements. This differs from an array where the individual elements do not reference each other are always referenced via their position or key in the array.

    So, where you might have an array that is something like...

    Fruit[0]="Bannana"
    Fruit[1]="Grape"
    Fruit[2]="Plum"

    In a linked list you'd have something like...

    Fruit[0]['Name']="Bannana"
    Fruit[0]['Next']=1
    Fruit[0]['Prev']=null

    Fruit[1][0]="Grape"
    Fruit[1]['Next']=2
    Fruit[1]['Prev']=0

    Fruit[2][0]="Plum"
    Fruit[2]['Next']=null
    Fruit[2]['Prev']=2

    Note the above still uses an array structure to convey the idea - though it probably wouldn't be in real life.

    The advantage is clear. What happens when you need to keep the list in order but want to add another fruit - say a Grapefuit?

    With an array, you'd have to rebuild the entire array to keep everything sweet. With a linked list, you can very quickly find where it goes in the list, and just by changing the Next and Prev links, ensure your new item appearts in the correct place. So even if we just add a new element on the end for grapefruit, we can change around the next and previous pointers of the adjacent items to ensure it is inserted correctly..

    Fruit[0]['Name']="Bannana"
    Fruit[0]['Next']=1
    Fruit[0]['Prev']=null

    Fruit[1][0]="Grape"
    Fruit[1]['Next']=3
    Fruit[1]['Prev']=0

    Fruit[2][0]="Plum"
    Fruit[2]['Next']=null
    Fruit[2]['Prev']=2

    Fruit[3][0]="Grapefruit"
    Fruit[3]['Next']=2
    Fruit[3]['Prev']=1

    By organising the linked list in a tree form, finding an element in a massive list can be achieved very rapidly.

    For a more detailed explanation, have a look at wikipedia...

    http://en.wikipedia.org/wiki/Linked_list

    Which is really where you should have started for such a broad topic!
     
    ecentricNick, Aug 7, 2007 IP
  3. TnGurl

    TnGurl Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nick, thanks for that wiki link and your explanation. I've wondered about using linked lists also.
     
    TnGurl, Nov 12, 2008 IP