linked lists

18
Linked Lists

Upload: taharazvi

Post on 18-Nov-2015

3 views

Category:

Documents


1 download

TRANSCRIPT

  • Linked Lists

  • ExampleWe would like to keep a list of inventory records but only as many as we need

    An array is a fixed size

    Instead use a linked listWhat are the disadvantages of using a linked list?

  • Linked ListNode one element of the linked listObject data stored in the node examples?next a reference to the next node in the listlast node points to NULL

    ObjectnextObjectnextObjectnext

  • Linked Listhead keeps track of the head of the listtail keeps track of the last node in the listtail not always used

    ObjectnextObjectnextObjectnextheadtail

  • Insertion at Head

    Object1nextheadObject2nextObject3nextInsert herenew_nodetail

  • Insertion at HeadCreate new_node store object in new_nodePoint new_node next to the node head points to

    Object1nextheadObject2nextObject3nextnew_nodetail

  • Insertion at HeadCreate new_node store object in new_nodePoint new_node next to the node head points toPoint head to new_node

    Object1nextheadObject2nextObject3nextnew_nodetail

  • Insertion at HeadDoes this algorithm work for the list below?

    Object1nextheadObject2nextObject3nextheadObject3nextnew_nodetailtail

  • Insertion at HeadCreate new_node store object in new_nodePoint new_node next to the node head points toPoint head to new_nodeIf tail points to NULLpoint tail to new_node

  • Insertion at HeadCreate new_node store object in new_nodePoint new_node next to the node head points toPoint head to new_nodeIf tail points to NULLpoint tail to new_node

    headObject3nextnew_nodetail

  • Insertion at Tail

    headObject3nextnew_nodetailObject1nextheadObject2nextObject3nextInsert heretailnew_node

  • Findfind(3)

    find(16) - always remember to deal with special cases

    5next3next12nextheadtail

  • DeletionDeletion of headComplexity?

    Deletion of tailComplexity?

    Object1nextheadObject2nextObject3nexttail

  • Insertion/Deletion in MiddleInsert between Object1 and Object2Delete Object1

    Object1nextheadObject2nextObject3nexttail

  • Doubly Linked ListsEach node keeps a pointer to the next node and to the previous nodeMakes some operations (such as insertion at end) more efficientCosts?At the beginning and end of the list are sentinel nodes Simplify insertion/deletion algorithm

    Object3prevnexttrailerObject2prevnextObject1prevnextheader

  • Doubly Linked ListsInsertion and deletion at beginning/end

    Insertion and deletion in middle

    Object3prevnexttrailerObject2prevnextObject1prevnextheadertrailerheader

  • Doubly Linked ListsInsertion

    Object3prevnexttrailerObject2prevnextObject1prevnextheaderObject4prevnextinsert herenew_node

  • Doubly Linked ListsInsertion at head

    Set next of new_node to point to what headers next points toSet prev of node that headers next points to to point to new_nodeSet prev of new_node to point to headerSet headers next to point to new_nodeNumber 1 must come before number 4Insertion at trailer?Deletion?

    Object3prevnexttrailerObject2prevnextObject1prevnextheaderObject4prevnextinsert herenew_node