binary linked list

Upload: jyoti222

Post on 08-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 Binary Linked List

    1/13

    TERM PAPER

    TOPIC- GENRALISED BINARYLINKED LIST

    SUBMITTED BY-JYOTI

    CLASS-B.TECH CSE

    ROLL NO- A-15

    REG NO.-10901555

    SUBMITTED TO- Mr.Harjit Singh

  • 8/7/2019 Binary Linked List

    2/13

    BINARY LINKED LISTS

    In computer science, a binary linked list or adoubly linked list, is a linked data structure thatconsists of a set of data records, each having twospecial linkfields that contain references to theprevious and to the next record in the sequence. Itcan be viewed as two singly-linked lists formed fromthe same data items, in two opposite orders.

    A binary-linked list whose nodes contain three fields:an integer value, the link to the next node, and the

    link to the previous node.

    The two links allow walking along the list in either

    direction with equal ease. Compared to a singly-linked list, modifying a binary-linked list usuallyrequires changing more pointers, but is sometimessimpler because there is no need to keep track of theaddress of the previous node.

  • 8/7/2019 Binary Linked List

    3/13

    OPERATIONS ON A BINARY LINKED

    LISTSThe following operations can be performed onbinary or doubly linked lists:

    Inserting an element

    Deleting an element

    Traversing and Searching

    INSERTING AN ELEMENT

    To insert an element in the list, the first task is toallocate memory for a new node, assign the elementto be inserted to the info field of the node, and thenthe new node is placed at the appropriate position byadjusting appropriate pointers. Insertion in the listcan take place at the following positions:

    At the beginning of the list At the end of the list After a given element Before a given element

    Insertion at the Beginning of the List

    First, test whether the linked list is empty, if yes,then the element is inserted as the first and only oneelement by performing the following steps:

  • 8/7/2019 Binary Linked List

    4/13

    Assign NULL to the next pointer and prev pointerfields of the new node

    Assign the address of the new node to head andtail pointer variables.

    If the list is not empty, then the element is insertedas the first element of the list by performing thefollowing steps:

    Assign NULL to the prev pointer field of the newnode.

    Assign the value of the head variable (theaddress of the first element of the existing list)to the next pointer field of the new node.

    Assign the address of the new node to prevpointer field of the node currently pointed byhead variable, i.e. first element of the existinglist.

    Finally assign the address of the new node to thehead variable

    Inserting at the End of the List

    First test whether the linked list is initially empty, ifyes, then the element is inserted as the first and onlyone element by performing the following steps:

    Assign NULL value to the next pointer and prevpointer field of the new node

    Assign address of new node to head and tailpointer variable.

    If the list is not empty, then element is inserted asthe last element of the list by performing thefollowing steps:

  • 8/7/2019 Binary Linked List

    5/13

    Assign NULL value to the next pointer field of thenew node.

    Assign value of the tail variable (the address ofthe last element of the existing list) to the prev

    pointer field of the new node. Assign address of the new node to the next

    pointer field of the node currently pointed by tailvariable i.e. last element of the existing list.

    Finally assign the address of the new node to tailvariable.

    DELETING AN ELEMENT

    To delete an element from the list, first the pointersare set properly and then the memory occupied bythe node to be deleted is deallocated (freed).

    Deletion in the list can take place at the followingpositions.

    At the beginning of the list At the end of the list After a given element Before a given element

    Deleting from the Beginning of the List

    An element from the beginning of the list can bedeleted by performing the following steps:

  • 8/7/2019 Binary Linked List

    6/13

    Assign the value of head (address of the firstelement of the list) to a temporary variable (saytemp)

    There are two further cases:

    1. If there is only one element in the existinglist, both head and tail are set to NULL.

    2. If there is more than one element in the listthen

    Assign NULL to the prev pointer field ofthe second node.

    Assign the address of the second nodeto head.

    Deallocate the memory occupied by the nodepointed to by temp.

    Deleting from the End of the List

    An element from the end of the list can be deleted byperforming the following steps:

    Assign the value of tail (address of the last

    element of the list) to a temporary variable (saytemp)

    Further there are two cases:1. If there is only one element in the existing

    list, set both head and tail to NULL.2. If there is more than one element in the list

    then Assign NULL to the next pointer field of

    the second last node. Assign the address of the second last

    node to tail. Deallocate the memory occupied by the node

    pointed to by temp.

  • 8/7/2019 Binary Linked List

    7/13

    Freeing up the Entire List

    The binary linked list can be deleted either from thebeginning or from the end. To delete from the

    beginning, use the following steps:

    Assign the head pointer to a temporary variable,say temp.

    Advance the head pointer to the next node. Deallocate the memory occupied by the node

    pointed to by temp.

    Repeat the above steps until the entire list is deleted.

    Finally, set the tail pointer to NULL.

    TRANSVERSING A BINARY OR DOUBLY LINKEDLIST

    Transversal of a linked list means to move throughthe list (means to visit each node exactly once)

    A binary linked list can be traversed either way andthat too very conveniently.

    Inorder traversal Reverse order traversal

    Inorder Traversal

    To traverse the binary linked list, we walk the listfrom the beginning, and process each element untilwe reach the last element.

  • 8/7/2019 Binary Linked List

    8/13

    .cf { font-family: Lucida Console; font-size: 9pt; color:black; background: white; } .cl { margin: 0px; } .cb1{ color: green; } .cb2 { color: blue; } .cb3 { color:maroon; }

    void traverseinorder(node *head)

    {while(head!=NULL)

    printf("%d\n",head->info);head=head->next;

    }

    }

    To call the above function, use:traverseinorder(head);

    Reverse Order Traversal

    The following listing shows how to traverse a binarylinked list in the backward direction. Note that thetail pointer will need to be passed in a call to thisfunction.

    void traversereverseorder(node *tail){

    if(tail!=NULL){

    printf("%d\n",tail->info); //print datatail = tail->prev; // go to previous node

    }}

  • 8/7/2019 Binary Linked List

    9/13

    To call the above function, use:traversereverseorder(tail);

    SEARCHING A ELEMENT

    Searching means to find an element in a linked listand it stars from first node to the last node. If item isfound at any location then there is no need to gofurther more in a linked list, if item is not found inany location then its output message is- the search isunsuccessful or item does not found.

    The binary linked list can be traversed in any order to

    reach the given element. The following listingdemonstrates how to search an element from thebeginning.

    node *search (node *head, int item){

    while(head!=NULL){

    if(head->info==item) // if the values match,return head; // return the matching node.

    head=head->next; // otherwise, move on}return NULL;

    }

    Example of a call to the above function:

    node *found = search(head, 10); // search for thevalue 10 in the linked list

    Inserting a node

  • 8/7/2019 Binary Linked List

    10/13

    These symmetric functions add a node either after orbefore a given node, with the diagram demonstratingafter:

    function insertAfter(Listlist, Node node, NodenewNode)

    newNode.prev := nodenewNode.next := node.next

    ifnode.next == nulllist.lastNode := newNode

    elsenode.next.prev := newNode

    node.next := newNodefunction insertBefore(Listlist, Node node, NodenewNode)

    newNode.prev := node.prev

    newNode.next := node ifnode.prev is null

    list.firstNode := newNode else

    node.prev.next := newNodenode.prev := newNode

    We also need a function to insert a node at the

    beginning of a possibly-empty list:

    function insertBeginning(Listlist, Node newNode) iflist.firstNode == null

    list.firstNode := newNodelist.lastNode := newNode

    http://en.wikipedia.org/wiki/File:Doubly_linked_list_insert_after.png
  • 8/7/2019 Binary Linked List

    11/13

    newNode.prev := nullnewNode.next := null

    elseinsertBefore(list, list.firstNode, newNode)

    A symmetric function inserts at the end:

    function insertEnd(Listlist, Node newNode) iflist.lastNode == null

    insertBeginning(list, newNode) else

    insertAfter(list, list.lastNode, newNode)

    Deleting a node

    Removing a node is easier, only requiring care withthe firstNode and lastNode:

    function remove(Listlist, Node node) ifnode.prev == null

    list.firstNode := node.next

    elsenode.prev.next := node.next ifnode.next == null

    list.lastNode := node.prev else

    node.next.prev := node.prev destroy node

    One subtle consequence of this procedure is that

    deleting the last element of a list sets both firstNodeand lastNode to null, and so it handles removing thelast node from a one-element list correctly. Noticethat we also don't need separate "removeBefore" or"removeAfter" methods, because in a doubly-linked

  • 8/7/2019 Binary Linked List

    12/13

    list we can just use "remove(node.prev)" or"remove(node.next)" where these are valid.

    ALGORITHM :-INSERT:-1.if(full list)1.return=02.end if3.found = Search list(list,pPre,pSucc,datain)4.if (not found)1.Allocate(pnew)2.pNew->data=datain3.if (pPre is null)

    1.pNew ->back=NULL2.pnew ->fore = list.head3.list.head=pNew4.else1.pNew-> fore = pPre->fore2.pNew ->back=pPre5.end if6.if (pPre ->fore NULL)1.list.rear=pNew

    7.else1.pSucc->back=pNew8.end if9.pPre->fore=pNew10.list.coount=list.count+111.return(1)5.endif6.return(2)END algorithm.

    DELETE:-1.if(pdlt NULL)1.abort (impossible condition in delete double)2.end if3.list.count=list.count+14.if(pDlt->back ot NULL)

  • 8/7/2019 Binary Linked List

    13/13

    1.pPred=pDlt->back2.pPred->fore=pDlt->fore5.else1.list.head=pDlt->fore

    6.endif7.if (pDlt->fore not NULL)1.pSucc=pDlt->fore2.pSucc->back=pDlt->back8.else1.list.rear=pDlt->back9.end if10.recycle(pDlt)11.returnEnd algorithm