2.linked list

Upload: meenakshib89

Post on 10-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 2.Linked List

    1/46

    LINKED LISTS

    A linked list is a non sequential collection

    of data items called nodes

  • 8/8/2019 2.Linked List

    2/46

    LINKED LISTS

    Each node in a linked list has basically two

    fields Data field

    Link field

  • 8/8/2019 2.Linked List

    3/46

    LINKED LISTS

    The data field contains an actual value to

    be stored and processed

    The link field contains the address of the

    next data item in the linked list

    Data field Link field

  • 8/8/2019 2.Linked List

    4/46

    LINKED LISTS

    The logical and physical ordering of dataitems in a linked list need not be the same

    The link field of the last node contains

    NULL rather than a valid address

    It is a null pointer and indicates the end of

    the list

  • 8/8/2019 2.Linked List

    5/46

    LINKED LISTS

    EMPTY LIST

    If the nodes are not present in a linked list,

    then it is called an empty linked list or

    empty list

    It is also called the null list

  • 8/8/2019 2.Linked List

    6/46

    LINKED LISTS

    REPRESENTATION OF LINEAR LINKED LIST

    The linear linked list can be represented in memorywith the following declarations

    class node

    { int data;

    node link;

    node(int val){ data=val;link=null;

    }}

  • 8/8/2019 2.Linked List

    7/46

    LINKED LISTS

    A new node can be created using

    node freshnode= new node(value);

    It can be accessed as

    freshnode.data

    freshnode.link

  • 8/8/2019 2.Linked List

    8/46

    LINKED LISTS

    OPERATION ON LINKED LIST

    The basic operations to be performed on the

    linked lists are Creation

    Insertion

    Deletion Traversing

    Searching

    Display

  • 8/8/2019 2.Linked List

    9/46

    LINKED LISTS

    Creation

    This operation is used to create a linked list

  • 8/8/2019 2.Linked List

    10/46

    LINKED LISTS

    Insertion

    This operation is used to insert a new node in

    the linked list at the specified position. Anew node may be inserted

    At the beginning of a linked list

    At the end of a linked list At the specified position in linked list

    If the list itself is empty, then the new node

    is inserted as a first node

  • 8/8/2019 2.Linked List

    11/46

    LINKED LISTS

    Deletion

    This operation is used to delete a node from

    the linked list. A node may be deleted from

    the

    Beginning of a linked list End of a linked list

    Specified item in the list

  • 8/8/2019 2.Linked List

    12/46

    LINKED LISTS

    Traversing

    It is a process of going through all the

    nodes of a linked list from one end to the

    other end

  • 8/8/2019 2.Linked List

    13/46

    LINKED LISTS

    Searching

    This operation is used to search a particularelement in a linked list

    If the desired element is found, we signaloperation SUCCESSFUL, otherwiseUNSUCCESSFULL

  • 8/8/2019 2.Linked List

    14/46

    LINKED LISTS

    Display

    This operation is used to print each and

    every nodes information

  • 8/8/2019 2.Linked List

    15/46

    LINKED LISTS

    TYPES OF LINKED LIST

    We can put linked list into the followingtypes

    Singly linked list

    Doubly linked list

    Circular linked list

    Circular doubly linked list

  • 8/8/2019 2.Linked List

    16/46

    LINKED LISTS

    Singly Linked List (Linear Linkedlist) A Singly linked list is one in which all

    nodes are linked together in some

    sequential manner

    10 20 30 40 nullstart

  • 8/8/2019 2.Linked List

    17/46

  • 8/8/2019 2.Linked List

    18/46

    LINKED LIST

    Doubly Linkedlist

    A doubly linked list is one in which all

    nodes are linked together by multiple links

    which help in accessing both the successornode (next node) and predecessor node

    (previous node) for any arbitrary node

    within the list

  • 8/8/2019 2.Linked List

    19/46

    LINKED LIST

    Doubly Linkedlist

    Each node in a doubly linked list has two

    pointer fields Pointer to left node

    Pointer to right node

    20 5 15 nullnull

    10

    start

    left data right

  • 8/8/2019 2.Linked List

    20/46

    LINKED LISTS

    Circular Linked List

    A Circular linked list is one which has nobeginning and no end

    10 20 30 40start

  • 8/8/2019 2.Linked List

    21/46

    LINKED LIST

    Circular Doubly Linkedlist

    A circular doubly linked list is one which

    has both the successor pointer andpredecessor pointer in circular manner

    20 5 1510

    start

    left data right

  • 8/8/2019 2.Linked List

    22/46

    LINKED LIST

    ADVANTAGES

    Linked lists are dynamic data structures

    can grow and shrink during the execution of aprogram

    Efficient memory utilization

    Memory is not pre-allocated

    Insertion and deletions are easier and

    efficient

  • 8/8/2019 2.Linked List

    23/46

    LINKED LIST

    DISADVANTAGES

    More memory If the number of fields are more, then more

    memory space is needed

    Access to an arbitrary data item is little bit

    cumbersome and also time consuming

  • 8/8/2019 2.Linked List

    24/46

    LINKED LIST

    Insertinganodeatthebeginning

    20 40 25 48 nullstart

    32 null

    newnode

  • 8/8/2019 2.Linked List

    25/46

    LINKED LIST

    Insertinganodeatthebeginning

    20 40 25 48 nullstart

    32

    newnode

  • 8/8/2019 2.Linked List

    26/46

    LINKED LIST

    Void insert_at_begin(int item){

    node newnode=new node(item);if (start!=null)

    newnode.link=start;

    start=newnode;

    }

  • 8/8/2019 2.Linked List

    27/46

    LINKED LIST

    Insertinganodeattheend

    20 40 25 48 nullstart

    32 null

    newnode

  • 8/8/2019 2.Linked List

    28/46

    LINKED LIST

    Insertinganodeattheend

    20 40 25 48 nullstart

    32 null

    newnode

    loc

  • 8/8/2019 2.Linked List

    29/46

    LINKED LIST

    Insertinganodeattheend

    20 40 25 48start

    32 null

    newnode

    loc

  • 8/8/2019 2.Linked List

    30/46

    LINKED LIST

    Insertinganodeattheend

    Void insert_at_end(int item){ node newnode=new node(item);

    loc=start;

    while (loc.link!=null)loc=loc.link;

    loc.link=newnode;

    }

  • 8/8/2019 2.Linked List

    31/46

    LINKED LIST

    Insertinganewnodeatthespecified

    position

    20 40 25 48 nullstart

    32 null

    newnode

    Insert a new node after

    the second node

  • 8/8/2019 2.Linked List

    32/46

    LINKED LIST

    Insertinganewnodeatthespecified

    position

    20 40 25 48 nullstart

    32 null

    newnode

    loc

  • 8/8/2019 2.Linked List

    33/46

    LINKED LIST

    Insertinganewnodeatthespecified

    position

    20 40 25 48 nullstart

    32

    newnode

    loc

  • 8/8/2019 2.Linked List

    34/46

    LINKED LIST

    Insertinganewnodeatthespecifiedposition

    Void insert_specific_position(int item, int pos){ node newnode= new node(item);

    loc=start; temp=1;

    while(temp

  • 8/8/2019 2.Linked List

    35/46

  • 8/8/2019 2.Linked List

    36/46

    LINKED LIST

    Deletingthefirstnode

    20 40 25 48 nullstart

  • 8/8/2019 2.Linked List

    37/46

    LINKED LIST

    Deletingthefirstnode

    Void delete_firstnode(){ if (start!= null)

    start=start.link;

    }

  • 8/8/2019 2.Linked List

    38/46

    LINKED LIST

    Deletingthelastnode

    20 40 25 48 nullstart

  • 8/8/2019 2.Linked List

    39/46

    LINKED LIST

    Deletingthelastnode

    20 40 25 48 nullstart

    prev last

  • 8/8/2019 2.Linked List

    40/46

    LINKED LIST

    Deletingthelastnode

    20 40 25 null 48 nullstart

    prev last

  • 8/8/2019 2.Linked List

    41/46

    LINKED LIST

    Deletingthelastnode

    Void delete_lastnode(){ last=start;

    while(last.link!=null){ prev=last;

    last=last.link;

    }

    prev.link=null;

    }

  • 8/8/2019 2.Linked List

    42/46

    LINKED LIST

    Deletingthenodecontainingaparticular

    item

    Delete the node containing value 25

    20 40 25 48 nullstart

  • 8/8/2019 2.Linked List

    43/46

    LINKED LIST

    Deletingthenodecontainingaparticular

    item

    Delete the node containing value 25

    20 40 25 48 nullstart

    prev loc

  • 8/8/2019 2.Linked List

    44/46

    LINKED LIST

    Deletingthenodecontainingaparticular

    item

    Delete the node containing value 25

    20 40 25 48 nullstart

    prev loc

  • 8/8/2019 2.Linked List

    45/46

    LINKED LIST

    Deletingthenodecontainingaparticularitem

    Void delete_particular_item(int item){ loc=start; prev=start;

    while (loc!=null && loc.data!=item){ prev=loc;

    loc=loc.link;

    }if (prev==start)

    { start=null;

    return;

    }

  • 8/8/2019 2.Linked List

    46/46

    LINKED LIST

    Deletingthenodecontainingaparticular

    item

    if (loc!=null)prev.link=loc.link;

    else return unsuccessful;

    }