singly link list

Upload: -aar-ad-

Post on 07-Jul-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/18/2019 Singly Link List

    1/12

    Linked list

    CSE 247

    Data Structures

  • 8/18/2019 Singly Link List

    2/12

    outline

    Quratulain Rajput2

    Linked lists Motivation

    Abstract data type (ADT)

    Basic operations of linked lists

    Insert, find, delete, traversal, etc.

  • 8/18/2019 Singly Link List

    3/12

  • 8/18/2019 Singly Link List

    4/12

    Linked Lists

    A linked list is a series of connected nodes Each node contains at least

    A piece of data (any type)

    Pointer to the next node in the list

    Head : pointer to the first node

    The Null indicates end of list

     A    Null

    Head

    B C

     A 

    data pointer

    node

  • 8/18/2019 Singly Link List

    5/12

    Linked List

      Actual picture in memory:1051

    1052

    1055

    1059

    1060

    1061

    1062

    1063

    1064

    1056

    1057

    1058

    1053

    1054 2

    6

    8

    7

    1

    1051

    1063

    1057

    1060

    null

    head 1054

    1063current

    2 6 8 7 1

    head

    current

    1065

  • 8/18/2019 Singly Link List

    6/12

    Implementation of Node class

    Quratulain Rajput6

    class Node{

    int data;

    Node next;

    Node () { // no argument constructor

    data=0;

    next=null;

    }

    Node (intV) { // one argument constructordata=V;

    next=null;

    } }

  • 8/18/2019 Singly Link List

    7/12

    Implementation of List class

    Quratulain Rajput7

    class LIST{

    Node Head;

    LIST (){

    Head=null;}

    // list operations (insertion, deletion, …)

    }

  • 8/18/2019 Singly Link List

    8/12

    A Singly Linked List

    Operations of List IsEmpty: determine whether or not the list is empty

    InsertNode: insert a new node in the end of list

    FindNode: find a node with a given value

    DeleteNode: delete a node with a given value

    DisplayList: print all the nodes in the list

    Clear: delete all node from list / make it an empty list

    InsertInOrder: Insertion according to sorted order.

  • 8/18/2019 Singly Link List

    9/12

    Insert operation in linked list

    Quratulain Rajput9

    public void INSERT(int value){

    Node NewNode=new Node(value);

    Node Temp=null, Prev=null;

    if(Head==null){

    Head=NewNode;}else{ Temp=Head;

    while(Temp!=null){

    Prev=Temp;

    Temp=Temp.next;}

    Prev.next=NewNode;

    } }

  • 8/18/2019 Singly Link List

    10/12

    Display list items

    Quratulain Rajput10

    public void DisplayList(){Node Temp=Head;

    int i=0;

    while(Temp!=null){

    i++;

    System.out.println("Item "+i+ Temp.data);

    Temp=Temp.next;

    }

    }

  • 8/18/2019 Singly Link List

    11/12

    Application of Singly linked list

    11

    Operations in databases

    In word processor, del

    Operating system cache scheduling algorithm (LRU)

    In browser BACK button. (List of urls)

  • 8/18/2019 Singly Link List

    12/12

    Analysis of Singly linked list

    12

    Insertion and deletion at current reference is one stepoperation.

    Search entire list when:

    To find an item that not stored in a list or stored at the end.

    Moving back from current position is requires to traverse a

    list again from beginning. This problem is resolved by doubly

    linked list.